Пример #1
0
def refine(L, input, region, method='bilinear'):
    """change resolution back to base resolution and resample a raster"""
    
    input_padded = '_'.join(['tmp', input, '_padded']) + \
        ''.join([random.choice(string.ascii_letters + string.digits) for n in range(4)])
    TMP_RAST[L].append(input_padded)
    cell_padding(input=input, output=input_padded, radius=2)
    
    Region.write(region)
    input = '_'.join(['tmp', input, 'refined_base_resolution']) + \
        ''.join([random.choice(string.ascii_letters + string.digits) for n in range(4)])
    TMP_RAST[L].append(input)

    if method == 'bilinear':
       r.resamp_interp(input=input_padded, 
                       output=input,
                       method="bilinear")

    if method == 'average':
        r.resamp_stats(input=input_padded,
                       output=input,
                       method='average',
                       flags='w')
    
    return input
Пример #2
0
    def set_region_from_rast(self, rastname='', mapset=''):
        """Set the computational region from a map,
           if rastername and mapset is not specify, use itself.
           This region will be used by all
           raster map layers that are opened in the same process.

           The GRASS region settings will not be modified.

           call C function `Rast_get_cellhd`, `Rast_set_window`

           return: The region that was used to set the computational region

           """
        if self.is_open():
            fatal("You cannot change the region if map is open")
            raise
        region = Region()
        if rastname == '':
            rastname = self.name
        if mapset == '':
            mapset = self.mapset

        libraster.Rast_get_cellhd(rastname, mapset,
                                  region.byref())
        self._set_raster_window(region)
        return region
Пример #3
0
    def test_resampling_2(self):

        region = Region()

        region.ewres = 5
        region.nsres = 5
        region.north = 60
        region.south = -20
        region.east = 60
        region.west = -20
        region.adjust(rows=True, cols=True)

        rast = RasterRow(self.name)
        rast.set_region(region)
        rast.open(mode='r')
        """
        [nan, nan, nan, nan, nan, nan, nan, nan]
        [nan, nan, nan, nan, nan, nan, nan, nan]
        [nan, nan, 11.0, 21.0, 31.0, 41.0, nan, nan]
        [nan, nan, 12.0, 22.0, 32.0, 42.0, nan, nan]
        [nan, nan, 13.0, 23.0, 33.0, 43.0, nan, nan]
        [nan, nan, 14.0, 24.0, 34.0, 44.0, nan, nan]
        [nan, nan, nan, nan, nan, nan, nan, nan]
        [nan, nan, nan, nan, nan, nan, nan, nan]
        """

        six.assertCountEqual(self, rast[2].tolist()[2:6], [11., 21., 31., 41.])
        six.assertCountEqual(self, rast[5].tolist()[2:6], [14., 24., 34., 44.])

        rast.close()
Пример #4
0
def refine(L, input, region, method='bilinear'):
    """change resolution back to base resolution and resample a raster"""
    
    input_padded = '_'.join(['tmp', input, '_padded']) + \
        ''.join([random.choice(string.ascii_letters + string.digits) for n in range(4)])
    TMP_RAST[L].append(input_padded)
    cell_padding(input=input, output=input_padded, radius=2)
    
    Region.write(region)
    input = '_'.join(['tmp', input, 'refined_base_resolution']) + \
        ''.join([random.choice(string.ascii_letters + string.digits) for n in range(4)])
    TMP_RAST[L].append(input)

    if method == 'bilinear':
       r.resamp_interp(input=input_padded, 
                       output=input,
                       method="bilinear")

    if method == 'average':
        r.resamp_stats(input=input_padded,
                       output=input,
                       method='average',
                       flags='w')
    
    return input
Пример #5
0
 def test_bounds(self):
     reg1 = Region()
     reg2 = Region()
     self.assertTrue(reg1, reg2)
     north = reg2.north
     reg2.north = 0
     self.assertNotEqual(reg1.north, reg2.north)
     reg2.north = north
Пример #6
0
 def test_bounds(self):
     reg1 = Region()
     reg2 = Region()
     self.assertTrue(reg1, reg2)
     north = reg2.north
     reg2.north = 0
     self.assertNotEqual(reg1.north, reg2.north)
     reg2.north = north
Пример #7
0
    def test_resampling_to_numpy_img_4(self):

        region = Region()
        region.ewres = 0.1
        region.nsres = 0.1
        region.adjust(rows=True, cols=True)

        a = raster2numpy_img(self.name, region, color="GRAY2")

        self.assertEqual(len(a), region.rows * region.cols*1)
Пример #8
0
    def test_resampling_to_numpy_img_1(self):

        region = Region()
        region.ewres = 10
        region.nsres = 10
        region.adjust(rows=True, cols=True)

        a = raster2numpy_img(self.name, region)

        self.assertEqual(len(a), region.rows * region.cols*4)
Пример #9
0
def cleanup():
    grass.message("Deleting intermediate files...")

    for k, v in TMP_RAST.items():
        for f in v:
            if len(grass.find_file(f)['fullname']) > 0:
                grass.run_command(
                    "g.remove", type="raster", name=f, flags="f", quiet=True)
    
    Region.write(current_region)
Пример #10
0
def cleanup():
    grass.message("Deleting intermediate files...")

    for k, v in TMP_RAST.iteritems():
        for f in v:
            if len(grass.find_file(f)['fullname']) > 0:
                grass.run_command(
                    "g.remove", type="raster", name=f, flags="f", quiet=True)
    
    Region.write(current_region)
Пример #11
0
    def test_resampling_2(self):
        
        region = Region()
        
        region.ewres = 5
        region.nsres = 5
        region.north = 60
        region.south = -20
        region.east = 60
        region.west = -20
        region.adjust(rows=True, cols=True)
        
        rast = RasterRow(self.name)
        rast.set_region(region)
        rast.open(mode='r')
        
        """
        [nan, nan, nan, nan, nan, nan, nan, nan]
        [nan, nan, nan, nan, nan, nan, nan, nan]
        [nan, nan, 11.0, 21.0, 31.0, 41.0, nan, nan]
        [nan, nan, 12.0, 22.0, 32.0, 42.0, nan, nan]
        [nan, nan, 13.0, 23.0, 33.0, 43.0, nan, nan]
        [nan, nan, 14.0, 24.0, 34.0, 44.0, nan, nan]
        [nan, nan, nan, nan, nan, nan, nan, nan]
        [nan, nan, nan, nan, nan, nan, nan, nan]
        """

        self.assertItemsEqual(rast[2].tolist()[2:6], [11.,21.,31.,41.])        
        self.assertItemsEqual(rast[5].tolist()[2:6], [14.,24.,34.,44.])
        
        rast.close()
Пример #12
0
def main():

    elevation = sys.argv[1]
    nightlights = sys.argv[2]
    photopollution = sys.argv[3]
    max_distance = float(sys.argv[4])

    # Overwrite and verbose parameters
    #os.environ["GRASS_OVERWRITE"] = "1"
    os.environ['GRASS_VERBOSE'] = "-1"

    reg = Region()  #get current region, use it for cell iteration
    if Region().nsres != Region().ewres:
        print "ewres is different from nsres"
        raise SystemExit

    # open dem
    dem = raster.RasterRow(elevation)
    if dem.is_open():
        dem.close()
    if not dem.is_open():
        dem.open()

    # open night time lights
    lights = raster.RasterRow(nightlights)

    if lights.is_open():
        lights.close()

    if not lights.is_open():
        lights.open()

    script.use_temp_region(
    )  #temporary region in case of parallel processing, maybe useless for current project

    # expand region according to viewshed max distance  (align to dem)
    script.run_command('g.region',
                       n=reg.north + max_distance,
                       s=reg.south - max_distance,
                       e=reg.east + max_distance,
                       w=reg.west - max_distance,
                       align=elevation)

    # resample lights raster
    script.run_command("r.resample",
                       input=nightlights,
                       output=tmp_res_lights,
                       overwrite=True)

    script.del_temp_region()

    calcPhotoPolRast(dem, max_distance, reg, elevation, lights, photopollution)
Пример #13
0
 def setRegion(parcelmap,betriebid):
     ## set region to parcel layer extent + buffer
     reg = Region()
     reg.vect(parcelmap.name)
     regbuffer = 100
     reg.north += regbuffer
     reg.east += regbuffer
     reg.south -= regbuffer
     reg.west -= regbuffer
     reg.set_current()
     # set_current() not working right now
     # so using g.region() :
     g.region(n=str(reg.north), s=str(reg.south), w=str(reg.west), e=str(reg.east), res='2', flags='a',quiet=quiet)
     g.region(save='B'+betriebid,overwrite=True,quiet=quiet)
