Esempio n. 1
0
    def has_color_table(self):
        """Return if vector has color table associated in file system;
        Color table stored in the vector's attribute table well be not checked

        >>> test_vect = Vector(test_vector_name)
        >>> test_vect.open(mode='r')
        >>> test_vect.has_color_table()
        False

        >>> test_vect.close()
        >>> from grass.pygrass.utils import copy, remove
        >>> copy(test_vector_name,'mytest_vect','vect')
        >>> from grass.pygrass.modules.shortcuts import vector as v
        >>> v.colors(map='mytest_vect', color='population', column='value')
        Module('v.colors')
        >>> mytest_vect = Vector('mytest_vect')
        >>> mytest_vect.open(mode='r')
        >>> mytest_vect.has_color_table()
        True
        >>> mytest_vect.close()
        >>> remove('mytest_vect', 'vect')
        """
        loc = Location()
        path = join(loc.path(), self.mapset, 'vector', self.name, 'colr')
        return True if exists(path) else False
Esempio n. 2
0
    def has_color_table(self):
        """Return if vector has color table associated in file system;
        Color table stored in the vector's attribute table well be not checked

        >>> test_vect = Vector(test_vector_name)
        >>> test_vect.open(mode='r')
        >>> test_vect.has_color_table()
        False

        >>> test_vect.close()
        >>> from grass.pygrass.utils import copy, remove
        >>> copy(test_vector_name,'mytest_vect','vect')
        >>> from grass.pygrass.modules.shortcuts import vector as v
        >>> v.colors(map='mytest_vect', color='population', column='value')
        Module('v.colors')
        >>> mytest_vect = Vector('mytest_vect')
        >>> mytest_vect.open(mode='r')
        >>> mytest_vect.has_color_table()
        True
        >>> mytest_vect.close()
        >>> remove('mytest_vect', 'vect')
        """
        loc = Location()
        path = join(loc.path(), self.mapset, 'vector', self.name, 'colr')
        return True if exists(path) else False
Esempio n. 3
0
 def patch(self):
     """Patch the final results."""
     bboxes = split_region_tiles(width=self.width, height=self.height)
     loc = Location()
     mset = loc[self.mset.name]
     mset.visible.extend(loc.mapsets())
     noutputs = 0
     for otmap in self.module.outputs:
         otm = self.module.outputs[otmap]
         if otm.typedesc == "raster" and otm.value:
             rpatch_map(
                 otm.value,
                 self.mset.name,
                 self.msetstr,
                 bboxes,
                 self.module.flags.overwrite,
                 self.start_row,
                 self.start_col,
                 self.out_prefix,
             )
             noutputs += 1
     if noutputs < 1:
         msg = "No raster output option defined for <{}>".format(self.module.name)
         if self.module.name == "r.mapcalc":
             msg += ". Use <{}.simple> instead".format(self.module.name)
         raise RuntimeError(msg)
Esempio n. 4
0
 def patch(self):
     """Patch the final results."""
     bboxes = split_region_tiles(width=self.width, height=self.height)
     loc = Location()
     mset = loc[self.mset.name]
     mset.visible.extend(loc.mapsets())
     for otmap in self.module.outputs:
         otm = self.module.outputs[otmap]
         if otm.typedesc == 'raster' and otm.value:
             rpatch_map(otm.value, self.mset.name, self.msetstr, bboxes,
                        self.module.flags.overwrite, self.start_row,
                        self.start_col, self.out_prefix)
Esempio n. 5
0
def findmaps(type, pattern=None, mapset="", location="", gisdbase=""):
    """Return a list of tuple contining the names of the:

    * map
    * mapset,
    * location,
    * gisdbase

    """
    from grass.pygrass.gis import Gisdbase, Location, Mapset

    def find_in_location(type, pattern, location):
        res = []
        for msetname in location.mapsets():
            mset = Mapset(msetname, location.name, location.gisdbase)
            res.extend(
                [
                    (m, mset.name, mset.location, mset.gisdbase)
                    for m in mset.glist(type, pattern)
                ]
            )
        return res

    def find_in_gisdbase(type, pattern, gisdbase):
        res = []
        for loc in gisdbase.locations():
            res.extend(find_in_location(type, pattern, Location(loc, gisdbase.name)))
        return res

    if gisdbase and location and mapset:
        mset = Mapset(mapset, location, gisdbase)
        return [
            (m, mset.name, mset.location, mset.gisdbase)
            for m in mset.glist(type, pattern)
        ]
    elif gisdbase and location:
        loc = Location(location, gisdbase)
        return find_in_location(type, pattern, loc)
    elif gisdbase:
        gis = Gisdbase(gisdbase)
        return find_in_gisdbase(type, pattern, gis)
    elif location:
        loc = Location(location)
        return find_in_location(type, pattern, loc)
    elif mapset:
        mset = Mapset(mapset)
        return [
            (m, mset.name, mset.location, mset.gisdbase)
            for m in mset.glist(type, pattern)
        ]
    else:
        gis = Gisdbase()
        return find_in_gisdbase(type, pattern, gis)
