Example #1
0
def parse_alias(alias_file):
    """Parse alias file if provided"""
    if alias_file:
        if not os.access(alias_file, os.R_OK):
            gscript.fatal(
                _("Alias file <{}> not found or not readable".format(
                    alias_file)))

        with open(alias_file, "r") as a_f:
            alias_dict = gscript.parse_key_val(a_f.read(), sep=",")
    else:
        alias_dict = None

    if alias_dict:
        for alias, full_name in alias_dict.items():
            # Skip invaid lines
            if not alias or not full_name:
                continue
            # Check if environmental parameter map(s) exist
            if "@" in full_name:
                raster, mapset = full_name.split("@")
            else:
                raster, mapset = full_name, ""
            raster_map = RasterRow(full_name)  # raster, mapset)
            mapset = "." if not mapset else mapset
            if not raster_map.exist():
                gscript.fatal(
                    _("Could not find environmental parameter raster map <{}> in mapset <{}>."
                      .format(raster, mapset)))

    return alias_dict
Example #2
0
def sample(vect_in_name, rast_in_name):
    """sample('point00', 'field')"""
    # instantiate the object maps
    vect_in = VectorTopo(vect_in_name)
    rast_in = RasterRow(rast_in_name)
    vect_out = VectorTopo('test_' + vect_in_name)
    # define the columns of the attribute table of the new vector map
    columns = [(u'cat',       'INTEGER PRIMARY KEY'),
               (rast_in_name,  'DOUBLE')]
    # open the maps
    vect_in.open('r')
    rast_in.open('r')
    vect_out.open('w', tab_cols=columns, link_driver='sqlite')
    # get the current region
    region = Region()
    # initialize the counter
    counter = 0
    data = []
    for pnt in vect_in.viter('points'):
        counter += 1
        # transform the spatial coordinates in row and col value
        x, y = coor2pixel(pnt.coords(), region)
        value = rast_in[int(x)][int(y)]
        data.append((counter, None if np.isnan(value) else float(value)))
        # write the geometry features
        vect_out.write(pnt)
    # write the attributes
    vect_out.table.insert(data, many=True)
    vect_out.table.conn.commit()
    # close the maps
    vect_in.close()
    rast_in.close()
    vect_out.close()
Example #3
0
def _split_maps(maps, splitting):
    from datetime import datetime
    before = OrderedDic()
    after = OrderedDic()
    split = None
    if splitting.count('T') == 0:
        try:
            split = datetime.strptime(splitting, "%Y-%m-%d")
        except ValueError:
            pass
    else:
        try:
            split = datetime.strptime(splitting, "%Y-%m-%dT%H:%M:%S")
        except ValueError:
            pass
    if not split:
        gscript.fatal(_("It is not possible to parse splittingday value. "
                        "Please you one of the two supported format: "
                        "'%Y-%m-%d' or '%Y-%m-%dT%H:%M:%S'"))
    for mapp in maps:
        tempext = mapp.get_temporal_extent()
        raster = RasterRow(mapp.get_name())
        raster.open('r')
        array = np.array(raster)
        if tempext.start_time <= split:
            before[mapp.get_name()] = array
        else:
            after[mapp.get_name()] = array
    return before, after
Example #4
0
def getRandomGridCoords(n, accessible):
    c = grass.region()
    rows = c['rows']
    cols = c['cols']
    nc = c['n']
    wc = c['w']
    ns = c['nsres']
    ew = c['ewres']
    
    if(rows*cols<n):
        n = rows*cols
    rand_rows = np.random.randint(0,rows, n)
    rand_cols = np.random.randint(0,cols, n)
    if accessible:
        rvals = RasterRow(crmap, mapset)
        rvals.open('r')
        
        for i in xrange(0,n):
            for j in xrange(0,n):
                while rvals[rand_rows[i]][rand_cols[j]] < 0 or rvals[rand_rows[i]][rand_cols[j]]>=999:
                    rand_rows[i] = np.random.randint(0,rows)
                    rand_cols[i] = np.random.randint(0,cols)
        rvals.close()
    
    return [Point(wc + rand_cols[i]*ew + ew/2, nc - rand_rows[i]*ns - ns/2) for i in xrange(0,n)]
