Beispiel #1
0
 def __init__(self,
              cmd,
              width=None,
              height=None,
              overlap=0,
              processes=None,
              split=False,
              debug=False,
              region=None,
              move=None,
              log=False,
              start_row=0,
              start_col=0,
              out_prefix='',
              *args,
              **kargs):
     kargs['run_'] = False
     self.mset = Mapset()
     self.module = Module(cmd, *args, **kargs)
     self.width = width
     self.height = height
     self.overlap = overlap
     self.processes = processes
     self.region = region if region else Region()
     self.start_row = start_row
     self.start_col = start_col
     self.out_prefix = out_prefix
     self.log = log
     self.move = move
     self.gisrc_src = os.environ['GISRC']
     self.n_mset, self.gisrc_dst = None, None
     if self.move:
         self.n_mset = copy_mapset(self.mset, self.move)
         self.gisrc_dst = write_gisrc(self.n_mset.gisdbase,
                                      self.n_mset.location,
                                      self.n_mset.name)
         rasters = [r for r in select(self.module.inputs, 'raster')]
         if rasters:
             copy_rasters(rasters,
                          self.gisrc_src,
                          self.gisrc_dst,
                          region=self.region)
         vectors = [v for v in select(self.module.inputs, 'vector')]
         if vectors:
             copy_vectors(vectors, self.gisrc_src, self.gisrc_dst)
         groups = [g for g in select(self.module.inputs, 'group')]
         if groups:
             copy_groups(groups,
                         self.gisrc_src,
                         self.gisrc_dst,
                         region=self.region)
     self.bboxes = split_region_tiles(region=region,
                                      width=width,
                                      height=height,
                                      overlap=overlap)
     self.msetstr = cmd.replace('.', '') + "_%03d_%03d"
     self.inlist = None
     if split:
         self.split()
     self.debug = debug
Beispiel #2
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)
Beispiel #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())
     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)
Beispiel #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)
Beispiel #5
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,
            )
Beispiel #6
0
 def __init__(self, cmd, width=None, height=None, overlap=0, processes=None,
              split=False, debug=False, region=None, move=None, log=False,
              start_row=0, start_col=0, out_prefix='', mapset_prefix=None,
              *args, **kargs):
     kargs['run_'] = False
     self.mset = Mapset()
     self.module = Module(cmd, *args, **kargs)
     self.width = width
     self.height = height
     self.overlap = overlap
     self.processes = processes
     self.region = region if region else Region()
     self.start_row = start_row
     self.start_col = start_col
     self.out_prefix = out_prefix
     self.log = log
     self.move = move
     self.gisrc_src = os.environ['GISRC']
     self.n_mset, self.gisrc_dst = None, None
     if self.move:
         self.n_mset = copy_mapset(self.mset, self.move)
         self.gisrc_dst = write_gisrc(self.n_mset.gisdbase,
                                      self.n_mset.location,
                                      self.n_mset.name)
         rasters = [r for r in select(self.module.inputs, 'raster')]
         if rasters:
             copy_rasters(rasters, self.gisrc_src, self.gisrc_dst,
                          region=self.region)
         vectors = [v for v in select(self.module.inputs, 'vector')]
         if vectors:
             copy_vectors(vectors, self.gisrc_src, self.gisrc_dst)
         groups = [g for g in select(self.module.inputs, 'group')]
         if groups:
             copy_groups(groups, self.gisrc_src, self.gisrc_dst,
                         region=self.region)
     self.bboxes = split_region_tiles(region=region,
                                      width=width, height=height,
                                      overlap=overlap)
     if mapset_prefix:
         self.msetstr = mapset_prefix + "_%03d_%03d"
     else:
         self.msetstr = cmd.replace('.', '') + "_%03d_%03d"
     self.inlist = None
     if split:
         self.split()
     self.debug = debug
def node_patch(cmd, nwidth, nheight, out_regexp, overwrite=True):
    from grass.lib.gis import G_tempfile
    tmp, dummy = os.path.split(os.path.split(G_tempfile())[0])
    tmpdir = os.path.join(cmd)
    bboxes = split_region_tiles(width=nwidth, height=nheight)
    for out in os.listdir(tmpdir):
        outdir = os.path.join(tmpdir, out)
        rasts = os.listdir(outdir)
        rsts = row_order(rasts, bboxes)
        rst = RasterRow(re.findall(out_regexp, rasts[0])[0])
        rst.open('w', mtype=rsts[0][0].mtype, overwrite=overwrite)
        for rrst, rbbox in zip(rsts, bboxes):
            rpatch_row(rst, rrst, rbbox)

        for rrst in rsts:
            for r in rrst:
                r.close()

        rst.close()
Beispiel #8
0
from grass.pygrass.gis.region import Region
from grass.pygrass.modules.grid.grid import GridModule
from grass.pygrass.modules.grid import split
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.raster import RasterRow
import multiprocessing as mltp
from itertools import chain
import time
import numpy as np

reg = Region()

# profile reading region-based blocks
# testreg = GridModule('g.region', width=100, height=100, processes=4)
testreg = split.split_region_tiles(width=reg.cols, height=100)