Пример #14
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
Пример #15
0
def find_segments(river, discharge, dtm, range_plant, distance, p_max):
    check_multilines(river)
    #pdb.set_trace()
    river, mset = river.split('@') if '@' in river else (river, '')
    vec = VectorTopo(river, mapset=mset, mode='r')
    vec.open("r")
    raster_q = RasterRow(discharge)
    raster_dtm = RasterRow(dtm)
    raster_q.open('r')
    raster_dtm.open('r')
    reg = Region()
    plants = []
    for line in vec:
        count = 0
        # args is prog, h,  q
        line, prog, h, q = build_array(line, raster_q, raster_dtm)
        #pdb.set_trace()
        if len(line) > 2:
            #            import ipdb; ipdb.set_trace()
            #        else:
            # import pdb; pdb.set_trace()
            plants = recursive_plant(
                (prog, h, q), range_plant, distance, prog[0], prog[-1],
                str(line.cat), line.cat, line, plants, count, p_max)
    #pdb.set_trace()
    vec.close()
    raster_q.close()
    raster_dtm.close()
    return plants
Пример #16
0
def filter(method, names, winsize, order, prefix, itercount, fit_up):

    current_mapset = grass.read_command('g.mapset', flags='p')
    current_mapset = current_mapset.strip()

    inputs = init_rasters(names)
    output_names = [prefix + name for name in names]
    outputs = init_rasters(output_names, mapset=current_mapset)
    try:
        open_rasters(outputs, write=True)
        open_rasters(inputs)

        reg = Region()
        for i in range(reg.rows):
            # import ipdb; ipdb.set_trace()
            row_data = np.array([_get_row_or_nan(r, i) for r in inputs])
            filtered_rows = _filter(method, row_data, winsize, order,
                                    itercount, fit_up)
            for map_num in range(len(outputs)):
                map = outputs[map_num]
                row = filtered_rows[map_num, :]
                buf = Buffer(row.shape, map.mtype, row)
                map.put_row(i, buf)
    finally:
        close_rasters(outputs)
        close_rasters(inputs)
Пример #17
0
def write_plants(plants, output, efficiency, min_power):
    # create vetor segment
    new_vec = VectorTopo(output)
    #TODO:  check if the vector already exists
    new_vec.layer = 1
    new_vec.open('w', tab_cols=COLS)
    reg = Region()
    for pla in plants:
        power = pla.potential_power(efficiency=efficiency)
        if power > min_power:
            for cat, ink in enumerate(pla.intakes):
                if version == 70:
                    new_vec.write(
                        pla.line,
                        (pla.id, pla.id_stream, power,
                         float(pla.restitution.discharge), float(
                             ink.elevation), float(pla.restitution.elevation)))
                else:
                    new_vec.write(pla.line,
                                  cat=cat,
                                  attrs=(pla.id, pla.id_stream, power,
                                         float(pla.restitution.discharge),
                                         float(ink.elevation),
                                         float(pla.restitution.elevation)))

    new_vec.table.conn.commit()
    new_vec.comment = (' '.join(sys.argv))
    #pdb.set_trace()
    new_vec.close()
Пример #18
0
    def to_pandas(self):
        """RasterStack to pandas DataFrame

        Returns
        -------
        pandas.DataFrame
        """

        reg = Region()
        arr = self.read()

        # generate x and y grid coordinate arrays
        x_range = np.linspace(start=reg.west, stop=reg.east, num=reg.cols)
        y_range = np.linspace(start=reg.south, stop=reg.north, num=reg.rows)
        xs, ys = np.meshgrid(x_range, y_range)

        # flatten 3d data into 2d array (layer, sample)
        arr = arr.reshape((arr.shape[0], arr.shape[1] * arr.shape[2]))
        arr = arr.transpose()

        # convert to dataframe
        df = pd.DataFrame(
            np.column_stack((xs.flatten(), ys.flatten(), arr)),
            columns=["x", "y"] + self.names,
        )

        # set nodata values to nan
        for i, col_name in enumerate(self.names):
            df.loc[df[col_name] == self._cell_nodata, col_name] = np.nan

        return df
Пример #19
0
 def get_works(self):
     """Return a list of tuble with the parameters for cmd_exe function"""
     works = []
     reg = Region()
     if self.move:
         mdst, ldst, gdst = read_gisrc(self.gisrc_dst)
     else:
         ldst, gdst = self.mset.location, self.mset.gisdbase
     cmd = self.module.get_dict()
     groups = [g for g in select(self.module.inputs, 'group')]
     for row, box_row in enumerate(self.bboxes):
         for col, box in enumerate(box_row):
             inms = None
             if self.inlist:
                 inms = {}
                 cols = len(box_row)
                 for key in self.inlist:
                     indx = row * cols + col
                     inms[key] = "%s@%s" % (self.inlist[key][indx],
                                            self.mset.name)
             # set the computational region, prepare the region parameters
             bbox = dict([(k[0], str(v)) for k, v in box.items()[:-2]])
             bbox['nsres'] = '%f' % reg.nsres
             bbox['ewres'] = '%f' % reg.ewres
             new_mset = self.msetstr % (self.start_row + row,
                                        self.start_col + col),
             works.append((bbox, inms, self.gisrc_src,
                           write_gisrc(gdst, ldst, new_mset), cmd, groups))
     return works
Пример #20
0
    def __init__(self, options1, options2, scale, offset='0,0'):
        """ Return
        """
        self.opts1 = dict(
            zip(['mark_lon', 'mark_x_dist', 'mark_y_dist'],
                [int(opt) for opt in options1.split(',')]))

        self.opts2 = dict(
            zip(['rows', 'dist_axis_x', 'dist_axis_y'],
                [int(opt) for opt in options2.split(',')]))

        self.scale = float(scale)

        self.cols = 0
        reg = Region()
        self.zero_x = int(reg.west) + int(offset.split(',')[0])
        self.zero_y = int(reg.south) + int(offset.split(',')[1])

        self.mat_trans = []

        self.rows_height = []
        self.cols_width_left = [0]
        self.cols_width_right = [0]

        self.centers = []
Пример #21
0
    def __init__(self, options1, options2, scale, offset="0,0"):
        """Return"""
        self.opts1 = dict(
            zip(
                ["mark_lon", "mark_x_dist", "mark_y_dist"],
                [int(opt) for opt in options1.split(",")],
            ))

        self.opts2 = dict(
            zip(
                ["rows", "dist_axis_x", "dist_axis_y"],
                [int(opt) for opt in options2.split(",")],
            ))

        self.scale = float(scale)

        self.cols = 0
        reg = Region()
        self.zero_x = int(reg.west) + int(offset.split(",")[0])
        self.zero_y = int(reg.south) + int(offset.split(",")[1])

        self.mat_trans = []

        self.rows_height = []
        self.cols_width_left = [0]
        self.cols_width_right = [0]

        self.centers = []
Пример #22
0
    def __init__(self, start_time, end_time, dtype, mkeys):
        assert isinstance(start_time, datetime), \
            "start_time not a datetime object!"
        assert isinstance(end_time, datetime), \
            "end_time not a datetime object!"
        assert start_time <= end_time, "start_time > end_time!"

        self.start_time = start_time
        self.end_time = end_time
        self.dtype = dtype
        self.region = Region()
        self.xr = self.region.cols
        self.yr = self.region.rows
        # Check if region is at least 3x3
        if self.xr < 3 or self.yr < 3:
            msgr.fatal(u"GRASS Region should be at least 3 cells by 3 cells")

        self.dx = self.region.ewres
        self.dy = self.region.nsres
        self.reg_bbox = {
            'e': self.region.east,
            'w': self.region.west,
            'n': self.region.north,
            's': self.region.south
        }
        self.overwrite = gscript.overwrite()
        self.mapset = gutils.getenv('MAPSET')
        self.maps = dict.fromkeys(mkeys)
        # init temporal module
        tgis.init()

        assert os.path.isfile(self.rules_h)
        assert os.path.isfile(self.rules_v)
        assert os.path.isfile(self.rules_def)