Example #5
0
    def setup(self):
        """Open all input raster maps, generate the time vectors and return them with the map type as tuple

        :param map_list:
        :param dbif:
        :return:
        """
        print("Setup strds", self.strds.get_id())
        self.start_times = []
        self.end_times = []

        # Open all existing maps for processing
        for map in self.map_list:
            start, end = map.get_temporal_extent_as_tuple()
            self.start_times.append(start)
            self.end_times.append(end)

            rmap = RasterRow(map.get_id())
            rmap.open(mode='r')
            if self.mtype is not None:
                if self.mtype != rmap.mtype:
                    self.dbif.close()
                    gcore.fatal(
                        _("Space time raster dataset <%s> is contains map with different type. "
                          "This is not supported.") % input)

            self.mtype = rmap.mtype
            self.open_input_maps.append(rmap)

        self.dt_start_times = DatetimeIndex(self.start_times)
        self.dt_end_times = DatetimeIndex(self.end_times)
def rpatch_map(raster, mapset, mset_str, bbox_list, overwrite=False,
               start_row=0, start_col=0, prefix=''):
    """Patch raster using a bounding box list to trim the raster."""
    # Instantiate the RasterRow input objects
    rast = RasterRow(prefix + raster, mapset)
    with RasterRow(name=raster, mapset=mset_str % (0, 0), mode='r') as rtype:
        rast.open('w', mtype=rtype.mtype, overwrite=overwrite)
    rasts = []
    mrast = 0
    nrows = len(bbox_list)
    for row, rbbox in enumerate(bbox_list):
        rrasts = []
        max_rasts = []
        for col in range(len(rbbox)):
            libgis.G_percent(row, nrows, 1)
            rrasts.append(RasterRow(name=raster,
                                    mapset=mset_str % (start_row + row,
                                                       start_col + col)))
            rrasts[-1].open('r')
            mrast += rrasts[-1].info.max + 1
            max_rasts.append(mrast)
        rasts.append(rrasts)
        rpatch_row(rast, rrasts, rbbox, max_rasts)
        for rst in rrasts:
            rst.close()
            del(rst)

    rast.close()
Example #7
0
 def testCategory(self):
     r = RasterRow(self.name)
     r.open()
     cats = r.cats
     cats1 = Category(self.name)
     cats1.read()
     self.assertEqual(cats, cats1)
     r.close()
Example #8
0
def check_raster(name):
    r = RasterRow(name)
    try:
        r.open(mode='r')
        r.close()
        return True
    except:
        return False
Example #9
0
 def testCategory(self):
     r = RasterRow(self.name)
     r.open()
     cats = r.cats
     cats1 = Category(self.name)
     cats1.read()
     # this is not working, I don't know why
     self.assertEqual(cats, cats1)
     r.close()
Example #10
0
 def test_open_r(self):
     notexist = RasterRow(self.name + "notexist")
     with self.assertRaises(OpenError):
         # raster does not exist
         notexist.open(mode="r")
     r = RasterRow(self.name)
     r.open(mode="r", mtype="FCELL")
     # ignore the mtype if is open in read mode
     self.assertEqual(r.mtype, "DCELL")
     r.close()
Example #11
0
 def test_open_r(self):
     notexist = RasterRow(self.tmp + 'notexist')
     with self.assertRaises(OpenError):
         # raster does not exist
         notexist.open(mode='r')
     r = RasterRow(self.name)
     r.open(mode='r', mtype='DCELL')
     # ignore the mtype if is open in read mode
     self.assertEqual(r.mtype, 'FCELL')
     r.close()
Example #12
0
 def test_isopen(self):
     r = RasterRow(self.name)
     self.assertFalse(r.is_open())
     r.open(mode='r')
     self.assertTrue(r.is_open())
     r.close()
     self.assertFalse(r.is_open())
Example #13
0
def assert_raster_no_difference(reference, actual, precision):
    with RasterRow(reference) as ref, RasterRow(actual) as act:
        for i_row, (r_row, a_row) in enumerate(zip(ref, act)):
            diff = abs(r_row - a_row) > precision
            if diff.any():
                cols = diff.nonzero()
                msg = ("The two rows are different!\nrow:{i_row}, "
                       "cols={cols}\nref: {ref}\nact:{act}")
                raise AssertionError(msg.format(i_row=i_row, cols=cols,
                                                ref=r_row[cols],
                                                act=a_row[cols]))
Example #14
0
def cleanup():
    """Restore old mask if it existed"""
    if RasterRow("{}_MASK".format(TMP_NAME), Mapset().name).exist():
        gscript.verbose("Restoring old mask...")
        if RasterRow("MASK", Mapset().name).exist():
            gscript.run_command("r.mask", flags="r")
        gscript.run_command(
            "g.rename", rast="{}_MASK,MASK".format(TMP_NAME), quiet=True
        )
    if RasterRow("MASK", Mapset().name).exist():
        gscript.run_command("r.mask", flags="r")
 def test_open_w(self):
     r = RasterRow(self.name)
     with self.assertRaises(OpenError):
         # raster type is not defined!
         r.open(mode='w')
     with self.assertRaises(OpenError):
         # raster already exist
         r.open(mode='w', mtype='DCELL')
     # open in write mode and overwrite
     r.open(mode='w', mtype='DCELL', overwrite=True)
     self.assertTrue(r.mtype, 'DCELL')
     r.close()