Esempio n. 6
0
File: grid.py Progetto: caomw/grass
 def patch(self):
     """Patch the final results."""
     bboxes = split_region_tiles(width=self.width, height=self.height)
     loc = Location()
     mset = loc[self.mset.name]
     mset.visible.extend(loc.mapsets())
     for otmap in self.module.outputs:
         otm = self.module.outputs[otmap]
         if otm.typedesc == 'raster' and otm.value:
             rpatch_map(otm.value,
                        self.mset.name, self.msetstr, bboxes,
                        self.module.flags.overwrite,
                        self.start_row, self.start_col, self.out_prefix)
Esempio n. 7
0
    def patch(self):
        """Patch the final results."""
        # make all mapset in the location visible
        loc = Location()
        mset = loc[self.mset.name]
        mset.current()
        mset.visible.extend(loc.mapsets())
        # patch all the outputs
        bboxes = split_region_tiles(width=self.width, height=self.height)
        inputs = self.module.inputs
        print("Start patching the segments")
        start = time.time()
        rpatch_map(
            inputs.outputs_prefix % inputs.thresholds[-1],
            self.mset.name,
            self.msetstr,
            bboxes,
            self.module.flags.overwrite,
            self.start_row,
            self.start_col,
            self.out_prefix,
        )
        print("%s, required: %.2fs" % (OPTS["output"], time.time() - start))

        # segment
        print("Start running segment for the last time in the whole region")
        start = time.time()
        iseg = Module("i.segment")
        threshold = self.module.inputs.thresholds[-1]
        iseg(
            group=self.module.inputs.group,
            output=self.module.outputs.output,
            threshold=threshold,
            method=self.module.inputs.method,
            similarity=self.module.inputs.similarity,
            minsize=self.module.inputs.minsizes[-1],
            memory=self.memory,
            iterations=3,
            seeds=self.module.inputs.outputs_prefix % threshold,
        )
        print("%s, required: %.2fs" % (OPTS["output"], time.time() - start))

        self.mset.current()
        if self.move:
            copy_rasters(
                [
                    self.module.outputs.output,
                ],
                self.gisrc_dst,
                self.gisrc_src,
            )
Esempio n. 8
0
def register(mset_pat,
             rast_pat,
             datefmt="%Y%m%d_%H",
             mapset_fmt="r%Y_%m",
             raster_fmt="{base}_{date:%Y%m}",
             sep='|',
             overwrite=False,
             **kwargs):
    reg_dir = gscript.tempdir()
    env = os.environ.copy()
    loc = Location()
    temporals = {}
    for mset_name in loc.mapsets(pattern=mset_pat):
        menv = gs.grass_init(gs.GISBASE,
                             loc.gisdbase,
                             loc.name,
                             mset_name,
                             env=env.copy())
        for rname in loc[mset_name].glist("raster", pattern=rast_pat):
            base, date = extract_date(rname, datefmt=datefmt)
            trast = raster_fmt.format(base=base, date=date)
            rasts = temporals.get(trast, [])
            rasts.append((rname, "{date:%Y-%m-%d %H:%M:%S}".format(date=date)))
            temporals[trast] = rasts

    # create
    for tname, trasts in temporals.items():
        gscript.run_command(
            "t.create",
            type="strds",
            temporaltype="absolute",
            output=tname,
            title=tname,
            description="COSMO REA6: {tname}".format(tname=tname),
            semantictype="mean",
            overwrite=overwrite,
            env=menv)
        csvfile = os.path.join(reg_dir, "{}.csv".format(tname))
        with open(csvfile, mode="w") as csv:
            for row in trasts:
                csv.write(sep.join(row) + '\n')
        gscript.run_command("t.register",
                            overwrite=overwrite,
                            type="raster",
                            input=tname,
                            file=csvfile,
                            separator=SEP.get(sep, sep),
                            env=menv)
Esempio n. 9
0
File: grid.py Progetto: caomw/grass
    def clean_location(self, location=None):
        """Remove all created mapsets.

        :param location: a Location instance where we are running the analysis
        :type location: Location object
        """
        if location is None:
            if self.n_mset:
                self.n_mset.current()
            location = Location()

        mapsets = location.mapsets(self.msetstr.split('_')[0] + '_*')
        for mset in mapsets:
            Mapset(mset).delete()
        if self.n_mset and self.n_mset.is_current():
            self.mset.current()
Esempio n. 10
0
    def clean_location(self, location=None):
        """Remove all created mapsets.

        :param location: a Location instance where we are running the analysis
        :type location: Location object
        """
        if location is None:
            if self.n_mset:
                self.n_mset.current()
            location = Location()

        mapsets = location.mapsets(self.msetstr.split('_')[0] + '_*')
        for mset in mapsets:
            Mapset(mset).delete()
        if self.n_mset and self.n_mset.is_current():
            self.mset.current()
