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 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)]
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))
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)
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_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()
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 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 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))
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()
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 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()
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 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 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()
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 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()
def check_raster(name): r = RasterRow(name) try: r.open(mode='r') r.close() return True except: return False
def check_raster_exists(rast): try: r = RasterRow(inn) r.open() r.close() except: return False return True
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 testCategory(self): r = RasterRow(self.name) r.open() cats = r.cats cats1 = Category(self.name) cats1.read() self.assertEqual(cats, cats1) r.close()
def check_raster(name): r = RasterRow(name) try: r.open(mode='r') r.close() return True except: return False
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 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 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 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()
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()
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 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()
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 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 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 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()
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 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
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
def createRast(name, matrix, inverse=False): """Create the new raster map using the output matrix of calculateOblique""" newscratch = RasterRow(name) newscratch.open('w', overwrite=True) try: for r in matrix: if inverse: r.reverse() newrow = Buffer((len(r), ), mtype='FCELL') for c in range(len(r)): newrow[c] = r[c] newscratch.put_row(newrow) newscratch.close() return True except: return False
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 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 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()
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()
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")
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 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 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_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')
#!/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()
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
def test_type(self): r = RasterRow(self.name) r.open(mode='r') self.assertTrue(r.mtype,'DCELL') r.close()
# 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 m = speed_cell * scale #dx = 5; #dy = 0;