Example #16
0
 def testHistory(self):
     r = RasterRow(self.name)
     r.open()
     hist = r.hist
     hist1 = History(self.name)
     hist1.read()
     # this is not working, I don't know why
     #self.assertEqual(hist, hist1)
     self.assertEqual(hist.creator, hist1.creator)
     hist1.creator = 'markus'
     self.assertNotEqual(hist.creator, hist1.creator)
     r.close()
Example #17
0
 def test_isopen(self):
     r = RasterRow(self.name)
     self.assertFalse(r.is_open())
     r.open(mode='r')
     self.assertTrue(r.is_open())
     r.close()
     self.assertFalse(r.is_open())
Example #18
0
def _kappa_pixel(maps1, maps2, out, method, over):
    from grass.pygrass.raster.buffer import Buffer
    import sklearn
    rasterout = RasterRow(out, overwrite=over)
    rasterout.open('w','DCELL')
    array1 = maps1.values()[0]
    for row in range(len(array1)):
        newrow = Buffer((len(array1[row]),), mtype='DCELL')
        for col in range(len(array1[row])):
            vals1 = np.ndarray(len(maps1.values()))
            vals2 = np.ndarray(len(maps2.values()))
            x = 0
            for value in maps1.values():
                vals1[x] = value[row][col]
                x += 1
            x = 0
            for value in maps2.values():
                vals2[x] = value[row][col]
                x += 1
            if sklearn.__version__ >= '0.18':
                outval = sklearn.metrics.cohen_kappa_score(vals1, vals2,
                                                           weights=method)
            else:
                outval = sklearn.metrics.cohen_kappa_score(vals1, vals2)
            newrow[col] = outval
        rasterout.put_row(newrow)
    rasterout.close()
    return
Example #19
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()
Example #20
0
 def test_open_w(self):
     r = RasterRow(self.name)
     with self.assertRaises(OpenError):
         # raster type is not defined!
         r.open(mode='w')
     with self.assertRaises(OpenError):
         # raster already exist
         r.open(mode='w', mtype='DCELL')
     # open in write mode and overwrite
     r.open(mode='w', mtype='DCELL', overwrite=True)
     self.assertTrue(r.mtype, 'DCELL')
     r.close()
Example #21
0
def manage_map_band_reference(name, band_ref):
    """Manage band reference assigned to a single raster map

    :param str name: raster map name
    :param str band_ref: band reference (None for dissociating band reference)

    :return int: return code
    """
    from grass.pygrass.raster import RasterRow

    try:
        with RasterRow(name) as rast:
            if band_ref:
                gs.debug(
                    _("Band reference <{}> assigned to raster map <{}>").
                    format(band_ref, name), 1)
            else:
                gs.debug(
                    _("Band reference dissociated from raster map <{}>").
                    format(name), 1)
            try:
                rast.info.band_reference = band_ref
            except GrassError as e:
                gs.error(
                    _("Unable to assign/dissociate band reference. {}").format(
                        e))
                return 1
    except OpenError as e:
        gs.error(_("Map <{}> not found in current mapset").format(name))
        return 1

    return 0
Example #22
0
 def _init_elev(self):
     """Return"""
     with RasterRow(self.mapname) as rrow:
         for row in rrow:
             self.elev.append([])
             for elem in row:
                 self.elev[-1].append(elem)
Example #23
0
def raster2compressM(A):
    """Return a compress matrix from a raster map"""
    with RasterRow(A, mode="r") as A_ar:
        A_sparse = np.array(A_ar)
    A_sparse[A_sparse == -2147483648] = 0
    A_sparse = csr_matrix(A_sparse)
    return A_sparse
Example #24
0
def worker(cmd):

    window, src = cmd
    
    reg = Region()
    old_reg = deepcopy(reg)
    
    try:
        # update region
        reg.north = dict(window)['north']
        reg.south = dict(window)['south']
        reg.west = dict(window)['west']
        reg.east = dict(window)['east']
        reg.set_current()
        reg.write()
        reg.set_raster_region()
        
        # read raster data
        with RasterRow(src) as rs:
            arr = np.asarray(rs)
    except:
        pass

    finally:
        # reset region
        old_reg.write()
        reg.set_raster_region()
    
    return(arr)
