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.0, 21.0, 31.0, 41.0]) six.assertCountEqual(self, rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0]) rast.close()
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
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
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)
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
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()
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())
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()
def check_raster_exists(rast): try: r = RasterRow(inn) r.open() r.close() except: return False return True
def check_raster(name): r = RasterRow(name) try: r.open(mode='r') r.close() return True except: return False
def testCategory(self): r = RasterRow(self.name) r.open() cats = r.cats cats1 = Category(self.name) cats1.read() self.assertEqual(cats, cats1) r.close()
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)
def main(opts, flgs): TMPVECT = [] DEBUG = True if flgs['d'] else False atexit.register(cleanup, vector=TMPVECT, debug=DEBUG) # check input maps plant = [ opts['plant_column_discharge'], opts['plant_column_elevup'], opts['plant_column_elevdown'], opts['plant_column_point_id'], opts['plant_column_plant_id'], opts['plant_column_power'], opts['plant_column_stream_id'] ] ovwr = overwrite() try: plnt = check_required_columns(opts['plant'], int(opts['plant_layer']), plant, 'plant') except ParameterError as exc: exception2error(exc) return if not opts['output_point']: output_point = 'tmp_output_point' TMPVECT.append(output_point) else: output_point = opts['output_point'] plnt = conv_segpoints(opts['plant'], output_point) el, mset = (opts['elevation'].split('@') if '@' in opts['elevation'] else (opts['elevation'], '')) elev = RasterRow(name=el, mapset=mset) elev.open('r') plnt.open('r') plants, skipped = read_plants(plnt, elev=elev, restitution='restitution', intake='intake', ckind_label='kind_label', cdischarge='discharge', celevation='elevation', cid_point='cat', cid_plant='plant_id') plnt.close() # contour options resolution = float(opts['resolution']) if opts['resolution'] else None write_structures(plants, opts['output_struct'], elev, ndigits=int(opts['ndigits']), resolution=resolution, contour=opts['contour'], overwrite=ovwr) elev.close()
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 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()
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()
def test_row_range(self): r = RasterRow(self.name) with self.assertRaises(IndexError): # Map is not open yet r[1] with self.assertRaises(IndexError): # Index is out of range r.open() r[9999] r.close()
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)
def test_open_w(self): r = RasterRow(self.tmp) 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')
def convert_maps(base, date=None, year=None, month=None, startnum=1, log=None): """Convert the data if needed, like temperature from kelvin to celsius""" 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') Module("g.region", raster="{ba}_{mo}.{im}".format(ba=base, im=startnum, mo=date.strftime("%Y_%m"))) 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 if base == 'T_2M': try: mapc = Module("r.mapcalc", expression="{ou} = {inn} - " "273.15".format(ou=out, inn=inn), stdout_=PIPE, stderr_=PIPE) if log: if mapc.outputs.stdout: fi.write("{}\n".format(mapc.outputs.stdout)) if mapc.outputs.stderr: fi.write("{}\n".format(mapc.outputs.stderr)) Module("g.remove", type="raster", name=inn, flags="f") except CalledModuleError: continue elif base == 'TOT_PRECIP': try: mapc = Module("r.mapcalc", expression="{ou} = if({inn} < 0, 0," " {inn})".format(ou=out, inn=inn), stdout_=PIPE, stderr_=PIPE) if log: if mapc.outputs.stdout: fi.write("{}\n".format(mapc.outputs.stdout)) if mapc.outputs.stderr: fi.write("{}\n".format(mapc.outputs.stderr)) Module("g.remove", type="raster", name=inn, flags="f") except CalledModuleError: continue mydat = mydat + timedelta(seconds=3600) if log: fi.close()
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()
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])
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()
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()
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
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
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
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) msgr = get_msgr() rasts = [] mrast = 0 nrows = len(bbox_list) for row, rbbox in enumerate(bbox_list): rrasts = [] max_rasts = [] for col in range(len(rbbox)): msgr.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()
def worker(src): window, src = src window = dict(window) window['n'] = window.pop('north') window['s'] = window.pop('south') window['e'] = window.pop('east') window['w'] = window.pop('west') del (window['top']) del (window['bottom']) g.region(**window) with RasterRow(src) as rs: arr = np.asarray(rs) return (arr)
def cleanup(): """Remove temporary data """ #remove temporary region file grass.del_temp_region() try: grass.run_command('g.remove', flags='f', name=TMP_MAPS, quiet=True, type=['vector', 'raster'], stderr=os.devnull, stdout_=os.devnull) except: pass if RasterRow("MASK", Mapset().name).exist(): grass.run_command("r.mask", flags="r", quiet=True) reset_mask()