Пример #23
0
def cell_padding(input, output, radius=3):
    """Mitigates edge effect by growing an input raster map by radius cells

    Args
    ----
    input, output : str
        Names of GRASS raster map for input, and padded output
    radius : int
        Radius in which to expand region and grow raster
    
    Returns
    -------
    input_grown : str
        GRASS raster map which has been expanded by radius cells"""
    
    region = Region()

    g.region(n=region.north + (region.nsres * radius),
             s=region.south - (region.nsres * radius),
             w=region.west - (region.ewres * radius),
             e=region.east + (region.ewres * radius))

    r.grow(input=input,
           output=output,
           radius=radius+1,
           quiet=True)

    return (region)
Пример #24
0
 def __new__(cls,
             name,
             mapset="",
             mtype='CELL',
             mode='r+',
             overwrite=False):
     reg = Region()
     shape = (reg.rows, reg.cols)
     mapset = libgis.G_find_raster(name, mapset)
     gtype = None
     if mapset:
         # map exist, set the map type
         gtype = libraster.Rast_map_type(name, mapset)
         mtype = RTYPE_STR[gtype]
     filename = grasscore.tempfile()
     obj = np.memmap.__new__(cls,
                             filename=filename,
                             dtype=RTYPE[mtype]['numpy'],
                             mode=mode,
                             shape=shape)
     obj.mtype = mtype.upper()
     obj.gtype = gtype if gtype else RTYPE[mtype]['grass type']
     obj._rows = reg.rows
     obj._cols = reg.cols
     obj.filename = filename
     obj._name = name
     obj.mapset = mapset
     obj.reg = reg
     obj.overwrite = overwrite
     return obj
Пример #25
0
def _kappa_skll(map1, map2, lowmem, method):
    import sklearn
    raster1 = RasterRow(map1)
    raster2 = RasterRow(map2)
    raster1.open('r')
    raster2.open('r')
    if lowmem is False:
        array1 = np.array(raster1).reshape(-1)
        array2 = np.array(raster2).reshape(-1)
    else:
        import tempfile
        current = Region()
        array1 = np.memmap(tempfile.NamedTemporaryFile(),
                           dtype='float32', mode='w+',
                           shape=(current.rows, current.cols))
        array1[:] = np.array(raster1).reshape(-1)
        array2 = np.memmap(tempfile.NamedTemporaryFile(),
                           dtype='float32', mode='w+',
                           shape=(current.rows, current.cols))
        array2[:] = np.array(raster2).reshape(-1)
    raster1.close()
    raster2.close()
    if sklearn.__version__ >= '0.18':
        return sklearn.metrics.cohen_kappa_score(array1, array2,
                                                 weights=method)
    else:
        return sklearn.metrics.cohen_kappa_score(array1, array2)
Пример #26
0
def conv_segpoints(seg, output):

    segments, mset = (seg.split('@') if '@' in seg else (seg, ''))
    # convert the map with segments in a map with intakes and restitution
    new_vec = VectorTopo(output)
    #TODO:  check if the vector already exists
    new_vec.layer = 1
    new_vec.open('w', tab_cols=COLS_points)
    reg = Region()

    seg = VectorTopo(segments, mapset=mset)
    seg.layer = 1
    seg.open('r')

    for pla in seg:
        #import ipdb; ipdb.set_trace()
        new_vec.write(pla[-1],
                      (2, pla.attrs['plant_id'], 'restitution',
                       pla.attrs['stream_id'], pla.attrs['elev_down'],
                       pla.attrs['discharge'], pla.attrs['pot_power']))
        #import ipdb; ipdb.set_trace()
        new_vec.write(pla[0], (1, pla.attrs['plant_id'], 'intake',
                               pla.attrs['stream_id'], pla.attrs['elev_up'],
                               pla.attrs['discharge'], pla.attrs['pot_power']))

    new_vec.table.conn.commit()
    new_vec.comment = (' '.join(sys.argv))
    new_vec.write_header()
    #pdb.set_trace()
    new_vec.close()

    return new_vec
Пример #27
0
def write_points(plants, output, efficiency, min_power):
    # create vetor segment
    new_vec = VectorTopo(output)
    #TODO:  check if the vector already exists
    new_vec.layer = 1
    new_vec.open('w', tab_cols=COLS_points)
    reg = Region()

    # import ipdb; ipdb.set_trace()
    for pla in plants:
        power = pla.potential_power(efficiency=efficiency)
        if power > min_power:
            new_vec.write(pla.line[-1],
                          (pla.restitution.id, pla.id, 'restitution',
                           pla.id_stream, float(pla.restitution.elevation),
                           float(pla.restitution.discharge), power))
            for ink in pla.intakes:
                new_vec.write(
                    pla.line[0],
                    (ink.id, pla.id, 'intake', pla.id_stream,
                     float(ink.elevation), float(ink.discharge), power))

    new_vec.table.conn.commit()
    new_vec.comment = (' '.join(sys.argv))
    new_vec.write_header()
    #pdb.set_trace()
    new_vec.close()
Пример #28
0
def get_raster_for_points(poi_vector, raster, column=None, region=None):
    """Query a raster map for each point feature of a vector

    Example

    >>> from grass.pygrass.vector import VectorTopo
    >>> from grass.pygrass.raster import RasterRow
    >>> ele = RasterRow('elevation')
    >>> copy('schools','myschools','vect')
    >>> sch = VectorTopo('myschools')
    >>> sch.open(mode='r')
    >>> get_raster_for_points(sch, ele)               # doctest: +ELLIPSIS
    [(1, 633649.2856743174, 221412.94434781274, 145.06602)...
    >>> sch.table.columns.add('elevation','double precision')
    >>> 'elevation' in sch.table.columns
    True
    >>> get_raster_for_points(sch, ele, 'elevation')
    True
    >>> sch.table.filters.select('NAMESHORT','elevation')
    Filters(u'SELECT NAMESHORT, elevation FROM myschools;')
    >>> cur = sch.table.execute()
    >>> cur.fetchall()                                # doctest: +ELLIPSIS
    [(u'SWIFT CREEK', 145.06602), ... (u'9TH GRADE CTR', None)]
    >>> remove('myschools','vect')


    :param point: point vector object
    :param raster: raster object
    :param str column: column name to update

    """
    from math import isnan
    if not column:
        result = []
    if region is None:
        from grass.pygrass.gis.region import Region
        region = Region()
    if not poi_vector.is_open():
        poi_vector.open()
    if not raster.is_open():
        raster.open()
    if poi_vector.num_primitive_of('point') == 0:
        raise GrassError(_("Vector doesn't contain points"))
    for poi in poi_vector.viter('points'):
        val = raster.get_value(poi, region)
        if column:
            if val is not None and not isnan(val):
                poi.attrs[column] = val
        else:
            if val is not None and not isnan(val):
                result.append((poi.id, poi.x, poi.y, val))
            else:
                result.append((poi.id, poi.x, poi.y, None))
    if not column:
        return result
    else:
        poi.attrs.commit()
        return True
Пример #29
0
def get_electro_length(opts):
    # open vector plant
    pname = opts["struct"]
    pname, vmapset = pname.split("@") if "@" in pname else (pname, "")
    with VectorTopo(pname,
                    mapset=vmapset,
                    layer=int(opts["struct_layer"]),
                    mode="r") as vect:
        kcol = opts["struct_column_kind"]
        ktype = opts["struct_kind_turbine"]
        # check if electro_length it is alredy in the table
        if "electro_length" not in vect.table.columns:
            vect.table.columns.add("electro_length", "double precision")
        # open vector map with the existing electroline
        ename = opts["electro"]
        ename, emapset = ename.split("@") if "@" in ename else (ename, "")
        ltemp = []
        with VectorTopo(ename,
                        mapset=emapset,
                        layer=int(opts["electro_layer"]),
                        mode="r") as electro:
            pid = os.getpid()
            elines = opts["elines"] if opts["elines"] else (
                "tmprgreen_%i_elines" % pid)
            for cat, line in enumerate(vect):
                if line.attrs[kcol] == ktype:
                    # the turbine is the last point of the penstock
                    turbine = line[-1]
                    # find the closest electro line
                    eline = electro.find["by_point"].geo(turbine, maxdist=1e6)
                    dist = eline.distance(turbine)
                    line.attrs["electro_length"] = dist.dist
                    if line.attrs["side"] == "option1":
                        ltemp.append([
                            geo.Line([turbine, dist.point]),
                            (line.attrs["plant_id"], line.attrs["side"]),
                        ])
                else:
                    line.attrs["electro_length"] = 0.0
            vect.table.conn.commit()
        new = VectorTopo(elines)  # new vec with elines
        new.layer = 1
        cols = [
            (u"cat", "INTEGER PRIMARY KEY"),
            (u"plant_id", "VARCHAR(10)"),
            (u"side", "VARCHAR(10)"),
        ]
        new.open("w", tab_cols=cols)
        reg = Region()
        for cat, line in enumerate(ltemp):
            if version == 70:
                new.write(line[0], line[1])
            else:
                new.write(line[0], cat=cat, attrs=line[1])
        new.table.conn.commit()
        new.comment = " ".join(sys.argv)
        new.close()