Example #25
0
def manage_map_semantic_label(name, semantic_label):
    """Manage semantic label assigned to a single raster map

    :param str name: raster map name
    :param str semantic_label: semantic label (None for dissociating semantic label)

    :return int: return code
    """
    from grass.pygrass.raster import RasterRow

    try:
        with RasterRow(name) as rast:
            if semantic_label:
                gs.debug(
                    _("Semantic label <{}> assigned to raster map <{}>").format(
                        semantic_label, name
                    ),
                    1,
                )
            else:
                gs.debug(
                    _("Semantic label dissociated from raster map <{}>").format(name), 1
                )
            try:
                rast.info.semantic_label = semantic_label
            except GrassError as e:
                gs.error(_("Unable to assign/dissociate semantic label. {}").format(e))
                return 1
    except OpenError as e:
        gs.error(_("Map <{}> not found in current mapset").format(name))
        return 1

    return 0
Example #26
0
def raster2df(raster):
    """ Written with tricks from the notebook
    http://nbviewer.jupyter.org/github/zarch/workshop-pygrass/blob/master/03_Raster.ipynb
    """

    #from __future__ import (nested_scopes, generators, division, absolute_import,
    #                    with_statement, print_function, unicode_literals)
    from grass.pygrass.raster import RasterRow

    # Use PYGRASS API to stdout to a np/pandas array
    # null = -2147483648
    crops = RasterRow(raster)
    crops.open('r')
    df = pd.DataFrame(np.array(crops))
    #df.replace(-2147483648,np.nan, inplace=True)
    return df
Example #27
0
    def _get_raster_dim(self, dem_clip):
        """
        Get raster spatial reference info.

        :param dem_clip: clipped dem raster map
        """
        # size of the raster [0] = number of rows; [1] = number of columns
        self.data['r'] = self.data['mat_dem'].shape[0]
        self.data['c'] = self.data['mat_dem'].shape[1]

        with RasterRow(dem_clip) as data:
            # lower left corner coordinates
            self.data['xllcorner'] = data.info.west
            self.data['yllcorner'] = data.info.south
            # x/y resolution
            self.data['vpix'] = data.info.nsres
            self.data['spix'] = data.info.ewres
            # check data consistency
            # see https://github.com/storm-fsv-cvut/smoderp2d/issues/42
            if data.info.rows != self.data['r'] or \
               data.info.cols != self.data['c']:
                raise DataPreparationError(
                    "Data inconsistency ({},{}) vs ({},{})".format(
                        data.info.rows, data.info.cols, self.data['r'], self.data['c']
                ))

        self.data['NoDataValue'] = None
        self.data['pixel_area'] = self.data['spix'] * self.data['vpix']
Example #28
0
def cleanup():
    """Remove raster and vector maps stored in a list"""
    grass.run_command(
        "g.remove",
        flags="f",
        type="raster,vector",
        pattern="{}_*".format(TEMPNAME),
        quiet=True,
        stderr=subprocess.PIPE,
    )

    # Reset mask if user MASK was present
    if (RasterRow("MASK",
                  Mapset().name).exist()
            and RasterRow("MASK_{}".format(TEMPNAME)).exist()):
        grass.run_command("r.mask", flags="r", quiet=True)
    reset_mask()
Example #29
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)
    finally:
        # Send even if an exception was raised.
        conn.send(array)
Example #30
0
def raster_type(raster, tabulate, use_label):
    """Check raster map type (int or double) and return categories for int maps

    :param raster: name of the raster map to check
    :type raster: string
    :param tabulate: check categories for tabulation
    :type tabulate: bool
    :param use_label: use label strings instead of category numbers
    :type use_label: bool
    :returns: string with raster map type and list of categories with labels
    :rmap_type: string
    :returns: logical if rastermap contains valid labels
    :rmap_type: bool
    :rcats: list of category tuples
    :Example:

    >>> raster_type('elevation')
    'double precision', False, []
    """

    valid_lab = False
    r_map = RasterRow(raster)
    r_map.open()
    if not r_map.has_cats() and r_map.mtype != "CELL":
        rmap_type = "double precision"
        rcats = []
    else:
        rmap_type = "int"
        rcats = []
        if tabulate:
            rcats = r_map.cats
            r_map.close()
            if not rcats:
                rcats = []
            if len(rcats) == 0:
                rcats = (grass.read_command(
                    "r.category", map=raster).rstrip("\n").split("\n"))
                rcats = [
                    tuple((rcat.split("\t")[1], rcat.split("\t")[0], None))
                    for rcat in rcats
                ]
            cat_list = [rcat[1] for rcat in rcats]
            label_list = [rcat[0] for rcat in rcats]
            if len(set(cat_list)) != len(set(label_list)):
                rcats = [tuple((rcat[1], rcat[1], None)) for rcat in rcats]
            elif use_label:
                valid_lab = True
        else:
            r_map.close()

    return rmap_type, valid_lab, rcats