def worker(src):
    window, src = src
    window = dict(window)
    window['n'] = window.pop('north')
    window['s'] = window.pop('south')
    window['e'] = window.pop('east')
    window['w'] = window.pop('west')
    del (window['top'])
    del (window['bottom'])

    g.region(**window)

    with RasterRow(src) as rs:
Beispiel #9
0
 def __init__(
     self,
     cmd,
     width=None,
     height=None,
     overlap=0,
     processes=None,
     split=False,
     debug=False,
     region=None,
     move=None,
     log=False,
     start_row=0,
     start_col=0,
     out_prefix="",
     mapset_prefix=None,
     *args,
     **kargs,
 ):
     kargs["run_"] = False
     self.mset = Mapset()
     self.module = Module(cmd, *args, **kargs)
     self.width = width
     self.height = height
     self.overlap = overlap
     self.processes = processes
     self.region = region if region else Region()
     self.start_row = start_row
     self.start_col = start_col
     self.out_prefix = out_prefix
     self.log = log
     self.move = move
     self.gisrc_src = os.environ["GISRC"]
     self.n_mset, self.gisrc_dst = None, None
     if self.move:
         self.n_mset = copy_mapset(self.mset, self.move)
         self.gisrc_dst = write_gisrc(self.n_mset.gisdbase,
                                      self.n_mset.location,
                                      self.n_mset.name)
         rasters = [r for r in select(self.module.inputs, "raster")]
         if rasters:
             copy_rasters(rasters,
                          self.gisrc_src,
                          self.gisrc_dst,
                          region=self.region)
         vectors = [v for v in select(self.module.inputs, "vector")]
         if vectors:
             copy_vectors(vectors, self.gisrc_src, self.gisrc_dst)
         groups = [g for g in select(self.module.inputs, "group")]
         if groups:
             copy_groups(groups,
                         self.gisrc_src,
                         self.gisrc_dst,
                         region=self.region)
     self.bboxes = split_region_tiles(region=region,
                                      width=width,
                                      height=height,
                                      overlap=overlap)
     if mapset_prefix:
         self.mapset_prefix = mapset_prefix
     else:
         self.mapset_prefix = append_node_pid("grid_" +
                                              legalize_vector_name(cmd))
     self.msetstr = self.mapset_prefix + "_%03d_%03d"
     self.inlist = None
     if split:
         self.split()
     self.debug = debug
Beispiel #10
0
 def __init__(
     self,
     cmd,
     width=None,
     height=None,
     overlap=0,
     processes=None,
     split=False,
     debug=False,
     region=None,
     move=None,
     log=False,
     start_row=0,
     start_col=0,
     out_prefix="",
     mapset_prefix=None,
     patch_backend=None,
     *args,
     **kargs,
 ):
     kargs["run_"] = False
     self.mset = Mapset()
     self.module = Module(cmd, *args, **kargs)
     self.width = width
     self.height = height
     self.overlap = overlap
     self.processes = processes
     self.region = region if region else Region()
     self.start_row = start_row
     self.start_col = start_col
     self.out_prefix = out_prefix
     self.log = log
     self.move = move
     # by default RasterRow is used as previously
     # if overlap > 0, r.patch won't work properly
     if not patch_backend:
         self.patch_backend = "RasterRow"
     elif patch_backend not in ("r.patch", "RasterRow"):
         raise RuntimeError(
             _("Parameter patch_backend must be 'r.patch' or 'RasterRow'"))
     elif patch_backend == "r.patch" and self.overlap:
         raise RuntimeError(
             _("Patching backend 'r.patch' doesn't work for overlap > 0"))
     else:
         self.patch_backend = patch_backend
     self.gisrc_src = os.environ["GISRC"]
     self.n_mset, self.gisrc_dst = None, None
     self.estimate_tile_size()
     if self.move:
         self.n_mset = copy_mapset(self.mset, self.move)
         self.gisrc_dst = write_gisrc(self.n_mset.gisdbase,
                                      self.n_mset.location,
                                      self.n_mset.name)
         rasters = [r for r in select(self.module.inputs, "raster")]
         if rasters:
             copy_rasters(rasters,
                          self.gisrc_src,
                          self.gisrc_dst,
                          region=self.region)
         vectors = [v for v in select(self.module.inputs, "vector")]
         if vectors:
             copy_vectors(vectors, self.gisrc_src, self.gisrc_dst)
         groups = [g for g in select(self.module.inputs, "group")]
         if groups:
             copy_groups(groups,
                         self.gisrc_src,
                         self.gisrc_dst,
                         region=self.region)
     self.bboxes = split_region_tiles(region=region,
                                      width=self.width,
                                      height=self.height,
                                      overlap=overlap)
     if mapset_prefix:
         self.mapset_prefix = mapset_prefix
     else:
         self.mapset_prefix = append_node_pid("grid_" +
                                              legalize_vector_name(cmd))
     self.msetstr = self.mapset_prefix + "_%03d_%03d"
     self.inlist = None
     if split:
         self.split()
     self.debug = debug