Esempio n. 11
0
    def run(self, patch=True, clean=True):
        """Run the GRASS command

        :param patch: set False if you does not want to patch the results
        :type patch: bool
        :param clean: set False if you does not want to remove all the stuff
                      created by GridModule
        :type clean: bool
        """
        self.module.flags.overwrite = True
        self.define_mapset_inputs()
        if self.debug:
            for wrk in self.get_works():
                cmd_exe(wrk)
        else:
            pool = mltp.Pool(processes=self.processes)
            result = pool.map_async(cmd_exe, self.get_works())
            result.wait()
            pool.close()
            pool.join()
            if not result.successful():
                raise RuntimeError(
                    _("Execution of subprocesses was not successful"))

        if patch:
            if self.move:
                os.environ['GISRC'] = self.gisrc_dst
                self.n_mset.current()
                self.patch()
                os.environ['GISRC'] = self.gisrc_src
                self.mset.current()
                # copy the outputs from dst => src
                routputs = [
                    self.out_prefix + o
                    for o in select(self.module.outputs, 'raster')
                ]
                copy_rasters(routputs, self.gisrc_dst, self.gisrc_src)
            else:
                self.patch()

        if self.log:
            # record in the temp directory
            from grass.lib.gis import G_tempfile
            tmp, dummy = os.path.split(G_tempfile())
            tmpdir = os.path.join(tmp, self.module.name)
            for k in self.module.outputs:
                par = self.module.outputs[k]
                if par.typedesc == 'raster' and par.value:
                    dirpath = os.path.join(tmpdir, par.name)
                    if not os.path.isdir(dirpath):
                        os.makedirs(dirpath)
                    fil = open(
                        os.path.join(dirpath, self.out_prefix + par.value),
                        'w+')
                    fil.close()

        if clean:
            self.clean_location()
            self.rm_tiles()
            if self.n_mset:
                gisdbase, location = os.path.split(self.move)
                self.clean_location(Location(location, gisdbase))
                # rm temporary gis_rc
                os.remove(self.gisrc_dst)
                self.gisrc_dst = None
                sht.rmtree(os.path.join(self.move, 'PERMANENT'))
                sht.rmtree(os.path.join(self.move, self.mset.name))
Esempio n. 12
0
 def find_in_gisdbase(type, pattern, gisdbase):
     res = []
     for loc in gisdbase.locations():
         res.extend(
             find_in_location(type, pattern, Location(loc, gisdbase.name)))
     return res
Esempio n. 13
0
def reproject(igisdbase,
              ilocation,
              olocation,
              mset_pat,
              rast_pat,
              datefmt="%Y%m%d_%H",
              mapset_fmt="r%Y_%m",
              raster_fmt="T{elev:03d}m_%Y%m%d_%H",
              nprocs=4,
              ogisdbase=None,
              **kwargs):
    env = os.environ.copy()
    ogisdbase = igisdbase if ogisdbase is None else ogisdbase
    mset_envs = {}
    mset_rasters = {}
    queue = ParallelModuleQueue(nprocs=nprocs)
    iloc = Location(location=ilocation, gisdbase=igisdbase)
    # oloc = Location(location=olocation, gisdbase=ogisdbase)
    #import ipdb; ipdb.set_trace()
    for imset_name in iloc.mapsets(pattern=mset_pat):
        for rname in iloc[imset_name].glist("raster", pattern=rast_pat):
            base, date, elev = extract_date(rname, datefmt=datefmt)
            rast_name = date.strftime(raster_fmt.format(elev=elev))
            mset_name = date.strftime(mapset_fmt)
            mset_path = os.path.join(ogisdbase, olocation, mset_name)
            if not os.path.exists(mset_path):
                gs.grass_create(gs.GRASSBIN, mset_path, create_opts="")
                try:
                    os.makedirs(os.path.join(mset_path, '.tmp'))
                    os.makedirs(
                        os.path.join(mset_path, '.tmp', socket.gethostname()))
                except:
                    # ignore error in creating the
                    pass
            try:
                menv = mset_envs[mset_name]
                rasters = mset_rasters[mset_name]
            except KeyError:
                menv = gs.grass_init(gs.GISBASE,
                                     ogisdbase,
                                     olocation,
                                     mset_name,
                                     env=env.copy())
                mset_envs[mset_name] = menv
                mset = Mapset(mset_name,
                              location=olocation,
                              gisdbase=ogisdbase)
                rasters = set(mset.glist("raster"))
                mset_rasters[mset_name] = rasters
                # set region for the mapset
                sregion = read_command("r.proj",
                                       location=ilocation,
                                       dbase=igisdbase,
                                       mapset=imset_name,
                                       input=rname,
                                       output=rast_name,
                                       flags="g",
                                       env=menv)
                #import ipdb; ipdb.set_trace()
                kregion = dict([tuple(s.split('=')) for s in sregion.split()])
                run_command("g.region",
                            save=mset_name,
                            env=menv,
                            overwrite=True,
                            **kregion)
                menv["WIND_OVERRIDE"] = mset_name

            if rast_name not in rasters:
                mod = Module("r.proj",
                             location=ilocation,
                             dbase=igisdbase,
                             mapset=imset_name,
                             input=rname,
                             output=rast_name,
                             run_=False,
                             **kwargs)
                mod.env_ = menv
                print(rast_name)
                #time.sleep(0.2) # sllep otherwise there is a problem in creating
                queue.put(mod)
    queue.wait()