Example #31
0
 def test_open_r(self):
     notexist = RasterRow(self.name + 'notexist')
     with self.assertRaises(OpenError):
         # raster does not exist
         notexist.open(mode='r')
     r = RasterRow(self.name)
     r.open(mode='r', mtype='FCELL')
     # ignore the mtype if is open in read mode
     self.assertEqual(r.mtype, 'DCELL')
     r.close()
Example #32
0
    def layers(self, mapnames):
        """Setter method for the layers attribute in the RasterStack

        Parameters
        ----------
        mapnames : str, list
            Name or list of names of GRASS GIS rasters to add to the
            RasterStack object.
        """

        # some checks
        if isinstance(mapnames, str):
            mapnames = [mapnames]

        # reset existing attributes
        for name in list(self.layers.keys()):
            delattr(self, name)

        self.loc = _LocIndexer(self)
        self.iloc = _ILocIndexer(self, self.loc)
        self.count = len(mapnames)
        self.mtypes = {}

        # split raster name from mapset name
        raster_names = [i.split("@")[0] for i in mapnames]
        mapset_names = [get_mapset_raster(i) for i in mapnames]

        if None in mapset_names:
            missing_idx = mapset_names.index(None)
            missing_rasters = raster_names[missing_idx]
            gs.fatal(
                "GRASS GIS raster(s) {x} is not found in any mapsets".format(
                    x=missing_rasters))

        # add rasters and metadata to stack
        for name, mapset in zip(raster_names, mapset_names):

            with RasterRow(name=name, mapset=mapset) as src:

                if src.exist() is True:

                    ras_name = src.name.split("@")[0]  # name sans mapset
                    full_name = src.name_mapset()  # name with mapset
                    valid_name = ras_name.replace(".", "_")

                    # grass gis raster could have same name if in diff mapset
                    if valid_name in list(self.layers.keys()):
                        raise ValueError(
                            "Cannot append map {name} to the "
                            "RasterStack because a map with the same name "
                            "already exists".format(name=ras_name))

                    self.mtypes.update({full_name: src.mtype})
                    self.loc[valid_name] = src
                    setattr(self, valid_name, src)

                else:
                    gs.fatal("GRASS raster map " + r + " does not exist")
    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()
Example #34
0
 def test_name(self):
     r = RasterRow(self.name)
     r.open(mode='r')
     self.assertEqual(r.name, self.name)
     fullname = "{name}@{mapset}".format(name=r.name, mapset=r.mapset)
     self.assertEqual(r.fullname(), fullname)
     r.close()
Example #35
0
def rpatch_map(raster, mapset, mset_str, bbox_list, overwrite=False,
               start_row=0, start_col=0, prefix=''):
    # TODO is prefix useful??
    """Patch raster using a bounding box list to trim the raster.

    :param raster: the name of output raster
    :type raster: str
    :param mapset: the name of mapset to use
    :type mapset: str
    :param mset_str:
    :type mset_str: str
    :param bbox_list: a list of BBox object to convert
    :type bbox_list: list of BBox object
    :param overwrite: overwrite existing raster
    :type overwrite: bool
    :param start_row: the starting row of original raster
    :type start_row: int
    :param start_col: the starting column of original raster
    :type start_col: int
    :param prefix: the prefix of output raster
    :type prefix: str
    """
    # Instantiate the RasterRow input objects
    rast = RasterRow(prefix + raster, mapset)
    rtype = RasterRow(name=raster, mapset=mset_str % (0, 0))
    rtype.open('r')
    rast.open('w', mtype=rtype.mtype, overwrite=overwrite)
    rtype.close()
    rasts = []
    for row, rbbox in enumerate(bbox_list):
        rrasts = []
        for col in range(len(rbbox)):
            rrasts.append(RasterRow(name=raster,
                                    mapset=mset_str % (start_row + row,
                                                       start_col + col)))
            rrasts[-1].open('r')
        rasts.append(rrasts)
        rpatch_row(rast, rrasts, rbbox)

        for rst in rrasts:
            rst.close()
            del(rst)

    rast.close()