Пример #30
0
def generate_coordinates(number, bbox=None, with_z=False):
    """Return 2 or 3 random arrays of coordinates"""
    bbox = Region() if bbox is None else bbox
    x = bbox.south + (bbox.north - bbox.south) * np.random.random(number)
    y = bbox.west + (bbox.east - bbox.west) * np.random.random(number)
    if with_z:
        z = np.random.random(number) * 1000
        return x, y, z
    return x, y
Пример #31
0
def get_electro_length(opts):
    # open vector plant
    pname = opts['struct']
    pname, vmapset = pname.split('@') if '@' in pname else (pname, '')
    with VectorTopo(pname,
                    mapset=vmapset,
                    layer=int(opts['struct_layer']),
                    mode='r') as vect:
        kcol = opts['struct_column_kind']
        ktype = opts['struct_kind_turbine']
        # check if electro_length it is alredy in the table
        if 'electro_length' not in vect.table.columns:
            vect.table.columns.add('electro_length', 'double precision')
        # open vector map with the existing electroline
        ename = opts['electro']
        ename, emapset = ename.split('@') if '@' in ename else (ename, '')
        ltemp = []
        with VectorTopo(ename,
                        mapset=emapset,
                        layer=int(opts['electro_layer']),
                        mode='r') as electro:
            pid = os.getpid()
            elines = (opts['elines'] if opts['elines'] else
                      ('tmprgreen_%i_elines' % pid))
            for cat, line in enumerate(vect):
                if line.attrs[kcol] == ktype:
                    # the turbine is the last point of the penstock
                    turbine = line[-1]
                    # find the closest electro line
                    eline = electro.find['by_point'].geo(turbine, maxdist=1e6)
                    dist = eline.distance(turbine)
                    line.attrs['electro_length'] = dist.dist
                    if line.attrs['side'] == 'option1':
                        ltemp.append([
                            geo.Line([turbine, dist.point]),
                            (line.attrs['plant_id'], line.attrs['side'])
                        ])
                else:
                    line.attrs['electro_length'] = 0.
            vect.table.conn.commit()
        new = VectorTopo(elines)  # new vec with elines
        new.layer = 1
        cols = [
            (u'cat', 'INTEGER PRIMARY KEY'),
            (u'plant_id', 'VARCHAR(10)'),
            (u'side', 'VARCHAR(10)'),
        ]
        new.open('w', tab_cols=cols)
        reg = Region()
        for cat, line in enumerate(ltemp):
            if version == 70:
                new.write(line[0], line[1])
            else:
                new.write(line[0], cat=cat, attrs=line[1])
        new.table.conn.commit()
        new.comment = (' '.join(sys.argv))
        new.close()
Пример #32
0
def random_map(mapname, mtype, overwrite=True, factor=100):
    region = Region()
    random_map = RasterRow(mapname)
    random_map.open('w', mtype, overwrite)
    for _ in range(region.rows):
        row_buf = Buffer((region.cols, ), mtype,
                         buffer=(np.random.random(region.cols,) * factor).data)
        random_map.put_row(row_buf)
    random_map.close()
    return random_map
Пример #33
0
 def test_resampling_1(self):
     
     region = Region()
     
     region.ewres = 4
     region.nsres = 4
     region.north = 30
     region.south = 10
     region.east = 30
     region.west = 10
     region.adjust(rows=True, cols=True)
     
     rast = RasterRow(self.name)
     rast.set_region(region)
     rast.open(mode='r')
     
     self.assertItemsEqual(rast[0].tolist(), [22, 22, 22, 22, 22, 32, 32, 32, 32, 32])
     self.assertItemsEqual(rast[5].tolist(), [23, 23, 23, 23, 23, 33, 33, 33, 33, 33])
     
     rast.close()
Пример #34
0
    def get_value(self, point, region=None):
        """This method returns the pixel value of a given pair of coordinates:

        :param point: pair of coordinates in tuple object or class object with coords() method
        :param region: The region that should be used for sampling,
                       default is the current computational region that can be set with set_region()
                       or set_region_from_rast()
        """
        # Check for tuple
        if type(point) != type([]) and type(point) != type(()):
            point = point.coords()
        # If no region was set, use the current computational region
        if not region:
            region = Region()
            libraster.Rast_get_window(region.byref())
        row, col = utils.coor2pixel(point, region)
        if col < 0 or col > region.cols or row < 0 or row > region.rows:
            return None
        line = self.get_row(int(row))
        return line[int(col)]
Пример #35
0
    def __init__(self, parent, giface):
        wx.Frame.__init__(self,
                          parent,
                          id=wx.ID_ANY,
                          title=_("GRASS GIS Temporal Plot Tool"))

        tgis.init(True)
        self._giface = giface
        self.datasetsV = None
        self.datasetsR = None
        # self.vectorDraw=False
        # self.rasterDraw=False
        self.init()
        self._layout()

        # We create a database interface here to speedup the GUI
        self.dbif = tgis.SQLDatabaseInterfaceConnection()
        self.dbif.connect()
        self.Bind(wx.EVT_CLOSE, self.onClose)
        self.region = Region()
Пример #36
0
def numpy2raster(array, mtype, name):
    """Save a numpy array to a raster map"""
    reg = Region()
    if (reg.rows, reg.cols) != array.shape:
        msg = "Region and array are different: %r != %r"
        raise TypeError(msg % ((reg.rows, reg.cols), array.shape))
    with RasterRow(name, mode="w", mtype=mtype) as new:
        newrow = Buffer((array.shape[1],), mtype=mtype)
        for row in array:
            newrow[:] = row[:]
            new.put_row(newrow)
Пример #37
0
    def predict_proba(self,
                      estimator,
                      output,
                      class_labels=None,
                      height=None,
                      overwrite=False):
        """Prediction method for RasterStack class

        Parameters
        ----------
        estimator : estimator object implementing 'fit'
            The object to use to fit the data

        output : str
            Output name for prediction raster

        class_labels : ndarray (opt)
            1d array containing indices of class labels to subset the
            prediction by. Only probability outputs for these classes will be
            produced.

        height : int (opt)
            Number of raster rows to pass to estimator at one time. If not
            specified then the entire raster is read into memory.

        overwrite : bool (opt). Default is False
            Option to overwrite an existing raster(s)

        Returns
        -------
        RasterStack
        """
        reg = Region()
        func = self._prob_fun

        # use class labels if supplied else output preds as 0,1,2...n
        if class_labels is None:
            test_window = list(self.row_windows(height=1))[0]
            img = self.read(rows=test_window)
            result = func(img, estimator)
            class_labels = range(result.shape[0])

        # only output positive class if result is binary
        if len(class_labels) == 2:
            class_labels, indexes = [max(class_labels)], [1]
        else:
            indexes = np.arange(0, len(class_labels), 1)

        # create and open rasters for writing
        result_stack = self._predict_multi(estimator, reg, indexes,
                                           class_labels, height, func, output,
                                           overwrite)

        return result_stack
Пример #38
0
    def set_region_from_rast(self, rastname='', mapset=''):
        """Set the computational region from a map,
           if rastername and mapset is not specify, use itself.
           This region will be used by all
           raster map layers that are opened in the same process.

           The GRASS region settings will not be modified.

           call C function `Rast_get_cellhd`, `Rast_set_window`

           """
        if self.is_open():
            fatal("You cannot change the region if map is open")
            raise
        region = Region()
        if rastname == '':
            rastname = self.name
        if mapset == '':
            mapset = self.mapset

        libraster.Rast_get_cellhd(rastname, mapset,
                                  region.byref())
        self._set_raster_window(region)
Пример #39
0
 def test_resampling_1(self):
     
     region = Region()
     
     region.ewres = 4
     region.nsres = 4
     region.north = 30
     region.south = 10
     region.east = 30
     region.west = 10
     region.adjust(rows=True, cols=True)
     
     rast = RasterRow(self.name)
     rast.set_region(region)
     rast.open(mode='r')
     
     self.assertItemsEqual(rast[0].tolist(), [22,22,22,22,22,32,32,32,32,32])        
     self.assertItemsEqual(rast[5].tolist(), [23,23,23,23,23,33,33,33,33,33])
     
     rast.close()