Example #36
0
def df2raster(df, newrastname):
    """
    Writes a pandas dataframe to a GRASS raster
    
    """
    new = newrastname
    new = RasterRow('newET')
    new.open('w', overwrite=True)
    for row in df.iterrows():
        new.put_row(row)

    new.close()
Example #37
0
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()
def raster_type(raster, tabulate, use_lable):
    """Check raster map type (int or double) and return categories for int maps

    :param raster: name of the raster map to check
    :type raster: string
    :param tabulate: check categories for tabulation
    :type tabulate: bool
    :param use_lable: use label strings instead of category numbers
    :type use_lable: bool
    :returns: string with raster map type and list of categories with lables
    :rmap_type: string
    :rcats: list of category tuples
    :Example:

    >>> raster_type('elevation')
    'double precision', []
    """
    r_map = RasterRow(raster)
    r_map.open()
    if not r_map.has_cats() and r_map.mtype != "CELL":
        rmap_type = 'double precision'
        rcats = []
    else:
        rmap_type = 'int'
        rcats = []
        if tabulate:
            rcats = r_map.cats
            r_map.close()
            if not rcats:
                rcats = []
            if len(rcats) == 0:
                rcats = grass.read_command("r.category",
                                           map=raster).rstrip('\n').split('\n')
                rcats = [
                    tuple((rcat.split('\t')[1], rcat.split('\t')[1], None))
                    for rcat in rcats
                ]
            cat_list = [rcat[0] for rcat in rcats]
            lable_list = [rcat[1] for rcat in rcats]
            if use_lable:
                racts = lable_list if len(set(cat_list)) == len(
                    set(lable_list)) else cat_list
            else:
                racts = cat_list
        else:
            r_map.close()

    return rmap_type, rcats
Example #39
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)
Example #40
0
    def _predict_multi(self, estimator, region, indexes, class_labels, height,
                       func, output, overwrite):
        # create and open rasters for writing if incremental reading
        if height is not None:
            dst = []

            for i, label in enumerate(class_labels):
                rastername = output + "_" + str(label)
                dst.append(RasterRow(rastername))
                dst[i].open("w", mtype="FCELL", overwrite=overwrite)

            # create data reader generator
            n_windows = len([i for i in self.row_windows(height=height)])

            data_gen = (
                (wi, self.read(rows=rows))
                for wi, rows in enumerate(self.row_windows(height=height)))

        # perform prediction
        try:
            if height is not None:
                for wi, arr in data_gen:
                    gs.percent(wi, n_windows, 1)
                    result = func(arr, estimator)
                    result = np.ma.filled(result, np.nan)

                    # write multiple features to GRASS GIS rasters
                    for i, arr_index in enumerate(indexes):
                        for row in range(result.shape[1]):
                            newrow = Buffer((region.cols, ), mtype="FCELL")
                            newrow[:] = result[arr_index, row, :]
                            dst[i].put_row(newrow)
            else:
                arr = self.read()
                result = func(arr, estimator)
                result = np.ma.filled(result, np.nan)

                for i, arr_index in enumerate(indexes):
                    numpy2raster(
                        result[arr_index, :, :],
                        mtype="FCELL",
                        rastname=rastername[i],
                        overwrite=overwrite,
                    )
        except:
            gs.fatal("Error in raster prediction")

        finally:
            if height is not None:
                for i in dst:
                    i.close()

        return RasterStack([i.name for i in dst])
Example #41
0
    def testHistory(self):
        r = RasterRow(self.name)
        r.open("r")
        hist = r.hist

        self.assertEqual(decode(hist.title), self.name)
        self.assertEqual(decode(hist.keyword), "This is a test map")

        hist1 = History(self.name)
        hist1.read()

        self.assertEqual(decode(hist1.title), self.name)
        self.assertEqual(decode(hist1.keyword), "This is a test map")

        self.assertEqual(hist, hist1)
        self.assertEqual(decode(hist.creator), decode(hist1.creator))
        hist1.creator = "Markus"
        self.assertNotEqual(decode(hist.creator), decode(hist1.creator))
        r.close()

        hist1.title = "No such title"
        hist1.keyword = "No such description"
        hist1.src1 = "No such source 1"
        hist1.src2 = "No such source 2"
        hist1.write()

        r.open("r")
        hist = r.hist

        self.assertEqual(decode(hist.title), "No such title")
        self.assertEqual(decode(hist.keyword), "No such description")
        self.assertEqual(decode(hist.creator), "Markus")
        self.assertEqual(decode(hist.creator), "Markus")
        self.assertEqual(decode(hist.src1), "No such source 1")
        self.assertEqual(decode(hist.src2), "No such source 2")
        r.close()

        hist1 = History("no_map")
        hist1.command()
        self.assertEqual(decode(hist1.line(0)), "test_history.py")
Example #42
0
def rename_maps(base, date=None, year=None, month=None, startnum=1, log=None):
    if isinstance(date, datetime):
        mydat = deepcopy(date)
    elif year and month:
        mydat = datetime(year, month, 1, 0, 0)
    else:
        print("Please set date or year (with or without month")
        sys.exit(1)
    if not date:
        date = datetime(year, month, 1, 0, 0)
    if log:
        fi = open(log, 'w')
    for do in range(startnum, 745):
        out = "{ba}_{da}".format(ba=base, da=mydat.strftime("%Y_%m_%d_%H"))
        inn = "{ba}_{mo}.{im}".format(ba=base, mo=date.strftime("%Y_%m"),
                                      im=do)
        try:
            r = RasterRow(inn)
            r.open()
            r.close()
        except:
            continue
        cop = Module("g.rename", raster=(inn, out),
                     stdout_=PIPE, stderr_=PIPE)
        if log:
            if cop.outputs.stdout:
                fi.write("{}\n".format(cop.outputs.stdout))
            if cop.outputs.stderr:
                fi.write("{}\n".format(cop.outputs.stderr))
        mydat = mydat + timedelta(seconds=MINUTE)
    if log:
        fi.close()
Example #43
0
 def test_name(self):
     r = RasterRow(self.name)
     r.open(mode='r')
     self.assertEqual(r.name, self.name)
     fullname = "{name}@{mapset}".format(name=r.name, mapset=r.mapset)
     self.assertEqual(r.fullname(), fullname)
     r.close()
Example #44
0
def parse_bgr_input(alias_input, env_maps, alias_names):
    """Parse environmental background input"""
    if env_maps:
        env_maps = env_maps.split(",")

    if alias_names:
        alias_names = alias_names.split(",")

    if alias_input:
        if not os.access(alias_input, os.R_OK):
            gscript.fatal(
                _("The file containing alias names does not exist or is not readable.")
            )
        gscript.info(
            _(
                "Alias and map names are beeing read from file. Other input regarding environmental parameter(s) will be ignored..."
            )
        )
        with open(alias_input, "r") as alias_txt:
            lines = alias_txt.readlines()
            parameters = []
            alias = []
            for line in lines:
                if "," in line:
                    parameters.append(line.split(",")[1].strip())
                    alias.append(line.split(",")[0].strip())
    else:
        parameters = env_maps

        if alias_names:
            alias = alias_names
        else:
            alias = [param.split("@")[0] for param in parameters]

    # Check if number of alias names is identically with number of background maps
    if len(alias) != len(parameters):
        gscript.fatal(
            _("Number of provided background maps and alias names do not match.")
        )

    for param in parameters:
        # Check if environmental parameter map(s) exist
        if not RasterRow(param).exist():
            gscript.fatal(
                _(
                    "Could not find environmental parameter raster map <{}>".format(
                        param
                    )
                )
            )

    return alias, parameters
Example #45
0
    def testHistory(self):
        r = RasterRow(self.name)
        r.open("r")
        hist = r.hist
        
        self.assertEqual(hist.title, "A test map")
        self.assertEqual(hist.keyword,  "This is a test map")
        
        hist1 = History(self.name)
        hist1.read()

        self.assertEqual(hist1.title, "A test map")
        self.assertEqual(hist1.keyword,  "This is a test map")
        
        self.assertEqual(hist, hist1)
        self.assertEqual(hist.creator, hist1.creator)
        hist1.creator = "Markus"
        self.assertNotEqual(hist.creator, hist1.creator)
        r.close()
        
        hist1.title = "No such title"
        hist1.keyword = "No such description"
        hist1.src1 = "No such source 1"
        hist1.src2 = "No such source 2"
        hist1.write()
        
        r.open("r")
        hist = r.hist

        self.assertEqual(hist.title, "No such title")
        self.assertEqual(hist.keyword,  "No such description")
        self.assertEqual(hist.creator, "Markus")
        self.assertEqual(hist.creator, "Markus")
        self.assertEqual(hist.src1, "No such source 1")
        self.assertEqual(hist.src2, "No such source 2")
        r.close()
 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()
#
#with RasterRow('aspect') as elev:
#    for row in elev:
#        l = []
#        for cell in row:
#            l.append(str(cell))
#        print ' '.join(l)

options, flags = gcore.parser()

direction = options['direction']
magnitude = options['magnitude']
probability = options['magnitude']
scale = float(options['scale'])

direction = RasterRow(direction)
speed = RasterRow(magnitude)