Пример #40
0
    def __init__(self, parent, giface):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY,
                          title=_("GRASS GIS Temporal Plot Tool"))

        tgis.init(True)
        self._giface = giface
        self.datasetsV = None
        self.datasetsR = None
        # self.vectorDraw=False
        # self.rasterDraw=False
        self.init()
        self._layout()

        # We create a database interface here to speedup the GUI
        self.dbif = tgis.SQLDatabaseInterfaceConnection()
        self.dbif.connect()
        self.Bind(wx.EVT_CLOSE, self.onClose)
        self.region = Region()
Пример #41
0
    def test_resampling_to_QImg_1(self):

        region = Region()
        region.from_rast(self.name)
        region.cols = 320
        region.rows = 240
        region.adjust()

        tmpfile = tempfile(False)
        tmpfile = tmpfile + ".png"

        a = raster2numpy_img(self.name, region)

        image = QImage(a.data, region.cols, region.rows,
                       QImage.Format_ARGB32)
        #image.save("data/a.png")
        image.save(tmpfile)
        self.assertFilesEqualMd5(tmpfile, "data/a.png")
Пример #42
0
    def test_resampling_to_QImg_4(self):

        region = Region()
        region.from_rast(self.name)
        region.cols = 400
        region.rows = 300
        region.adjust()

        tmpfile = tempfile(False)
        tmpfile = tmpfile + ".png"

        array = raster2numpy_img(rastname=self.name, region=region,
                                 color="RGB")

        image = QImage(array.data,
                       region.cols, region.rows, QImage.Format_RGB32)
        #image.save("data/e.png")
        image.save(tmpfile)
        self.assertFilesEqualMd5(tmpfile, "data/e.png")
Пример #43
0
    def test_resampling_to_QImg_large(self):

        region = Region()
        region.from_rast(self.name)
        region.cols = 4000
        region.rows = 3000
        region.adjust()

        tmpfile = tempfile(False)
        tmpfile = tmpfile + ".png"

        # With array as argument
        array = np.ndarray((region.rows*region.cols*4), np.uint8)

        raster2numpy_img(rastname=self.name, region=region,
                         color="ARGB", array=array)

        image = QImage(array.data,
                       region.cols, region.rows, QImage.Format_ARGB32)
        #image.save("data/c.png")
        image.save(tmpfile)
        self.assertFilesEqualMd5(tmpfile, "data/c.png")
Пример #44
0
    def test_resampling_to_numpy(self):
        
        region = Region()
        region.ewres = 0.1
        region.nsres = 0.1
        region.adjust()
        region.set_raster_region()
        
        a = raster2numpy(self.name)
        
        self.assertEqual(len(a), 400)
        
        region.ewres = 1
        region.nsres = 1
        region.adjust()
        region.set_raster_region()
        
        a = raster2numpy(self.name)
        
        self.assertEqual(len(a), 40)

        region.ewres = 5
        region.nsres = 5
        region.adjust()
        region.set_raster_region()
        
        a = raster2numpy(self.name)
        
        self.assertEqual(len(a), 8)