direction.open()
speed.open()

rows = []
for i, dir_row in enumerate(direction):
    speed_row = speed[i]
    vectors = []
    for j, dir_cell in enumerate(dir_row):
        speed_cell = speed_row[j]
        if speed_cell < 0:
            speed_cell = 0
        dx = numpy.cos(dir_cell / 180. * math.pi) * speed_cell * scale
        dy = - numpy.sin(dir_cell / 180. * math.pi) * speed_cell * scale
Example #48
0
File: frame.py Project: caomw/grass
    def _getData(self, timeseries):
        """Load data and read properties

        :param list timeseries: a list of timeseries
        """
        self.timeData = OrderedDict()
        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)
            sp.select(dbif=self.dbif)

            self.timeData[name] = OrderedDict()
            if not sp.is_in_db(dbif=self.dbif):
                GError(self, message=_("Dataset <%s> not found in temporal "
                                       "database") % (fullname))
                return

            self.timeData[name]['temporalDataType'] = etype
            self.timeData[name]['temporalType'] = sp.get_temporal_type()
            self.timeData[name]['granularity'] = sp.get_granularity()
            if mode is None:
                mode = self.timeData[name]['temporalType']
            elif self.timeData[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.timeData[name]['validTopology'] = sp.check_temporal_topology(maps=maps, dbif=self.dbif)

            self.timeData[name]['unit'] = None  # only with relative
            if self.timeData[name]['temporalType'] == 'relative':
                start, end, self.timeData[name]['unit'] = sp.get_relative_time()
                if unit is None:
                    unit = self.timeData[name]['unit']
                elif self.timeData[name]['unit'] != unit:
                    GError(self, _("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.timeData[name][row[0]] = {}
                self.timeData[name][row[0]]['start_datetime'] = row[1]
                self.timeData[name][row[0]]['end_datetime'] = row[2]
                r = RasterRow(row[0])
                r.open()
                val = r.get_value(self.poi)
                r.close()
                self.timeData[name][row[0]]['value'] = val
        self.unit = unit
        self.temporalType = mode
        return
Example #49
0
def ml2rast(segsname, outname, hdf=None, idsname=None, ids=None, hist=None):
    ids = ids if ids else pnd.read_hdf(hdf, idsname)
    t0 = time.time()
    segs = RasterRow(segsname)
    segs.open('r')
    out = RasterRow(outname)
    out.open('w', mtype='CELL', overwrite=True)
    nrows = len(segs)
    for r, row in enumerate(segs):
        #import ipdb; ipdb.set_trace()
        #row[:] = ids.loc[row]  # => MemoryError
        for i, col in enumerate(row):
            try:
                row[i] = ids[col]
            except KeyError:
                row[i] = 0
        out.put_row(row)
        G_percent(r, nrows, 10)
    segs.close()
    if hist:
        import datetime, os
        out.hist.date = datetime.datetime.now()
        out.hist.title = hist
        out.hist.creator = os.getenv('USER')
        out.hist.write(out.name)
    out.close()
    print("Time spent writing the raster %s: %.2fs" % (outname,
                                                       time.time() - t0))
Example #50
0
 def test_exist(self):
     notexist = RasterRow(self.name + 'notexist')
     self.assertFalse(notexist.exist())
     exist = RasterRow(self.name)
     self.assertTrue(exist.exist())
Example #51
0
def ifnumpy(mapname0, mapname1):
    # instantiate raster objects
    old = RasterRow(mapname0)
    new = RasterRow(mapname1)
    # open the maps
    old.open('r')
    new.open('w', mtype=old.mtype, overwrite=True)
    # start a cycle
    for row in old:
        new.put_row(row > 50)
    # close the maps
    new.close()
    old.close()
Example #52
0
 def test_type(self):
     r = RasterRow(self.name)
     r.open(mode='r')
     self.assertTrue(r.mtype,'DCELL')
     r.close()
#!/usr/bin/env python

from grass.pygrass.raster import RasterRow
from grass.pygrass.vector import Vector
from grass.pygrass.gis.region import Region
from grass.pygrass.utils import coor2pixel
from grass.pygrass.modules import Module

rast = 'dmt@PERMANENT'

Module('g.region', raster=rast)

region = Region()

dmt = RasterRow(rast)
dmt.open('r')

obce = Vector('obce_bod')
obce.open('r')

for o in obce:
    x, y = coor2pixel(o.coords(), region)
    value = dmt[int(x)][int(y)]
    print (u'{:40}: {:.0f}'.format(o.attrs['nazev'], value))

obce.close()
dmt.close()