Пример #45
0
class TplotFrame(wx.Frame):
    """The main frame of the application"""

    def __init__(self, parent, giface):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY,
                          title=_("GRASS GIS Temporal Plot Tool"))

        tgis.init(True)
        self._giface = giface
        self.datasetsV = None
        self.datasetsR = None
        # self.vectorDraw=False
        # self.rasterDraw=False
        self.init()
        self._layout()

        # We create a database interface here to speedup the GUI
        self.dbif = tgis.SQLDatabaseInterfaceConnection()
        self.dbif.connect()
        self.Bind(wx.EVT_CLOSE, self.onClose)
        self.region = Region()

    def init(self):
        self.timeDataR = OrderedDict()
        self.timeDataV = OrderedDict()
        self.temporalType = None
        self.unit = None
        self.listWhereConditions = []
        self.plotNameListR = []
        self.plotNameListV = []
        self.poi = None

    def __del__(self):
        """Close the database interface and stop the messenger and C-interface
           subprocesses.
        """
        if self.dbif.connected is True:
            self.dbif.close()
        tgis.stop_subprocesses()

    def onClose(self, evt):
        # this two lines return errors during exit
        # self.coorval.OnClose()
        # self.cats.OnClose()
        self.Destroy()

    def _layout(self):
        """Creates the main panel with all the controls on it:
             * mpl canvas
             * mpl navigation toolbar
             * Control panel for interaction
        """
        self.mainPanel = wx.Panel(self)
        # Create the mpl Figure and FigCanvas objects.
        # 5x4 inches, 100 dots-per-inch
        #
        # color =  wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND)
        # ------------CANVAS AND TOOLBAR------------
        self.fig = Figure((5.0, 4.0), facecolor=(1, 1, 1))
        self.canvas = FigCanvas(self.mainPanel, wx.ID_ANY, self.fig)
        # axes are initialized later
        self.axes2d = None
        self.axes3d = None

        # Create the navigation toolbar, tied to the canvas
        #
        self.toolbar = NavigationToolbar(self.canvas)

        #
        # Layout
        #
        # ------------MAIN VERTICAL SIZER------------
        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
        self.vbox.Add(self.toolbar, 0, wx.EXPAND)
        # self.vbox.AddSpacer(10)

        # ------------ADD NOTEBOOK------------
        self.ntb = GNotebook(parent=self.mainPanel, style=FN.FNB_FANCY_TABS)

        # ------------ITEMS IN NOTEBOOK PAGE (RASTER)------------------------

        self.controlPanelRaster = wx.Panel(parent=self.ntb, id=wx.ID_ANY)
        self.datasetSelectLabelR = wx.StaticText(
            parent=self.controlPanelRaster,
            id=wx.ID_ANY,
            label=_(
                'Raster temporal '
                'dataset (strds)'))

        self.datasetSelectR = gselect.Select(
            parent=self.controlPanelRaster, id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE, type='strds', multiple=True)
        self.coor = wx.StaticText(parent=self.controlPanelRaster, id=wx.ID_ANY,
                                  label=_('X and Y coordinates separated by '
                                          'comma:'))
        try:
            self._giface.GetMapWindow()
            self.coorval = gselect.CoordinatesSelect(
                parent=self.controlPanelRaster, giface=self._giface)
        except:
            self.coorval = wx.TextCtrl(parent=self.controlPanelRaster,
                                       id=wx.ID_ANY,
                                       size=globalvar.DIALOG_TEXTCTRL_SIZE,
                                       validator=CoordinatesValidator())

        self.coorval.SetToolTipString(_("Coordinates can be obtained for example"
                                        " by right-clicking on Map Display."))
        self.controlPanelSizerRaster = wx.BoxSizer(wx.VERTICAL)
        # self.controlPanelSizer.Add(wx.StaticText(self.panel, id=wx.ID_ANY,
        # label=_("Select space time raster dataset(s):")),
        # pos=(0, 0), flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
        self.controlPanelSizerRaster.Add(self.datasetSelectLabelR,
                                         flag=wx.EXPAND)
        self.controlPanelSizerRaster.Add(self.datasetSelectR, flag=wx.EXPAND)

        self.controlPanelSizerRaster.Add(self.coor, flag=wx.EXPAND)
        self.controlPanelSizerRaster.Add(self.coorval, flag=wx.EXPAND)

        self.controlPanelRaster.SetSizer(self.controlPanelSizerRaster)
        self.controlPanelSizerRaster.Fit(self)
        self.ntb.AddPage(page=self.controlPanelRaster, text=_('STRDS'),
                         name='STRDS')

        # ------------ITEMS IN NOTEBOOK PAGE (VECTOR)------------------------
        self.controlPanelVector = wx.Panel(parent=self.ntb, id=wx.ID_ANY)
        self.datasetSelectLabelV = wx.StaticText(
            parent=self.controlPanelVector, id=wx.ID_ANY,
            label=_(
                'Vector temporal '
                'dataset (strds)\n'
                'Please press enter if'
                ' you digit the name'
                ' instead select with'
                ' combobox'))
        self.datasetSelectV = gselect.Select(
            parent=self.controlPanelVector, id=wx.ID_ANY,
            size=globalvar.DIALOG_GSELECT_SIZE, type='stvds', multiple=True)
        self.datasetSelectV.Bind(wx.EVT_COMBOBOX_CLOSEUP,
                                 self.OnVectorSelected)

        self.attribute = gselect.ColumnSelect(parent=self.controlPanelVector)
        self.attributeLabel = wx.StaticText(parent=self.controlPanelVector,
                                            id=wx.ID_ANY,
                                            label=_('Select attribute column'))
        # TODO fix the category selection as done for coordinates
        try:
            self._giface.GetMapWindow()
            self.cats = gselect.VectorCategorySelect(
                parent=self.controlPanelVector, giface=self._giface)
        except:
            self.cats = wx.TextCtrl(
                parent=self.controlPanelVector,
                id=wx.ID_ANY,
                size=globalvar.DIALOG_TEXTCTRL_SIZE)
        self.catsLabel = wx.StaticText(parent=self.controlPanelVector,
                                       id=wx.ID_ANY,
                                       label=_('Select category of vector(s)'))

        self.controlPanelSizerVector = wx.BoxSizer(wx.VERTICAL)
        # self.controlPanelSizer.Add(wx.StaticText(self.panel, id=wx.ID_ANY,
        # label=_("Select space time raster dataset(s):")),
        # pos=(0, 0), flag=wx.EXPAND | wx.ALIGN_CENTER_VERTICAL)
        self.controlPanelSizerVector.Add(self.datasetSelectLabelV,
                                         flag=wx.EXPAND)
        self.controlPanelSizerVector.Add(self.datasetSelectV, flag=wx.EXPAND)

        self.controlPanelSizerVector.Add(self.attributeLabel, flag=wx.EXPAND)
        self.controlPanelSizerVector.Add(self.attribute, flag=wx.EXPAND)

        self.controlPanelSizerVector.Add(self.catsLabel, flag=wx.EXPAND)
        self.controlPanelSizerVector.Add(self.cats, flag=wx.EXPAND)

        self.controlPanelVector.SetSizer(self.controlPanelSizerVector)
        self.controlPanelSizerVector.Fit(self)
        self.ntb.AddPage(page=self.controlPanelVector, text=_('STVDS'),
                         name='STVDS')

        # ------------Buttons on the bottom(draw,help)------------
        self.vButtPanel = wx.Panel(self.mainPanel, id=wx.ID_ANY)
        self.vButtSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.drawButton = wx.Button(self.vButtPanel, id=wx.ID_ANY,
                                    label=_("Draw"))
        self.drawButton.Bind(wx.EVT_BUTTON, self.OnRedraw)
        self.helpButton = wx.Button(self.vButtPanel, id=wx.ID_ANY,
                                    label=_("Help"))
        self.helpButton.Bind(wx.EVT_BUTTON, self.OnHelp)
        self.vButtSizer.Add(self.drawButton)
        self.vButtSizer.Add(self.helpButton)
        self.vButtPanel.SetSizer(self.vButtSizer)

        self.mainPanel.SetSizer(self.vbox)
        self.vbox.Add(self.ntb, flag=wx.EXPAND)
        self.vbox.Add(self.vButtPanel, flag=wx.EXPAND)
        self.vbox.Fit(self)
        self.mainPanel.Fit()

    def _getSTRDdata(self, timeseries):
        """Load data and read properties
        :param list timeseries: a list of timeseries
        """
        if not self.poi:
            GError(parent=self, message=_("Invalid input coordinates"),
                   showTraceback=False)
            return
        mode = None
        unit = None
        columns = ','.join(['name', 'start_time', 'end_time'])
        for series in timeseries:
            name = series[0]
            fullname = name + '@' + series[1]
            etype = series[2]
            sp = tgis.dataset_factory(etype, fullname)
            if not sp.is_in_db(dbif=self.dbif):
                GError(message=_("Dataset <%s> not found in temporal "
                                 "database") % (fullname), parent=self)
                return
            sp.select(dbif=self.dbif)

            minmin = sp.metadata.get_min_min()
            self.plotNameListR.append(name)
            self.timeDataR[name] = OrderedDict()

            self.timeDataR[name]['temporalDataType'] = etype
            self.timeDataR[name]['temporalType'] = sp.get_temporal_type()
            self.timeDataR[name]['granularity'] = sp.get_granularity()

            if mode is None:
                mode = self.timeDataR[name]['temporalType']
            elif self.timeDataR[name]['temporalType'] != mode:
                GError(
                    parent=self, message=_(
                        "Datasets have different temporal"
                        " type (absolute x relative), "
                        "which is not allowed."))
                return

            # check topology
            maps = sp.get_registered_maps_as_objects(dbif=self.dbif)
            self.timeDataR[name]['validTopology'] = sp.check_temporal_topology(
                maps=maps, dbif=self.dbif)

            self.timeDataR[name]['unit'] = None  # only with relative
            if self.timeDataR[name]['temporalType'] == 'relative':
                start, end, self.timeDataR[name][
                    'unit'] = sp.get_relative_time()
                if unit is None:
                    unit = self.timeDataR[name]['unit']
                elif self.timeDataR[name]['unit'] != unit:
                    GError(parent=self, message=_("Datasets have different "
                                                  "time unit which is not "
                                                  "allowed."))
                    return

            rows = sp.get_registered_maps(columns=columns, where=None,
                                          order='start_time', dbif=self.dbif)
            for row in rows:
                self.timeDataR[name][row[0]] = {}
                self.timeDataR[name][row[0]]['start_datetime'] = row[1]
                self.timeDataR[name][row[0]]['end_datetime'] = row[2]
                r = RasterRow(row[0])
                r.open()
                val = r.get_value(self.poi)
                r.close()
                if val == -2147483648 and val < minmin:
                    self.timeDataR[name][row[0]]['value'] = None
                else:
                    self.timeDataR[name][row[0]]['value'] = val

        self.unit = unit
        self.temporalType = mode
        return

    def _parseVDbConn(self, mapp, layerInp):
        '''find attribute key according to layer of input map'''
        vdb = Module('v.db.connect', map=mapp, flags='g', stdout_=PIPE)

        vdb = vdb.outputs.stdout
        for line in vdb.splitlines():
            lsplit = line.split('|')
            layer = lsplit[0].split('/')[0]
            if str(layer) == str(layerInp):
                return lsplit[2]
        return None

    def _getExistingCategories(self, mapp, cats):
        """Get a list of categories for a vector map"""
        vdb = grass.read_command('v.category', input=mapp, option='print')
        categories = vdb.splitlines()
        for cat in cats:
            if str(cat) not in categories:
                GMessage(message=_("Category {ca} is not on vector map"
                                   " {ma} and it will be used").format(ma=mapp,
                                                                       ca=cat),
                         parent=self)
                cats.remove(cat)
        return cats

    def _getSTVDData(self, timeseries):
        """Load data and read properties
        :param list timeseries: a list of timeseries
        """

        mode = None
        unit = None
        cats = None
        attribute = self.attribute.GetValue()
        if self.cats.GetValue() != '':
            cats = self.cats.GetValue().split(',')
        if cats and self.poi:
            GMessage(message=_("Both coordinates and categories are set, "
                               "coordinates will be used. The use categories "
                               "remove text from coordinate form"))
        if not attribute or attribute == '':
            GError(parent=self, showTraceback=False,
                   message=_("With Vector temporal dataset you have to select"
                             " an attribute column"))
            return
        columns = ','.join(['name', 'start_time', 'end_time', 'id', 'layer'])
        for series in timeseries:
            name = series[0]
            fullname = name + '@' + series[1]
            etype = series[2]
            sp = tgis.dataset_factory(etype, fullname)
            if not sp.is_in_db(dbif=self.dbif):
                GError(message=_("Dataset <%s> not found in temporal "
                                 "database") % (fullname), parent=self,
                       showTraceback=False)
                return
            sp.select(dbif=self.dbif)

            rows = sp.get_registered_maps(dbif=self.dbif, order="start_time",
                                          columns=columns, where=None)

            self.timeDataV[name] = OrderedDict()
            self.timeDataV[name]['temporalDataType'] = etype
            self.timeDataV[name]['temporalType'] = sp.get_temporal_type()
            self.timeDataV[name]['granularity'] = sp.get_granularity()

            if mode is None:
                mode = self.timeDataV[name]['temporalType']
            elif self.timeDataV[name]['temporalType'] != mode:
                GError(
                    parent=self, showTraceback=False, message=_(
                        "Datasets have different temporal type ("
                        "absolute x relative), which is not allowed."))
                return
            self.timeDataV[name]['unit'] = None  # only with relative
            if self.timeDataV[name]['temporalType'] == 'relative':
                start, end, self.timeDataV[name][
                    'unit'] = sp.get_relative_time()
                if unit is None:
                    unit = self.timeDataV[name]['unit']
                elif self.timeDataV[name]['unit'] != unit:
                    GError(message=_("Datasets have different time unit which"
                                     " is not allowed."), parent=self,
                           showTraceback=False)
                    return
            if self.poi:
                self.plotNameListV.append(name)
                # TODO set an appropriate distance, right now a big one is set
                # to return the closer point to the selected one
                out = grass.vector_what(map='pois_srvds',
                                        coord=self.poi.coords(),
                                        distance=10000000000000000)
                if len(out) != len(rows):
                    GError(parent=self, showTraceback=False,
                           message=_("Difference number of vector layers and "
                                     "maps in the vector temporal dataset"))
                    return
                for i in range(len(rows)):
                    row = rows[i]
                    values = out[i]
                    if str(row['layer']) == str(values['Layer']):
                        lay = "{map}_{layer}".format(map=row['name'],
                                                     layer=values['Layer'])
                        self.timeDataV[name][lay] = {}
                        self.timeDataV[name][lay][
                            'start_datetime'] = row['start_time']
                        self.timeDataV[name][lay][
                            'end_datetime'] = row['start_time']
                        self.timeDataV[name][lay]['value'] = values[
                            'Attributes'][attribute]
            else:
                wherequery = ''
                cats = self._getExistingCategories(rows[0]['name'], cats)
                totcat = len(cats)
                ncat = 1
                for cat in cats:
                    if ncat == 1 and totcat != 1:
                        wherequery += '{k}={c} or'.format(c=cat, k="{key}")
                    elif ncat == 1 and totcat == 1:
                        wherequery += '{k}={c}'.format(c=cat, k="{key}")
                    elif ncat == totcat:
                        wherequery += ' {k}={c}'.format(c=cat, k="{key}")
                    else:
                        wherequery += ' {k}={c} or'.format(c=cat, k="{key}")

                    catn = "cat{num}".format(num=cat)
                    self.plotNameListV.append("{na}+{cat}".format(na=name,
                                                                  cat=catn))
                    self.timeDataV[name][catn] = OrderedDict()
                    ncat += 1
                for row in rows:
                    lay = int(row['layer'])
                    catkey = self._parseVDbConn(row['name'], lay)
                    if not catkey:
                        GError(
                            parent=self, showTraceback=False, message=_(
                                "No connection between vector map {vmap} "
                                "and layer {la}".format(
                                    vmap=row['name'], la=lay)))
                        return
                    vals = grass.vector_db_select(
                        map=row['name'], layer=lay, where=wherequery.format(
                            key=catkey), columns=attribute)
                    layn = "lay{num}".format(num=lay)
                    for cat in cats:
                        catn = "cat{num}".format(num=cat)
                        if layn not in self.timeDataV[name][catn].keys():
                            self.timeDataV[name][catn][layn] = {}
                        self.timeDataV[name][catn][layn][
                            'start_datetime'] = row['start_time']
                        self.timeDataV[name][catn][layn][
                            'end_datetime'] = row['end_time']
                        self.timeDataV[name][catn][layn]['value'] = vals['values'][int(cat)][
                            0]
        self.unit = unit
        self.temporalType = mode
        return

    def _drawFigure(self):
        """Draws or print 2D plot (temporal extents)"""
        self.axes2d.clear()
        self.axes2d.grid(False)
        if self.temporalType == 'absolute':
            self.axes2d.xaxis_date()
            self.fig.autofmt_xdate()
            self.convert = mdates.date2num
            self.invconvert = mdates.num2date
        else:
            self.convert = lambda x: x
            self.invconvert = self.convert

        self.colors = cycle(COLORS)

        self.yticksNames = []
        self.yticksPos = []
        self.plots = []

        if self.datasetsR:
            self.lookUp = LookUp(self.timeDataR, self.invconvert)
        else:
            self.lookUp = LookUp(self.timeDataV, self.invconvert)

        if self.datasetsR:
            self.drawR()
        if self.datasetsV:
            if self.poi:
                self.drawV()
            elif self.cats:
                self.drawVCats()

        self.canvas.draw()
        DataCursor(self.plots, self.lookUp, InfoFormat, self.convert)

    def drawR(self):
        for i, name in enumerate(self.datasetsR):
            name = name[0]
            # just name; with mapset it would be long
            self.yticksNames.append(name)
            self.yticksPos.append(1)  # TODO
            xdata = []
            ydata = []
            for keys, values in self.timeDataR[name].iteritems():
                if keys in ['temporalType', 'granularity', 'validTopology',
                            'unit', 'temporalDataType']:
                    continue
                xdata.append(self.convert(values['start_datetime']))
                ydata.append(values['value'])

            if len(ydata) == ydata.count(None):
                GError(parent=self, showTraceback=False,
                       message=_("Problem getting data from raster temporal"
                                 " dataset. Empty list of values."))
                return
            self.lookUp.AddDataset(yranges=ydata, xranges=xdata,
                                   datasetName=name)
            color = self.colors.next()
            self.plots.append(self.axes2d.plot(xdata, ydata, marker='o',
                                               color=color,
                                               label=self.plotNameListR[i])[0])

        if self.temporalType == 'absolute':
            self.axes2d.set_xlabel(
                _("Temporal resolution: %s" % self.timeDataR[name]['granularity']))
        else:
            self.axes2d.set_xlabel(_("Time [%s]") % self.unit)
        self.axes2d.set_ylabel(', '.join(self.yticksNames))

        # legend
        handles, labels = self.axes2d.get_legend_handles_labels()
        self.axes2d.legend(loc=0)

    def drawVCats(self):
        for i, name in enumerate(self.plotNameListV):
            # just name; with mapset it would be long
            labelname = name.replace('+', ' ')
            self.yticksNames.append(labelname)
            name_cat = name.split('+')
            name = name_cat[0]
            self.yticksPos.append(1)  # TODO
            xdata = []
            ydata = []
            for keys, values in self.timeDataV[
                    name_cat[0]][
                    name_cat[1]].iteritems():
                if keys in ['temporalType', 'granularity', 'validTopology',
                            'unit', 'temporalDataType']:
                    continue
                xdata.append(self.convert(values['start_datetime']))
                ydata.append(values['value'])

            if len(ydata) == ydata.count(None):
                GError(parent=self, showTraceback=False,
                       message=_("Problem getting data from raster temporal"
                                 " dataset. Empty list of values."))
                return
            self.lookUp.AddDataset(yranges=ydata, xranges=xdata,
                                   datasetName=name)
            color = self.colors.next()

            self.plots.append(
                self.axes2d.plot(
                    xdata,
                    ydata,
                    marker='o',
                    color=color,
                    label=labelname)[0])
        # ============================
        if self.temporalType == 'absolute':
            self.axes2d.set_xlabel(
                _("Temporal resolution: %s" % self.timeDataV[name]['granularity']))
        else:
            self.axes2d.set_xlabel(_("Time [%s]") % self.unit)
        self.axes2d.set_ylabel(', '.join(self.yticksNames))

        # legend
        handles, labels = self.axes2d.get_legend_handles_labels()
        self.axes2d.legend(loc=0)
        self.listWhereConditions = []

    def drawV(self):
        for i, name in enumerate(self.plotNameListV):
            # just name; with mapset it would be long
            self.yticksNames.append(self.attribute.GetValue())
            self.yticksPos.append(0)  # TODO
            xdata = []
            ydata = []
            for keys, values in self.timeDataV[name].iteritems():
                if keys in ['temporalType', 'granularity', 'validTopology',
                            'unit', 'temporalDataType']:
                    continue
                xdata.append(self.convert(values['start_datetime']))
                ydata.append(values['value'])

            if len(ydata) == ydata.count(None):
                GError(parent=self, showTraceback=False,
                       message=_("Problem getting data from raster temporal"
                                 " dataset. Empty list of values."))
                return
            self.lookUp.AddDataset(yranges=ydata, xranges=xdata,
                                   datasetName=name)
            color = self.colors.next()

            self.plots.append(self.axes2d.plot(xdata, ydata, marker='o',
                                               color=color, label=name)[0])
        # ============================
        if self.temporalType == 'absolute':
            self.axes2d.set_xlabel(
                _("Temporal resolution: %s" % self.timeDataV[name]['granularity']))
        else:
            self.axes2d.set_xlabel(_("Time [%s]") % self.unit)
        self.axes2d.set_ylabel(', '.join(self.yticksNames))

        # legend
        handles, labels = self.axes2d.get_legend_handles_labels()
        self.axes2d.legend(loc=0)
        self.listWhereConditions = []

    def OnRedraw(self, event=None):
        """Required redrawing."""
        self.init()
        datasetsR = self.datasetSelectR.GetValue().strip()
        datasetsV = self.datasetSelectV.GetValue().strip()

        if not datasetsR and not datasetsV:
            return

        try:
            getcoors = self.coorval.coordsField.GetValue()
        except:
            try:
                getcoors = self.coorval.GetValue()
            except:
                getcoors = None
        if getcoors and getcoors != '':
            try:
                coordx, coordy = getcoors.split(',')
                coordx, coordy = float(coordx), float(coordy)
            except (ValueError, AttributeError):
                try:
                    coordx, coordy = self.coorval.GetValue().split(',')
                    coordx, coordy = float(coordx), float(coordy)
                except (ValueError, AttributeError):
                    GMessage(message=_("Incorrect coordinates format, should "
                                       "be: x,y"), parent=self)
            coors = [coordx, coordy]
            if coors:
                try:
                    self.poi = Point(float(coors[0]), float(coors[1]))
                except GException:
                    GError(parent=self, message=_("Invalid input coordinates"),
                           showTraceback=False)
                    return
                if not self.poi:
                    GError(parent=self, message=_("Invalid input coordinates"),
                           showTraceback=False)
                    return
                bbox = self.region.get_bbox()
                if not bbox.contains(self.poi):
                    GError(parent=self, message=_("Seed point outside the "
                                                  "current region"),
                           showTraceback=False)
                    return
        # check raster dataset
        if datasetsR:
            datasetsR = datasetsR.split(',')
            try:
                datasetsR = self._checkDatasets(datasetsR, 'strds')
                if not datasetsR:
                    return
            except GException:
                GError(parent=self, message=_("Invalid input raster dataset"),
                       showTraceback=False)
                return
            if not self.poi:
                GError(parent=self, message=_("Invalid input coordinates"),
                       showTraceback=False)
                return
            self.datasetsR = datasetsR

        # check vector dataset
        if datasetsV:
            datasetsV = datasetsV.split(',')
            try:
                datasetsV = self._checkDatasets(datasetsV, 'stvds')
                if not datasetsV:
                    return
            except GException:
                GError(parent=self, message=_("Invalid input vector dataset"),
                       showTraceback=False)
                return
            self.datasetsV = datasetsV
        self._redraw()

    def _redraw(self):
        """Readraw data.

        Decides if to draw also 3D and adjusts layout if needed.
        """
        if self.datasetsR:
            self._getSTRDdata(self.datasetsR)

        if self.datasetsV:
            self._getSTVDData(self.datasetsV)

        # axes3d are physically removed
        if not self.axes2d:
            self.axes2d = self.fig.add_subplot(1, 1, 1)
        self._drawFigure()

    def _checkDatasets(self, datasets, typ):
        """Checks and validates datasets.

        Reports also type of dataset (e.g. 'strds').

        :param list datasets: list of temporal dataset's name
        :return: (mapName, mapset, type)
        """
        validated = []
        tDict = tgis.tlist_grouped(type=typ, group_type=True, dbif=self.dbif)
        # nested list with '(map, mapset, etype)' items
        allDatasets = [[[(map, mapset, etype) for map in maps]
                        for etype, maps in etypesDict.iteritems()]
                       for mapset, etypesDict in tDict.iteritems()]
        # flatten this list
        if allDatasets:
            allDatasets = reduce(lambda x, y: x + y, reduce(lambda x, y: x + y,
                                                            allDatasets))
            mapsets = tgis.get_tgis_c_library_interface().available_mapsets()
            allDatasets = [
                i
                for i in sorted(
                    allDatasets, key=lambda l: mapsets.index(l[1]))]

        for dataset in datasets:
            errorMsg = _("Space time dataset <%s> not found.") % dataset
            if dataset.find("@") >= 0:
                nameShort, mapset = dataset.split('@', 1)
                indices = [n for n, (mapName, mapsetName, etype) in enumerate(
                    allDatasets) if nameShort == mapName and mapsetName == mapset]
            else:
                indices = [n for n, (mapName, mapset, etype) in enumerate(
                    allDatasets) if dataset == mapName]

            if len(indices) == 0:
                raise GException(errorMsg)
            elif len(indices) >= 2:
                dlg = wx.SingleChoiceDialog(
                    self,
                    message=_(
                        "Please specify the "
                        "space time dataset "
                        "<%s>." % dataset),
                    caption=_("Ambiguous dataset name"),
                    choices=[
                        ("%(map)s@%(mapset)s:"
                         " %(etype)s" % {
                             'map': allDatasets[i][0],
                             'mapset': allDatasets[i][1],
                             'etype': allDatasets[i][2]}) for i in indices],
                    style=wx.CHOICEDLG_STYLE | wx.OK)
                if dlg.ShowModal() == wx.ID_OK:
                    index = dlg.GetSelection()
                    validated.append(allDatasets[indices[index]])
                else:
                    continue
            else:
                validated.append(allDatasets[indices[0]])

        return validated

    def OnHelp(self, event):
        """Function to show help"""
        RunCommand(prog='g.manual', quiet=True, entry='g.gui.tplot')

    def SetDatasets(self, rasters, vectors, coors, cats, attr):
        """Set the data
        #TODO
        :param list rasters: a list of temporal raster dataset's name
        :param list vectors: a list of temporal vector dataset's name
        :param list coors: a list with x/y coordinates
        :param list cats: a list with incld. categories of vector
        :param str attr:  name of atribute of vectror data
        """
        if not (rasters or vectors) or not (coors or cats):
            return
        try:
            if rasters:
                self.datasetsR = self._checkDatasets(rasters, 'strds')
            if vectors:
                self.datasetsV = self._checkDatasets(vectors, 'stvds')
            if not (self.datasetsR or self.datasetsV):
                return
        except GException:
            GError(parent=self, message=_("Invalid input temporal dataset"),
                   showTraceback=False)
            return
        if coors:
            try:
                self.poi = Point(float(coors[0]), float(coors[1]))
            except GException:
                GError(parent=self, message=_("Invalid input coordinates"),
                       showTraceback=False)
                return
            try:
                self.coorval.coordsField.SetValue(','.join(coors))
            except:
                self.coorval.SetValue(','.join(coors))
        if self.datasetsV:
            vdatas = ','.join(map(lambda x: x[0] + '@' + x[1], self.datasetsV))
            self.datasetSelectV.SetValue(vdatas)
            if attr:
                self.attribute.SetValue(attr)
            if cats:
                self.cats.SetValue(cats)
        if self.datasetsR:
            self.datasetSelectR.SetValue(
                ','.join(map(lambda x: x[0] + '@' + x[1], self.datasetsR)))
        self._redraw()

    def OnVectorSelected(self, event):
        """Update the controlbox related to stvds"""
        dataset = self.datasetSelectV.GetValue().strip()
        if dataset:
            try:
                vect_list = grass.read_command('t.vect.list', flags='s',
                                               input=dataset, column='name')
            except Exception:
                self.attribute.Clear()
                GError(
                    parent=self,
                    message=_("Invalid input temporal dataset"),
                    showTraceback=False)
                return
            vect_list = list(set(sorted(vect_list.split())))
            for vec in vect_list:
                self.attribute.InsertColumns(vec, 1)
        else:
            return
Пример #46
0
def _get_raster_image_as_np(lock, conn, data):
    """Convert a raster map into an image and return
       a numpy array with RGB or Gray values.

       :param lock: A multiprocessing.Lock instance
       :param conn: A multiprocessing.Pipe instance used to send True or False
       :param data: The list of data entries [function_id, raster_name, extent, color]
    """
    array = None
    try:
        name = data[1]
        mapset = data[2]
        extent = data[3]
        color = data[4]

        mapset = utils.get_mapset_raster(name, mapset)

        if not mapset:
            raise ValueError("Unable to find raster map <%s>"%(name))

        rast = RasterRow(name, mapset)

        if rast.exist():

            reg = Region()
            reg.from_rast(name)

            if extent is not None:
                if "north" in extent:
                    reg.north = extent["north"]
                if "south" in extent:
                    reg.south = extent["south"]
                if "east" in extent:
                    reg.east =  extent["east"]
                if "west" in extent:
                    reg.west =  extent["west"]
                if "rows" in extent:
                    reg.rows =  extent["rows"]
                if "cols" in extent:
                    reg.cols =  extent["cols"]
                reg.adjust()

            array = raster2numpy_img(name, reg, color)
    except:
        raise
    finally:
        conn.send(array)