def main(): """ Compute cell areas """ projinfo = grass.parse_command('g.proj', flags='g') options, flags = grass.parser() output = options['output'] units = options['units'] # First check if output exists if len(grass.parse_command('g.list', type='rast', pattern=options['output'])): if not grass.overwrite(): grass.fatal("Raster map '" + options['output'] + "' already exists. Use '--o' to overwrite.") # Then compute if projinfo['units'] == 'meters': if units == 'm2': grass.mapcalc(output+' = nsres() * ewres()') elif units == 'km2': grass.mapcalc(output+' = nsres() * ewres() / 10.^6') elif projinfo['units'] == 'degrees': if units == 'm2': grass.mapcalc(output+' = ( 111195. * nsres() ) * \ ( ewres() * '+str(np.pi/180.)+' * 6371000. * cos(y()) )') elif units == 'km2': grass.mapcalc(output+' = ( 111.195 * nsres() ) * \ ( ewres() * '+str(np.pi/180.)+' * 6371. * cos(y()) )') else: print 'Units: ', + projinfo['units'] + ' not currently supported'
def getValOnPolys(inmap,poly,elev,dist,output): n = getAreasNum(inmap) for i in range(n-1): # extract one feature from points set with cat value. g.run_command("v.extract",input=poly,output="tmp",type="area",layer=1,new=-1,list=i+1,quiet=True,overwrite=True) # Add a new column for storing a new value. g.run_command("v.db.addcol",map="tmp",columns="val int") # set default value as 1. g.run_command("v.db.update",map="tmp",column="val",value=1,quiet=True) # Redefine geographic region for procedure. g.run_command("g.region",vect="tmp",quiet=True) # convert vector map buffer to raster map buffer. g.run_command("v.to.rast",input="tmp",output="tmprast",use="val",overwrite=True,quiet=True) # get elevation on the raster buffer. g.run_command("r.mapcalculator",amap="tmprast",bmap=elev,formula="A*B",outfile="tmprast2",overwrite=True,quiet=True) pix = g.parse_command("r.stats",flags="1",input="tmprast2",nv="",fs=",",quiet=True) res = "ID_" + str(i+1) for j in pix.keys(): res = str(res) + str(j) + "," # Write the result to text file file = open(output + str(i+1) + ".txt", "w") file.write(res) file.write("\n") file.close() # Remove temporal features g.parse_command("g.remove",vect="tmp",rast="tmprast,tmprast2",quiet=True) # Set region with DEM. g.parse_command("g.region",rast=elv,quiet=True)
def main(iters,bounds,padding,sampres,sampint,repeats,sampdist,prefix): '''Main set of code for the sampling simulation''' #set the sample universe size to the maximum boundaries, and set the resolution to match the sample size grass.run_command("g.region", quiet = True, n=(bounds + padding + padding), s=0, e=(bounds + padding + padding), w=0, res=sampres) # calculate the real binary presence/absence map for the input distribution (to compare with the sampled presabs) realpresab = "%s_real_presab" % prefix grass.mapcalc("${realpresab}=if(${sampdist} > 0, 1, 0)", overwrite = True, quiet = True, sampdist = sampdist, realpresab = realpresab) #extract the number of squares that have at least one artifact truepres = int(grass.parse_command("r.stats", quiet = True, flags="cn", input=realpresab, separator="=")["1"]) #set up statsfile f = open('%s%s%s_stats.csv' % (os.getcwd(), os.sep, prefix), 'w+') f.write("Iteration,Positives,True Positives,Difference,Pcnt Difference,R,R2\n") f.flush() #initiate the outer loop, which will randomly rotate/move the sampling grid within the sampling universe pad = len(str(repeats)) # set the zfill pad to match the number of repeats for x in range(repeats): grass.message("Iteration: %s" % (x + 1)) jitt1 = random.randint(0,sampint) # get a random seed for the Y offset jitt2 = random.randint(0,sampint) # get a random seed for the X offset rot = random.randint(0,360) # get a random seed for the grid rotation #reset the sample universe size and resolution grass.run_command("g.region", quiet = True, n=(bounds + padding - jitt1), s=padding - jitt1, e=(bounds + padding - jitt2), w=padding - jitt2, res=sampres) #make the sampling frame from the sampling interval, the current jitter, and the current rotation init_grid = "%s_%s_vgridpts" % (prefix,str(x + 1).zfill(pad)) init_sqrs = "%s_%s_gridpts" % (prefix,str(x + 1).zfill(pad)) grass.run_command("v.mkgrid", quiet = True, overwrite = True, map=init_grid, box="%s,%s" % (sampint,sampint), angle=rot, type="point") grass.run_command("v.to.rast", quiet = True, overwrite = True, input=init_grid, type="point", output=init_sqrs, use="val") # zoom out to the padding distance grass.run_command("g.region", quiet = True, n=(bounds + padding + padding), s=0, e=(bounds + padding + padding), w=0, res=sampres) grass.run_command("r.null", quiet = True, map=init_sqrs, null=0) #initiate and start inner loop for the additive sammpling regime for i in range(iters): if i + 1 == 1: old_sqrs = init_sqrs else: old_sqrs = new_sqrs new_sqrs = "%s_itr%s" % (prefix, str(i + 1).zfill(2)) grass.mapcalc("${new_sqrs}=eval(a=if(${old_sqrs} > 0 && ${sampdist} > 0, 1, 0), b=if(${old_sqrs}[1,0] == 1 || ${old_sqrs}[0,1] == 1 || ${old_sqrs}[-1,0] == 1 || ${old_sqrs}[0,-1] == 1, 1, 0), c=if(b > 0 && ${sampdist} > 0, 1, a), if(isnull(c),0,c))", quiet = True, overwrite = True, old_sqrs = old_sqrs, new_sqrs = new_sqrs, sampdist = sampdist) # rename last map to save it outmap = "%s_%s" % (prefix,str(x + 1).zfill(pad)) grass.run_command("g.rename", quiet = True, raster="%s,%s" % (new_sqrs,outmap)) # pull some stats presab = grass.parse_command("r.stats", quiet = True,flags="cn", input=outmap, separator="=") try: pres = int(presab['1']) except: pres = 0 R = grass.parse_command('r.regression.line', flags='g', mapx=realpresab, mapy=outmap)["R"] f.write("%s,%s,%s,%s,%s,%s,%s\n" % (x + 1,pres,truepres,truepres - pres,(truepres - pres)/float(truepres),R, float(R) * float(R))) f.flush() #clean up f.close() grass.run_command("g.remove", quiet = True, flags = 'f', type = 'raster', pattern = "*_itr*")
def ImportMap(self, map, column): """ Imports GRASS map as SpatialPointsDataFrame and adds x/y columns to attribute table. Checks for NULL values in the provided column and exits if they are present.""" #@NOTE: new way with R - as it doesn't alter original data Rpointmap = robjects.r.readVECT(map, type='point') # checks if x,y columns are present in dataframe. If they do are present, but with different names, # they'll be duplicated. if "x" not in robjects.r.names(Rpointmap): # extract coordinates with S4 method coordinatesPreDF = robjects.r['as.data.frame'](robjects.r.coordinates(Rpointmap)) coordinatesDF = robjects.r['data.frame'](x=coordinatesPreDF.rx('coords.x1')[0], y=coordinatesPreDF.rx('coords.x2')[0]) # match coordinates with data slot of SpatialPointsDataFrame - maptools function # match is done on row.names Rpointmap = robjects.r.spCbind(Rpointmap, coordinatesDF) # GRASS checks for null values in the chosen column. R can hardly handle column as a variable, # looks for a hardcoded string. cols = grass.vector_columns(map=map, layer=1) nulls = int(grass.parse_command('v.univar', map=map, column=column, type='point', parse=(grass.parse_key_val, {'sep': ': '} ) )['number of NULL attributes']) if nulls > 0: grass.fatal( _("%d NULL value(s) in the selected column - unable to perform kriging.") % nulls) return Rpointmap
def get_time_steps(): indexmaps = grass.parse_command('g.list', type='vect', patt='drainage_basins_??????').keys() indexmaps = sorted(indexmaps)[::-1] ages = [] for indexmap in indexmaps: ages.append( str(indexmap.split('_')[-1]) ) return ages
def CompositionMetrics(self): '''Overlay landcover with viewshed, calculate visible cover, and return percentage of landcover classes in viewshed ''' ## Compute statistics ## stat = gscript.parse_command('r.stats', input=self.out_landuse, flags='apn', separator='comma', overwrite=True) cleanDic = {} areaList = [] totarea = 0 for item in stat.keys(): cat = int(item.split(",")[0]) area = item.split(",")[1] percent = item.split(",")[2] cleanDic[cat] = percent.strip("%").strip("u") totarea += float(area) print totarea areaList = [] for item in sorted(self.landuse.keys()): if item in cleanDic.keys(): percent = cleanDic[item] areaList.append(str(percent)) else: areaList.append("0") areaList.append(str(totarea)) return areaList
def set_region_from_timeseries(self, timeseries): """Sets computational region for rendering. This function sets the computation region from the extent of a space-time dataset by using its bounding box and resolution. If user specified the name of saved region during object's initialization, the provided region is used. If it's not specified and use_region=True, current region is used. """ if self._saved_region: self._env["GRASS_REGION"] = gs.region_env( region=self._saved_region, env=self._env) return if self._use_region: # use current return # Get extent, resolution from space time dataset info = gs.parse_command("t.info", input=timeseries, flags="g", env=self._env) # Set grass region from extent self._env["GRASS_REGION"] = gs.region_env( n=info["north"], s=info["south"], e=info["east"], w=info["west"], nsres=info["nsres_min"], ewres=info["ewres_min"], )
def get_variance(mapname, raster): """Calculate intra-segment variance of the values of the given raster""" # current_process name contains '-' which can cause problems so we replace # with '_' temp_map = "isegmentuspo_temp_variance_map_%d_%s" % ( os.getpid(), current_process().name.replace("-", "_"), ) gscript.run_command( "r.stats.zonal", base=mapname, cover=raster, method="variance", output=temp_map, overwrite=True, quiet=True, ) univar = gscript.parse_command("r.univar", map_=temp_map, flags="g", quiet=True) var = float(univar["mean"]) gscript.run_command("g.remove", type_="raster", name=temp_map, flags="f", quiet=True) return var
def citation_for_module(name, add_grass=False): """Provide dictionary of citation values for a module""" path = documentation_filename(name) # derive core strings from lhmpom: # NAME / AUTHOR / LAST CHANGED / COPYRIGHT: Years + Entity text = open(path).read() g_version = gs.parse_command("g.version", flags="g") # using default empty value, this way we use just if d['k'] # to check presence and non-emptiness at the same time citation = defaultdict(str) citation["module"] = name citation["grass-version"] = g_version["version"] citation["grass-build-date"] = g_version["build_date"] citation["authors"] = get_authors_from_documentation(text) citation["year"] = get_year_from_documentation(text) code_url, code_history_url = get_code_urls_from_documentation(text) citation["code-url"] = code_url citation["url-code-history"] = code_history_url if add_grass: scope = "Use the following to cite the whole GRASS GIS" citation["references"] = [grass_cff_reference(g_version, scope=scope)] return citation
def Analyze(self): gscript.run_command('i.segment', group=self.group, output=self.segment, threshold=0.3, minsize=50, env=self.env) gscript.run_command('r.clump', input=self.segment, output=self.segment_clump, env=self.env) gscript.run_command('i.smap', group=self.group, subgroup=self.group, signaturefile=self.signature, output=self.classification, goodness=self.reject, env=self.env) percentile = float( gscript.parse_command('r.univar', flags='ge', map=self.reject, env=self.env)['percentile_90']) gscript.mapcalc( '{new} = if({classif} < {thres}, {classif}, null())'.format( new=self.filtered_classification, classif=self.classification, thres=percentile), env=self.env) gscript.run_command('r.stats.quantile', base=self.segment_clump, cover=self.filtered_classification, output=self.output, env=self.env)
def OnRegression(self, event): """Displays regression information in messagebox """ message = [] title = _('Regression Statistics for Scatterplot(s)') for rpair in self.rasterList: if isinstance(rpair, tuple) == False: continue rast1, rast2 = rpair rast1 = rast1.split('@')[0] rast2 = rast2.split('@')[0] ret = grass.parse_command('r.regression.line', mapx = rast1, mapy = rast2, flags = 'g', quiet = True, parse = (grass.parse_key_val, { 'sep' : '=' })) eqtitle = _('Regression equation for raster map <%(rast1)s> vs. <%(rast2)s>:\n\n') % \ { 'rast1' : rast1, 'rast2' : rast2 } eq = ' %s = %s + %s(%s)\n\n' % (rast2, ret['a'], ret['b'], rast1) num = 'N = %s\n' % ret['N'] rval = 'R = %s\n' % ret['R'] rsq = 'R-squared = %f\n' % pow(float(ret['R']), 2) ftest = 'F = %s\n' % ret['F'] str = eqtitle + eq + num + rval + rsq + ftest message.append(str) stats = PlotStatsFrame(self, id = wx.ID_ANY, message = message, title = title) if stats.Show() == wx.ID_CLOSE: stats.Destroy()
def main(): # process command options input = options['input'] if not gs.find_file(input)['file']: gs.fatal(_("Raster map <%s> not found") % input) output = options['output'] if gs.find_file(output)['file'] and not gs.overwrite(): gs.fatal(_("Output map <%s> already exists") % output) # set aside region for internal use gs.use_temp_region() # subset input if desired region = options.get('region') if region: if not gs.find_file(region)['file']: gs.fatal(_("Raster map <%s> not found") % region) gs.message("Setting region to %s" % region, flag='i') gs.run_command('g.region', rast=region, align=input) else: gs.message("Using existing GRASS region", flag='i') gs.debug('='*50) gs.debug('\n'.join(gs.parse_command('g.region', 'p').keys())) gs.debug('='*50) calculate_noise(input, output) # restore original region gs.del_temp_region() return None
def import_raster(filename, module, args): mapname = _map_name(filename) gs.message(_("Processing <{}>...").format(mapname)) if module == "r.import": kv = gs.parse_command("g.proj", flags="j") if kv["+proj"] == "longlat": args["resolution"] = "estimated" else: args["resolution"] = "value" args["resolution_value"] = _raster_resolution(filename) try: gs.run_command(module, input=filename, output=mapname, **args) if gs.raster_info(mapname)["datatype"] in ("FCELL", "DCELL"): gs.message("Rounding to integer after reprojection") gs.use_temp_region() gs.run_command("g.region", raster=mapname) gs.run_command( "r.mapcalc", quiet=True, expression="tmp_%s = round(%s)" % (mapname, mapname), ) gs.run_command( "g.rename", quiet=True, overwrite=True, raster="tmp_%s,%s" % (mapname, mapname), ) gs.del_temp_region() gs.raster_history(mapname) except CalledModuleError as e: pass # error already printed
def create_heatmaps(vectors, background_ortho, radius, width, height): os.environ['GRASS_FONT'] = '/usr/share/fonts/truetype/freefont/FreeSansBold.ttf' names = [] for vector in vectors: gscript.run_command('v.kernel', input=vector, output=vector + '_kernel', radius=radius, overwrite=True, quiet=True) names.append(vector + '_kernel') gscript.write_command('r.colors', map=names, rules='-', stdin='0% white\n10% yellow\n40% red\n100% magenta') maxdens = float(gscript.parse_command('r.univar', map=names, flags='g')['max']) for vector in vectors: gscript.run_command('d.mon', start='cairo', output='foreground_' + vector + '_kernel' + '.png', width=width, height=height, overwrite=True) gscript.run_command('d.rast', map=vector + '_kernel') gscript.run_command('d.mon', stop='cairo') # background gscript.run_command('d.mon', start='cairo', output='background_' + vector + '_kernel' + '.png', width=width, height=height, overwrite=True) gscript.run_command('d.rast', map=background_ortho) gscript.run_command('d.legend', flags='t', raster=vector + '_kernel', label_step=0.5, digits=1, range=[0, maxdens], at=[3,40,3,6], color='white') gscript.run_command('d.mon', stop='cairo') # put together with transparency foreground = Image.open('foreground_' + vector + '_kernel' + '.png') background = Image.open('background_' + vector + '_kernel' + '.png') foreground = foreground.convert("RGBA") datas = foreground.getdata() newData = [] for item in datas: intens = item[0] + item[1] + item[2] newData.append((item[0], item[1], item[2], min(765-intens, 200))) foreground.putdata(newData) background.paste(foreground, (0, 0), foreground) background.save('heatmap_{v}.png'.format(v=vector), "PNG") gscript.try_remove('foreground_' + vector + '_kernel' + '.png') gscript.try_remove('background_' + vector + '_kernel' + '.png')
def main(out,maps,ms,keepdtype,ndv,createopts): # set mapset set_mapset(ms) # turn categorical names into dummy names maps=parse_feature_names(maps) # make desired output_mask # slopes above 55 degrees masked and points on top of water or snow masked make_output_mask('(slope < 55) && (lcc == 2 || lcc == 3)','buffered_basin_border') # run separately for each map for m in maps: dtype = gscript.parse_command('r.info',flags='g',map=m)['datatype'] if dtype == 'CELL': export_maps([m],out,'Byte',keepdtype,ndv,createopts) else: export_maps([m],out,'Float32',keepdtype,ndv,createopts) # remove group and mask gscript.run_command('g.remove', type='group', name='to_export', quiet=True, flags='f') drop_mask()
def mkContours(self): """Make a contour map of the given DEM, in the given catchment""" # check what breaks to take, if int find nice breaks, if list take as breaks if type(self.contours) is int: interval = self.contours # get stats of DEM for nice breaks stats=grass.parse_command('r.univar',map=self.elevation,flags='g') # make nice breaks minelev = int(float(stats['min'])/interval+1)*interval maxelev = int(float(stats['max'])/interval)*interval breaks = range(minelev,maxelev+1,interval) else: breaks = self.contours if len(breaks)<2: grass.fatal('Need at least 2 contour breaks: %s \nRange of elevation: %s - %s' %(breaks,stats['min'],stats['max'])) grass.message(('Contour breaks:',str(breaks))) # form mapcalc expression and execute exp = self.contourrast+'= if(%s<%s,1)' %(self.elevation, breaks[0]) # for the first, smaller than for b in breaks: exp+='+ if(%s>=%s,1)' %(self.elevation,b) # for all greater eq than grass.mapcalc(exp, overwrite=True) grass.message(('Calculated contourmap: %s' %self.contourrast)) return
def main(): infile = options['input'] value = options['value'] mode = options['mode'] outfile = options['output'] global method method = options['method'] clumped = flags['c'] diagonal = flags['d'] # check for unsupported locations in_proj = grass.parse_command('g.proj', flags='g') if in_proj['unit'].lower() == 'degree': grass.fatal(_("Latitude-longitude locations are not supported")) if in_proj['name'].lower() == 'xy_location_unprojected': grass.fatal(_("xy-locations are not supported")) # check lesser and greater parameters limit = float(value) if mode == 'greater' and method == 'rmarea': grass.fatal(_("You have to specify mode='lesser' with method='rmarea'")) if not grass.find_file(infile)['name']: grass.fatal(_("Raster map <%s> not found") % infile) if method == 'reclass': reclass(infile, outfile, limit, clumped, diagonal, mode == 'lesser') elif method == 'rmarea': rmarea(infile, outfile, limit, in_proj['meters']) grass.message(_("Generating output raster map <%s>...") % outfile)
def main(): """ RichDEM flat resolution: give a gentle slope """ # lazy import RICHDEM try: import richdem as rd except: g.message( flags="e", message=("RichDEM not detected. Install pip3 and " + "then type at the command prompt: " + '"pip3 install richdem".'), ) _input = options["input"] _output = options["output"] # Check for overwrite _rasters = np.array(gscript.parse_command("g.list", type="raster").keys()) if (_rasters == _output).any(): g.message(flags="e", message="output would overwrite " + _output) dem = garray.array(_input, null=np.nan) rd_input = rd.rdarray(dem, no_data=np.nan) rd_output = rd.ResolveFlats(rd_input) dem[:] = rd_output[:] dem.write(_output, overwrite=gscript.overwrite())
def get_size(vector): tmpvector = "tmp_getsize_%s" % str(os.getpid()) rm_vectors.append(tmpvector) grass.run_command("g.copy", vector="%s,%s" % (vector, tmpvector), overwrite=True, quiet=True) if len(grass.vector_db(tmpvector)) == 0: grass.run_command("v.db.addtable", map=tmpvector, quiet=True) grass.run_command("v.db.addcolumn", map=tmpvector, columns="tmparea DOUBLE PRECISION", quiet=True) grass.run_command( "v.to.db", map=tmpvector, columns="tmparea", option="area", units="meters", quiet=True, overwrite=True, ) sizeselected = grass.parse_command("v.db.select", map=tmpvector, flags="v") sizesstr = [ x.split("|")[1:] for x in sizeselected if x.startswith("tmparea|") ][0] sizes = [float(x) for x in sizesstr] return sum(sizes)
def get_size(vector): tmpvector = 'tmp_getsize_%s' % str(os.getpid()) rm_vectors.append(tmpvector) grass.run_command('g.copy', vector="%s,%s" % (vector, tmpvector), quiet=True) if len(grass.vector_db(tmpvector)) == 0: grass.run_command('v.db.addtable', map=tmpvector, quiet=True) grass.run_command('v.db.addcolumn', map=tmpvector, columns="tmparea DOUBLE PRECISION", quiet=True) grass.run_command('v.to.db', map=tmpvector, columns='tmparea', option='area', units='meters', quiet=True, overwrite=True) sizeselected = grass.parse_command('v.db.select', map=tmpvector, flags="v") sizesstr = [ x.split('|')[1:] for x in sizeselected if x.startswith('tmparea|') ][0] sizes = [float(x) for x in sizesstr] return sum(sizes)
def preprocess(reflbandnames, panbandnames, dataset, outputraster, scene): options, flags = grass.parser() pansuffix = ['red', 'green', 'blue'] print os.environ importregex = re.compile('.*[.]TIF') counter = 1 for file in os.listdir(dataset): if re.search(importregex, file): if len(file) == 29: num = file[23] + file[24] else: num = file[23] read2_command('r.external', input=dataset + '/' + file, output='B' + num, overwrite=True, flags='e') counter = counter + 1 for file in os.listdir(dataset): if fnmatch.fnmatch(file, '*.txt'): mtl = file metfile = os.path.join(dataset, mtl) read2_command('i.landsat.toar', input='B', output='B_refl', metfile=metfile, sensor='oli8', overwrite=True) print('reflectance calculated') read2_command('r.colors', map=reflbandnames, flags='e', color='grey') print('histograms equalized') read2_command('i.colors.enhance', red=reflbandnames[0], green=reflbandnames[1], blue=reflbandnames[2]) print('colors enhanced') # pansharpen read2_command('i.fusion.brovey', ms3=reflbandnames[0], ms2=reflbandnames[1], ms1=reflbandnames[2], pan=panbandnames[3], overwrite=True, flags='l', output_prefix='brov') pannames = ['brov.' + s for s in pansuffix] pannames255 = [s + '_255' for s in pannames] print('pansharpening and composition achieved') read2_command('g.region', raster=pannames) read2_command('r.colors', map=pannames, flags='e', color='grey') for raster in pannames: minmax = grass.parse_command('r.info', map=raster, flags='r') print(minmax) newrast = raster + '_255' grass.write_command('r.recode', input=raster, output=newrast, rules='-', stdin=minmax[u'min'] + ':' + minmax[u'max'] + ':0:255', overwrite=True) print('rasters recoded to CELL type') # equalize colors once again read2_command('r.colors', map=[pannames255[0], pannames255[1], pannames255[2]], flags='e', color='grey') read2_command('i.colors.enhance', red=pannames255[0], green=pannames255[1], blue=pannames255[2]) #read2_command('r.composite', red=pannames[0], green=pannames[1], blue=pannames[2], output='comp', # overwrite=True) # create imagery group read2_command('i.group', group='pangroup876', subgroup='pangroup876', input=pannames255) print('created imagery group') read2_command('r.out.gdal', input='pangroup876', output=outputraster, overwrite=True, format='GTiff', type='Int32', flags='f')
def main(): vector = options['input'] output = options['output'] # JL Handling Invalid transect_spacing parameter try: transect_spacing = float(options['transect_spacing']) except: grass.fatal(_("Invalid transect_spacing value.")) if transect_spacing == 0.0: grass.fatal(_("Zero invalid transect_spacing value.")) dleft = options['dleft'] dright = options['dright'] shape = options['type'] print dleft, transect_spacing if not dleft: dleft = transect_spacing else: # JL Handling Invalid dleft parameter try: dleft = float(dleft) except: grass.fatal(_("Invalid dleft value.")) if not dright: dright = transect_spacing else: # JL Handling Invalid dright parameter try: dright = float(dright) except: grass.fatal(_("Invalid dright value.")) # check if input file does not exists if not grass.find_file(vector, element='vector')['file']: grass.fatal(_("<%s> does not exist.") % vector) # check if output file exists if grass.find_file(output, element='vector')['mapset'] == grass.gisenv()['MAPSET']: if not grass.overwrite(): grass.fatal(_("output map <%s> exists") % output) #JL Is the vector a line and does if have at least one feature? info = grass.parse_command('v.info', flags='t', map=vector) if info['lines'] == '0': grass.fatal(_("vector <%s> does not contain lines") % vector) ################################# v = loadVector(vector) transect_locs = get_transects_locs(v, transect_spacing) temp_map = tempmap() if shape == 'line' or not shape: transect_ends = get_transect_ends(transect_locs, dleft, dright) writeTransects(transect_ends, temp_map) elif shape == 'area': transect_ends = get_transect_ends(transect_locs, dleft, dright) writeQuads(transect_ends, temp_map) else: writePoints(transect_locs, temp_map) grass.run_command('v.category', input=temp_map, output=output, type=shape) grass.run_command('g.remove', vect=temp_map)
def get_time_steps(): indexmaps = grass.parse_command('g.list', type='vect', patt='drainage_basins_??????').keys() indexmaps = sorted(indexmaps)[::-1] ages = [] for indexmap in indexmaps: ages.append( str(indexmap.split('_')[-1]) ) #ages = ages[-25:] # TEMPORARY HACK-EY TO FINISH G12 return ages
def main(): """ Compute cell areas """ projinfo = grass.parse_command("g.proj", flags="g") options, flags = grass.parser() output = options["output"] units = options["units"] # First check if output exists if len(grass.parse_command("g.list", type="rast", pattern=options["output"])): if not grass.overwrite(): grass.fatal( "Raster map '" + options["output"] + "' already exists. Use '--o' to overwrite." ) projunits = str(projinfo["units"]) # Unicode to str # Then compute if (projunits == "meters") or (projunits == "Meters"): if units == "m2": grass.mapcalc(output + " = nsres() * ewres()") elif units == "km2": grass.mapcalc(output + " = nsres() * ewres() / 10.^6") elif (projunits == "degrees") or (projunits == "Degrees"): if units == "m2": grass.mapcalc( output + " = ( 111195. * nsres() ) * \ ( ewres() * " + str(np.pi / 180.0) + " * 6371000. * cos(y()) )" ) elif units == "km2": grass.mapcalc( output + " = ( 111.195 * nsres() ) * \ ( ewres() * " + str(np.pi / 180.0) + " * 6371. * cos(y()) )" ) else: print("Units: ", projunits, " not currently supported")
def render_2d(envs): # set rendering parameters brighten = 0 # percent brightness of shaded relief render_multiplier = 1 # multiplier for rendering size whitespace = 1.5 fontsize = 36 * render_multiplier # legend font size legend_coord = (10, 50, 1, 4) # legend display coordinates zscale = 1 # create rendering directory render = os.path.join(gisdbase, location, 'rendering') if not os.path.exists(render): os.makedirs(render) for mapset in simulations: # change mapset gscript.read_command('g.mapset', mapset=mapset, location=location) info = gscript.parse_command('r.info', map='elevation', flags='g') width = int(info.cols) * render_multiplier * whitespace height = int(info.rows) * render_multiplier # render net difference gscript.run_command('d.mon', start=driver, width=width, height=height, output=os.path.join( render, mapset + '_' + 'net_difference' + '.png'), overwrite=1) gscript.write_command('r.colors', map='net_difference', rules='-', stdin=difference_colors) gscript.run_command('r.relief', input='elevation', output='relief', altitude=90, azimuth=45, zscale=zscale, env=envs[mapset]) gscript.run_command('d.shade', shade='relief', color='net_difference', brighten=brighten) gscript.run_command('d.legend', raster='net_difference', fontsize=fontsize, at=legend_coord) gscript.run_command('d.mon', stop=driver) try: # stop cairo monitor gscript.run_command('d.mon', stop=driver) except CalledModuleError: pass
def get_autocorrelation(mapname, raster, neighbordict, method): """Calculate either Moran's I or Geary's C for values of the given raster""" raster_vars = gscript.parse_command("r.univar", map_=raster, flags="g", quiet=True) global_mean = float(raster_vars["mean"]) univar_res = gscript.read_command( "r.univar", flags="t", map_=raster, zones=mapname, out="-", sep="comma", quiet=True, ) means = {} mean_diffs = {} firstline = True for line in univar_res.splitlines(): l = line.split(",") if firstline: i = l.index("mean") firstline = False else: means[l[0]] = float(l[i]) mean_diffs[l[0]] = float(l[i]) - global_mean sum_sq_mean_diffs = sum(x**2 for x in mean_diffs.values()) total_nb_neighbors = 0 for region in neighbordict: total_nb_neighbors += len(neighbordict[region]) N = len(means) sum_products = 0 sum_squared_differences = 0 for region in neighbordict: region_value = means[region] - global_mean neighbors = neighbordict[region] nb_neighbors = len(neighbors) for neighbor in neighbors: neighbor_value = means[neighbor] - global_mean sum_products += region_value * neighbor_value sum_squared_differences = (means[region] - means[neighbor])**2 if method == "moran": autocor = (float(N) / total_nb_neighbors) * (float(sum_products) / sum_sq_mean_diffs) elif method == "geary": autocor = (float(N - 1) / (2 * total_nb_neighbors)) * ( float(sum_squared_differences) / sum_sq_mean_diffs) return autocor
def comp_rasters(directory, cleanup=False, png=False): for item in os.listdir(directory): path = os.path.join(directory, item) if not os.path.isdir(path) or path.endswith('gdb'): continue ds = gdal.Open(path) if ds is None: continue # blacklist if item in ['fl_dir']: continue # link arcgis raster to GRASS arcgis = '{}_arcgis'.format(item) gs.run_command('r.external', input=path, output=arcgis, flags='o' ) gs.run_command('r.colors', map=arcgis, raster=item ) gs.run_command('g.region', raster=item ) diff = '{}_diff'.format(item) gs.run_command('r.mapcalc', expression='{} = {} - {}'.format(diff, arcgis, item) ) gs.run_command('r.colors', map=diff, color='diff' ) if png: gs.run_command('d.mon', start='cairo', output=os.path.join(OUTPUT, '{}.png'.format(item)) ) gs.run_command('d.rast.leg', map=diff ) gs.run_command('d.mon', stop='cairo' ) print('{sep}{nl}{map}{nl}{sep}{nl}'.format(sep='-' * 80, nl=os.linesep, map=item)) stats = gs.parse_command('r.univar', flags='g', map=diff ) for key in ('min', 'max', 'range', 'mean'): print ('{}={}'.format(key, stats[key])) if abs(float(stats['mean'])) < 1e-4: print ("-> OK") else: print ("-> KO") if cleanup: gs.run_command('g.remove', type='raster', name=','.join(arcgis, diff), flags='f' )
def get_maxim_value(self): ''' Getting the maximum patch size of the map This is not working for some reason, but the overall script is working ''' stats = grass.parse_command('r.univar', map=self.patch_map, flags='g') self.raster_max = int(stats['max'])
def reg_2deg(input = '', size = 2): # get region center reg = grass.parse_command('g.region', input = input, flags = 'c') lat = float() lon = float() g.region(n = lat + size/2, s = lat - size/2, w = lon - size/2, e = lon + size/2, align = input, flags = 'ap')
def main(): # Get the options _input = options["input"] output = options["output"] expression = options["expression"] base = options["basename"] method = options["method"] nprocs = int(options["nprocs"]) register_null = flags["n"] spatial = flags["s"] new_flags = "" if register_null: new_flags = "n" if spatial: new_flags = new_flags + "s" # get list of bands available in the input strds t_info = grass.parse_command('t.info', input=_input, flags='g') input_bands = t_info["semantic_labels"].split(',') nbands = len(input_bands) # find needed bands in formula: if "data[0]" in formula: # go through the list of bands and replace (str.replace(old, new) # in the formula e.g. data[0] with (input_strds + '.' + bandref[0]) counter = 0 new_inputs = [] band_used = [False for i in range(nbands)] while counter < nbands: fstr = "data[" + str(counter) + "]" bandname = input_bands[counter] data_band = ("data.%s") % (bandname) if fstr in expression: band_used[counter] = True newstr = ("%s.%s") % (_input, bandname) new_inputs.append(newstr) expression = expression.replace(fstr, newstr) elif data_band in expression: band_used[counter] = True newstr = ("%s.%s") % (_input, bandname) new_inputs.append(newstr) expression = expression.replace(data_band, newstr) else: band_used[counter] = False counter = counter + 1 # print(expression) grass.run_command('t.rast.mapcalc', inputs=(',').join(new_inputs), expression=expression, method=method, output=output, basename=base, nprocs=nprocs, flags=new_flags)
def run_view(scanned_elev, blender_path, env, **kwargs): # regression group = 'color' before = 'scan_saved' elev_threshold = 20 color_threshold = 150 dist_threshold = 30 change = 'change' arrow = 'arrow' arrow3d = 'arrow3d' arrow_final = 'arrow_final' ff = gscript.find_file(name=arrow_final, element='vector') old_points = [] if ff and ff['fullname']: old_p = gscript.read_command('v.out.ascii', input=ff['fullname'], format='standard', type='line', env=env).strip().splitlines() for op in old_p: line = op.strip().split() if line == ['1', '1']: continue try: x = float(line[0]) y = float(line[1]) z = float(line[2]) old_points.append((x, y, z)) except ValueError: continue except IndexError as e: print (line) print (e) continue reg_params = gscript.parse_command('r.regression.line', flags='g', mapx=before, mapy=scanned_elev, env=env) gscript.mapcalc(exp='{new} = if(({a} + {b} * {after}) - {before} > {thr}, 1, null())'.format(a=reg_params['a'], b=reg_params['b'], after=scanned_elev, before=before, thr=elev_threshold, new=change), env=env) #gscript.parse_command('r.univar', map=[group + '_r', group + '_g', group + '_b'], output=, flags='t', zones=, env=env) gscript.mapcalc(exp='{new} = if({change} && ({r} + {g} + {b}) / 3. >= {th}, 1, 2)'.format(new=arrow, change=change, th=color_threshold, r=group + '_r', g=group + '_g', b=group + '_b'), env=env) gscript.run_command('r.volume', input=arrow, clump=arrow, centroids=arrow, env=env) gscript.run_command('v.drape', input=arrow, output=arrow3d, elevation=before, method='bilinear', env=env) points = gscript.read_command('v.out.ascii', input=arrow3d, env=env).strip() if points: new_points = [] linetext = 'L 2 1\n' for p in points.splitlines(): x, y, z, c = p.split('|') new_points.append((float(x), float(y), float(z))) linetext += '{} {} {}\n'.format(x, y, z) linetext += '1 1\n' # compare old and new if not old_points or (dist(old_points[0], new_points[0]) > dist_threshold or dist(old_points[1], new_points[1]) > dist_threshold): print ('write') gscript.write_command('v.in.ascii', stdin=linetext, input='-', output=arrow_final, format='standard', flags='zn', env=env) blender_export_vector(vector=arrow_final, name='vantage', z=True, vtype='line', path=blender_path, time_suffix=False, env=env)
def total_light_index(ntl_image): """ Evaluation index (TLI) which represents the sum of grey values in an area. """ univar = grass.parse_command("r.univar", map=ntl_image, flags='g', parse=(grass.parse_key_val, {'sep': '='})) return float(univar['sum'])
def epsg_location(request, response): """Check whether the EPSG of a mapset corresponds the specified one.""" from grass.script import parse_command g_proj = parse_command('g.proj', flags='g') assert g_proj['epsg'] == '5514', \ 'Error in creating a GRASS location based on an EPSG code' return response
def file_location(request, response): """Check whether the datum of a mapset corresponds the file one.""" from grass.script import parse_command g_proj = parse_command('g.proj', flags='g') assert g_proj['datum'] == 'wgs84', \ 'Error in creating a GRASS location based on a file' return response
def isExists(FeatNam,FeatType): # 'Delete temporal objects.' result = False vlist = dict.keys(g.parse_command('db.tables',flags='p')) for i in range(len(vlist)): # Delete the final result if exists. if vlist[i] == 'public.'+ str(FeatNam): result = True print(result) return(result)
def Calibrate(self, event): res = gscript.parse_command('r.in.kinect', flags='c', overwrite=True) if not (res['calib_matrix'] and len(res['calib_matrix'].split(',')) == 9): gscript.message(_("Failed to calibrate")) return self.settings['tangible']['calibration']['matrix'] = res['calib_matrix'] UserSettings.SaveToFile(self.settings) # update self.calib_matrix = res['calib_matrix']
def rusle(self): """!main method in rusle_base controlling the whole process, once called by main() @return soillossbare name of output raster map """ flowacc = outprefix + "flowacc" slope = outprefix + "slope" lsfactor = outprefix + "lsfactor" self.tmp_rast.append(flowacc) self.tmp_rast.append(slope) self.tmp_rast.append(lsfactor) global fieldblock if not fieldblock: if fieldblockvect: fieldblock = outprefix + "fieldblock" g.run_command( "v.to.rast", input=fieldblockvect, output=fieldblock, use="val", value="1", quiet=quiet, ) if fieldblock: g.verbose('Raster map fieldblock is in "%s"' % fieldblock) else: fieldblock = "" if not options["flowacc"]: self._getFlowacc(elevation, flowacc, fieldblock) g.verbose('Raster map flowacc is in "%s".' % flowacc) else: g.verbose('Raster map flowacc taken from "%s".' % flowacc) self._getSlope(elevation, slope) g.verbose('Raster map slope is in "%s"' % slope) self._getLsfac(flowacc, slope, lsfactor) g.verbose('Raster map lsfactor is in "%s"' % lsfactor) self._getSoillossbare(lsfactor, kfactor, rfactor, soillossbare) g.message('Soilloss for bare soil in map "%s".' % soillossbare) stats = g.parse_command("r.univar", flags="g", map=soillossbare, delimiter="=") g.message("mean = %s \n stddev = %s \n min = %s \n max = %s" % (stats["mean"], stats["stddev"], stats["min"], stats["max"])) return soillossbare
def get_autocorrelation(mapname, raster, neighbordict, indicator): """ Calculate either Moran's I or Geary's C for values of the given raster """ raster_vars = gscript.parse_command('r.univar', map_=raster, flags='g', quiet=True) global_mean = float(raster_vars['mean']) univar_res = gscript.read_command('r.univar', flags='t', map_=raster, zones=mapname, out='-', sep='comma', quiet=True) means = {} mean_diffs = {} firstline = True for line in univar_res.splitlines(): l = line.split(',') if firstline: i = l.index('mean') firstline = False else: means[l[0]] = float(l[i]) mean_diffs[l[0]] = float(l[i]) - global_mean sum_sq_mean_diffs = sum(x**2 for x in mean_diffs.values()) total_nb_neighbors = 0 for region in neighbordict: total_nb_neighbors += len(neighbordict[region]) N = len(means) sum_products = 0 sum_squared_differences = 0 for region in neighbordict: region_value = means[region] - global_mean neighbors = neighbordict[region] nb_neighbors = len(neighbors) for neighbor in neighbors: neighbor_value = means[neighbor] - global_mean sum_products += region_value * neighbor_value sum_squared_differences = (means[region] - means[neighbor])**2 if indicator == 'morans': autocor = ((float(N) / total_nb_neighbors) * (float(sum_products) / sum_sq_mean_diffs)) elif indicator == 'geary': autocor = (float(N - 1) / (2 * total_nb_neighbors)) * \ (float(sum_squared_differences) / sum_sq_mean_diffs) return autocor
def get_aoi(vector=None): args = {} if vector: args['input'] = vector if gs.vector_info_topo(vector)['areas'] <= 0: gs.fatal(_("No areas found in AOI map <{}>...").format(vector)) elif gs.vector_info_topo(vector)['areas'] > 1: gs.warning(_("More than one area found in AOI map <{}>. \ Using only the first area...").format(vector)) # are we in LatLong location? s = gs.read_command("g.proj", flags='j') kv = gs.parse_key_val(s) if '+proj' not in kv: gs.fatal(_("Unable to get AOI: unprojected location not supported")) geom_dict = gs.parse_command('v.out.ascii', format='wkt', **args) num_vertices = len(str(geom_dict.keys()).split(',')) geom = [key for key in geom_dict][0] if kv['+proj'] != 'longlat': gs.message(_("Generating WKT from AOI map ({} vertices)...").format(num_vertices)) if num_vertices > 500: gs.fatal(_("AOI map has too many vertices to be sent via HTTP GET (sentinelsat). \ Use 'v.generalize' to simplify the boundaries")) coords = geom.replace('POLYGON((', '').replace('))', '').split(', ') poly = 'POLYGON((' poly_coords = [] for coord in coords: coord_latlon = gs.parse_command( 'm.proj', coordinates=coord.replace(' ', ','), flags='od') for key in coord_latlon: poly_coords.append((' ').join(key.split('|')[0:2])) poly += (', ').join(poly_coords) + '))' # Note: poly must be < 2000 chars incl. sentinelsat request (see RFC 2616 HTTP/1.1) if len(poly) > 1850: gs.fatal(_("AOI map has too many vertices to be sent via HTTP GET (sentinelsat). \ Use 'v.generalize' to simplify the boundaries")) else: gs.message(_("Sending WKT from AOI map to ESA...")) return poly else: return geom
def test_patching_backend(tmp_path, patch_backend): """Check patching backend works""" location = "test" gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access with grass_setup.init(tmp_path / location): gs.run_command("g.region", s=0, n=50, w=0, e=50, res=1) points = "points" reference = "reference" gs.run_command("v.random", output=points, npoints=100) gs.run_command("v.to.rast", input=points, output=reference, type="point", use="cat") def run_grid_module(): # modules/shortcuts calls get_commands which requires GISBASE. # pylint: disable=import-outside-toplevel from grass.pygrass.modules.grid import GridModule grid = GridModule( "v.to.rast", width=10, height=5, overlap=0, patch_backend=patch_backend, processes=max_processes(), input=points, output="output", type="point", use="cat", ) grid.run() run_in_subprocess(run_grid_module) mean_ref = float( gs.parse_command("r.univar", map=reference, flags="g")["mean"]) mean = float( gs.parse_command("r.univar", map="output", flags="g")["mean"]) assert abs(mean - mean_ref) < 0.0001
def getValOnBuffer(inmap,elev,dist,output,user="",password=""): # get number of points n = getPointsNum(inmap) g.run_command('v.db.addcol', map=outmap, columns='pix_vals double array', quiet=True) #g.run_command('v.db.addcol', map=outmap, columns='fft_real double array', quiet=True) #g.run_command('v.db.addcol', map=outmap, columns='fft_imgn double array', quiet=True) # Connect to db server dbinfo = getDbInfo() dbname=dbinfo['dbname'] driver=dbinfo['driver'] conn = ppg.connect("dbname='"+str(dbname)+"' user='******' host='"+str(host)+"' password='******'") cur = conn.cursor() for i in range(n-1): print "Now processing:" + str(i+1) +":" + nams.keys()[i] # extract one feature from points set with cat value. g.run_command("v.extract",input=inmap,output="tmp",type="point",new=-1,list=i+1,quiet=True,overwrite=True) # create buffer of the extracted point g.run_command("v.buffer",input="tmp",output="tmp2",type="point",distance=dist,quiet=True,overwrite=True) # add attributes table and new a column storing value for the buffer object. g.run_command("v.db.addtable",map="tmp2",columns="val double precision",quiet=True) # set default value as 1. g.run_command("v.db.update",map="tmp2",column="val",value=1,quiet=True) # Redefine geographic region for procedure. g.run_command("g.region",vect="tmp2",quiet=True) # convert vector map buffer to raster map buffer. g.run_command("v.to.rast",input="tmp2",output="tmprast",col="val",overwrite=True,quiet=True) # get elevation on the raster buffer. g.run_command("r.mapcalculator",amap="tmprast",bmap=elev,formula="A*B",outfile="tmprast2",overwrite=True,quiet=True) nam = i+1 pix = g.parse_command("r.stats",flags="1",input="tmprast2",nv="",fs=",",quiet=True) res = 'ARRAY[' for j in pix.keys(): res = res + str(j) + ',' res = res + ']' # Update the column sql='UPDATE '+str(inmap)+' SET pix_vals = '+str(pix_vals)+' WHERE cat = '+str(i+1) cur.execute(sql) # Remove temporal features g.parse_command("g.remove",vect="tmp,tmp2",rast="tmprast,tmprast2",quiet=True) # Set region with DEM. g.parse_command("g.region",rast=elv,quiet=True) conn.close()
def Data_prep(Land_cover): ''' Function that extracts informations from the Land Cover : resolution and classes ''' info = gscript.raster_info(Land_cover) nsres = info.nsres ewres = info.ewres L = [] L = [cl for cl in gscript.parse_command('r.category', map=Land_cover)] return nsres, ewres, L
def D_index(n1, n2, v1, v2, txtf): """Calculate D (Schoener's 1968)""" tmpf0 = tmpname("rniche") gs.mapcalc("$tmpf0 = abs(double($n1)/$v1 - double($n2)/$v2)", tmpf0=tmpf0, n1=n1, v1=v1, n2=n2, v2=v2, quiet=True) NO = float(gs.parse_command("r.univar", quiet=True, flags="g", map=tmpf0)['sum']) NOV = 1 - (0.5 * NO) gs.info(_("Niche overlap (D) of {} and {} {}").format(n1.split('@')[0], n2.split('@')[0], round(NOV, 3))) return ['Niche overlap (D)', n1, n2, NOV]
def mkstreams(self): '''Create minimal stream network reaching all subbasins and with nice main streams''' # get max accumulation and cell count for each subbasin rsmaxaccum = gread('r.stats', input='maxaccum__', flags='lcn') rsmaxaccum = np.array(rsmaxaccum.split(), dtype=float).reshape((-1, 3)).astype(int) subbasinIDs = rsmaxaccum[:, 0] maxaccum = rsmaxaccum[:, 1] # cellcounts must not be larger than maxaccum (may happen in subbasins with more than 1 outlet) cellcounts = np.min([rsmaxaccum[:, 2], maxaccum], axis=0) # calculate optima accumulation for nice headwater mainstreams accr = grass.parse_command('r.info', map=self.accumulation, flags='g') minaccum = np.int32(round(float(self.minmainstreams)*1e6 / (float(accr['nsres'])*float(accr['ewres'])))) minaccum = np.ones_like(maxaccum) * minaccum optiaccum = np.min([maxaccum-np.int32(cellcounts*0.1), minaccum], axis=0) # check incoming subbasins maxaccum and take the smallest accumulation to update optiaccum subnext = readSubNxtID(self.outlets) optiaccum = dict(zip(subbasinIDs,optiaccum)) maxaccum = dict(zip(subbasinIDs,maxaccum)) for sn in np.unique(subnext['nextID']): if sn < 0: continue for sb in subnext[subnext['nextID']==sn]['subbasinID']: optiaccum[sn] = min(optiaccum[sn],maxaccum[sb]-1) # make raster tempf = grass.tempfile() np.savetxt(tempf,optiaccum.items(),fmt='%i=%i') grass.run_command('r.reclass',input='maxaccum__',output='optiaccum__', rules=tempf,quiet=True) # get accumulation and make lines grass.mapcalc("{0}__unthin=if({1} > {2},{1},null())".format(self.mainstreams, self.accumulation,'optiaccum__'),overwrite=True) # make sure all pixels between outlets and inlets are included #grun('v.to.rast',input=self.outletinletlines,output='routingnet__rast', # use='val',type='line',quiet=True) #grass.mapcalc("{0}=if(isnull({0})&~isnull({1}),{2},{0})".format(self.mainstreams, # 'routingnet__rast',self.accumulation),overwrite=True) grun('r.thin', input=self.mainstreams+'__unthin', output=self.mainstreams, overwrite=True, quiet=True) # may exclude outlet/inlet points # use predefined streams for subbasins that have them if 'streams' in self.options: grun('v.to.rast',input=self.streams,output='stream__rast', use='val',type='line',val=1,quiet=True) grun('r.thin', input='stream__rast', output='stream__rast__thin', overwrite=True, quiet=True) grun('r.stats.zonal',base=self.subbasinrast,cover='stream__rast__thin', method='sum',output='n__streams__subbasins',quiet=True,overwrite=True) # get subbasins that have at least x cells of streams grass.mapcalc('{0}=if(n__streams__subbasins>10,{1},{0})'.format(self.mainstreams,'stream__rast__thin'), overwrite=True) # make final vector grun('r.to.vect', input=self.mainstreams, output=self.mainstreams, type='line',quiet=True) return
def header(fd): import grass.script.core as grass fd.write('<?xml version="1.0" encoding="UTF-8"?>\n') fd.write('<!DOCTYPE task SYSTEM "grass-addons.dtd">\n') # TODO # doesn't work in GRASS 6 # vInfo = grass.version() vInfo = grass.parse_command('g.version', flags='g') fd.write('<addons version="%s" revision="%s" date="%s">\n' % \ (vInfo['version'].split('.')[0], vInfo['revision'], datetime.now()))
def getQu(FeatNam,colname): # get Calculated Values from the Feature table. dt = g.parse_command('v.db.select',flags='c', map=FeatNam ,columns=colname) dist = [] n=len(dt) for i in range(n-1): dist=dist + [float(dict.keys(dt)[i])] # calculate quantile of distance to the nearest point. r_dist = ro.FloatVector(dist) r_summ = r_base.summary(r_dist) return(r_summ) dt = g.parse_command('v.db.select',flags='c', map=FeatNam ,columns=colname) dist = [] n=len(dt) for i in range(n-1): dist=dist + [float(dict.keys(dt)[i])] # calculate quantile of distance to the nearest point. r_dist = ro.FloatVector(dist) r_summ = r_base.summary(r_dist) return(r_summ)
def render_2d(envs): brighten = 0 # percent brightness of shaded relief render_multiplier = 1 # multiplier for rendering size whitespace = 1.5 # canvas width relative to map for legend fontsize = 36 * render_multiplier # legend font size legend_coord = (5, 45, 2, 5) # legend display coordinates zscale = 1 # vertical exaggeration # create rendering directory render = os.path.join(gisdbase, location, 'rendering') if not os.path.exists(render): os.makedirs(render) for mapset in simulations: # change mapset gscript.read_command('g.mapset', mapset=mapset, location=location) # set region gscript.run_command('g.region', rast=region, res=res) # set render size info = gscript.parse_command('r.info', map='elevation', flags='g') width = int(info.cols)*render_multiplier*whitespace height = int(info.rows)*render_multiplier # render net difference gscript.run_command('d.mon', start=driver, width=width, height=height, output=os.path.join(render, mapset+'_'+'net_difference'+'.png'), overwrite=1) gscript.run_command('r.relief', input='elevation', output='relief', altitude=90, azimuth=45, zscale=zscale, env=envs[mapset]) gscript.run_command('d.shade', shade='relief', color='net_difference', brighten=brighten) gscript.run_command('d.legend', raster='net_difference', fontsize=fontsize, at=legend_coord) gscript.run_command('d.mon', stop=driver)
def parse_feature_names(maps): """Expand all categorical variable raster names into dummy variable raster names""" # take the dummy variable maps, not the categorical result = maps for i in ['lcc','feature']: if i in maps: dummies = gscript.parse_command('g.list', type='rast', pattern='{}_*'.format(i)).keys() result.remove(i) result += dummies return result
def rusle(self): """!main method in rusle_base controlling the whole process, once called by main() @return soillossbare name of output raster map """ flowacc = outprefix + 'flowacc' slope = outprefix + 'slope' lsfactor = outprefix+ 'lsfactor' #self.tmp_rast.append(flowacc) #self.tmp_rast.append(slope) #self.tmp_rast.append(lsfactor) global fieldblock if not fieldblock: if fieldblockvect: fieldblock = outprefix + "fieldblock" gscript.run_command("v.to.rast", input=fieldblockvect, output= fieldblock, use="val", value="1", quiet=quiet ) if fieldblock: gscript.verbose('Raster map fieldblock is in "%s"'%fieldblock) else: fieldblock = "" if not options['flowacc']: self._getFlowacc(elevation,flowacc,fieldblock) gscript.verbose('Raster map flowacc is in "%s".'%flowacc) else: gscript.verbose('Raster map flowacc taken from "%s".'%flowacc) self._getSlope(elevation,slope) gscript.verbose('Raster map slope is in "%s"'%slope) self._getLsfac(flowacc,slope,lsfactor) gscript.verbose('Raster map lsfactor is in "%s"'%lsfactor) self._getSoillossbare(lsfactor,kfactor,rfactor,soillossbare) gscript.message('Soilloss for bare soil in map "%s".' %soillossbare) stats = gscript.parse_command('r.univar', flags="g", map=soillossbare, delimiter = '=') gscript.message('mean = %s \n stddev = %s \n min = %s \n max = %s' % (stats['mean'],stats['stddev'], stats['min'], stats['max'])) return soillossbare
def Analyze(self): if self.hasSuperpixels: gscript.run_command('i.superpixels.slic', group=self.group, output=self.segment, compactness=2, minsize=50, env=self.env) else: gscript.run_command('i.segment', group=self.group, output=self.segment, threshold=0.3, minsize=50, env=self.env) gscript.run_command('r.clump', input=self.segment, output=self.segment_clump, env=self.env) gscript.run_command('i.smap', group=self.group, subgroup=self.group, signaturefile=self.signature, output=self.classification, goodness=self.reject, env=self.env) percentile = float(gscript.parse_command('r.univar', flags='ge', map=self.reject, env=self.env)['percentile_90']) gscript.mapcalc('{new} = if({classif} < {thres}, {classif}, null())'.format(new=self.filtered_classification, classif=self.classification, thres=percentile), env=self.env) segments = self.segment if self.hasSuperpixels else self.segment_clump gscript.run_command('r.stats.quantile', base=segments, cover=self.filtered_classification, output=self.output, env=self.env)
def map_info(landscape): info = grass.parse_command('r.info', map=landscape, flags='g') # aqui deveriamos checar se a resolucao eh inteira... como fazer isso? dim = [int(info['rows']), int(info['cols'])] x_west = int(info['west']) x_east = int(info['east']) y_south = int(info['south']) y_north = int(info['north']) res_x = int(info['ewres']) res_y = int(info['nsres']) print 'dims = %d, %d; limits W-E: %d - %d, S-N: %d - %d; res = %d, %d' % (dim[0], dim[1], x_west, x_east, y_south, y_north, res_x, res_y) return dim, x_west, x_east, y_south, y_north, res_x, res_y
def __init__(self,**optionsandflags): '''Process all arguments and prepare processing''' # add all options and flags as attributes (only nonempty ones) self.options = {} for o in optionsandflags: if optionsandflags[o]!='': self.options[o] = optionsandflags[o] self.__dict__.update(self.options) # get some location infos self.env=grass.gisenv() self.region=grass.region() self.proj=grass.parse_command('g.proj',flags='g') # convert res self.res = float(self.res) return
def map_info(landscape, usegrid = False, x_center = '', y_center = '', dims = ''): ''' This function extracts information (extent, resolutin/grain, number of rows and columns) from the landscape, directly from the GRASS GIS DataBase Input: - landscape: name of the landscape (it may the HABMAT landscape - binary) Output: - dim: (nrows, ncols) - number of rows and columns of the landscape - x_west: western limit of the map - x_east: eastern limit of the map - y_south: southern limit of the map - y_north: northern limit of the map - res_x: resolution/grain (size of the pixel), in the west-east direction - res_y: resolution/grain (size of the pixel), in the south-north direction ''' #landscape='lndscp_0002_Mapa0002_tif_HABMAT_HABMAT' #if usegrid: #set_region(x_col=x_center, y_row=y_center, dims=dims) #else: #grass.run_command('g.region', raster=landscape) info = grass.parse_command('r.info', map=landscape, flags='g') # aqui deveriamos checar se a resolucao eh inteira... como fazer isso? res_x = int(info['ewres']) res_y = int(info['nsres']) if usegrid: dim = [dims[0]/res_y, dims[1]/res_x] # dims (y,x): in y/rows (meters), in x/cols (meters) x_west = x_center-(dims[1]/2) x_east = x_center+(dims[1]/2) y_south = y_center-(dims[0]/2) y_north = y_center+(dims[0]/2) else: dim = [int(info['rows']), int(info['cols'])] x_west = int(float(info['west'])) x_east = int(float(info['east'])) y_south = int(float(info['south'])) y_north = int(float(info['north'])) #print 'dims = %d, %d; limits W-E: %d - %d, S-N: %d - %d; res = %d, %d' % (dim[0], dim[1], x_west, x_east, y_south, y_north, res_x, res_y) return dim, x_west, x_east, y_south, y_north, res_x, res_y #landscape_name = 'simulation_000001_p029_h059_HABMAT' #dim, x_west, x_east, y_south, y_north, res_x, res_y = map_info(landscape=landscape_name) #print 'dims = %d, %d; limits W-E: %d - %d, S-N: %d - %d; res = %d, %d' % (dim[0], dim[1], x_west, x_east, y_south, y_north, res_x, res_y)
def main(): # process command options input = options['input'] if not gs.find_file(input)['file']: gs.fatal(_("Raster map <%s> not found") % input) smooth = options['output'] if gs.find_file(smooth)['file'] and not gs.overwrite(): gs.fatal(_("Output map <%s> already exists") % smooth) sd = options['sd'] try: sd = float(sd) except ValueError: if not gs.find_file(sd)['file']: gs.fatal(_("Raster map <%s> not found") % sd) alpha = float(options['alpha']) # set aside region for internal use gs.use_temp_region() # subset input if desired region = options.get('region') if region: if not gs.find_file(region)['file']: gs.fatal(_("Raster map <%s> not found") % region) gs.message("Setting region to %s" % region, flag='i') gs.run_command('g.region', rast=region, align=input) else: gs.message("Using existing GRASS region", flag='i') gs.debug('='*50) gs.debug('\n'.join(gs.parse_command('g.region', 'p').keys())) gs.debug('='*50) multiscalesmooth(input, smooth, sd, alpha) # restore original region gs.del_temp_region() return None
def main(): infile = options['input'] lesser = options['lesser'] greater = options['greater'] outfile = options['output'] global METHOD METHOD = options['method'] clumped = flags['c'] diagonal = flags['d'] islesser = False in_proj = grass.parse_command('g.proj', flags='g') # check non supported location if in_proj['unit'].lower() == 'degree': grass.fatal(_("Latitude-Longitude locations are not supported")) if in_proj['name'].lower() == 'xy_location_unprojected': grass.fatal(_("xy-locations are not supported")) grass.fatal(_("Need projected data with grids in metric units")) # check lesser and greater parameters if not lesser and not greater: grass.fatal(_("You have to specify one of lesser= or greater=")) if lesser and greater: grass.fatal(_("lesser= and greater= are mutually exclusive")) if lesser: limit = float(lesser) islesser = True if greater and METHOD == 'rmarea': grass.fatal(_("You have to specify lesser= with method='rmarea'")) elif greater: limit = float(greater) if not grass.find_file(infile)['name']: grass.fatal(_("Raster map <%s> not found") % infile) if METHOD == 'reclass': reclass(infile, outfile, limit, clumped, diagonal, islesser) elif METHOD == 'rmarea': rmarea(infile, outfile, limit, in_proj['meters']) grass.message(_("Generating output raster map <%s>...") % outfile)
def getDbInfo(): dbinfo = g.parse_command('db.connect',flags="p") dbstat = {} for i in range(len(dbinfo)): info = dict.keys(dbinfo)[i].split(":")[0] if info == "driver": e_key = dict.keys(dbinfo)[i].split(":")[0] e_val = dict.keys(dbinfo)[i].split(":")[1] driver=None if e_val == "pg": driver="postgres" dbstat[e_key] = driver for i in range(len(dbinfo)): if not dict.values(dbinfo)[i] == None: elems = dict.values(dbinfo)[i].split(",") for elem in elems: if len(elem.split('=')) > 1: e_key = elem.split('=')[0] e_val = elem.split('=')[1] dbstat[e_key] = e_val return(dbstat)
def CalibrateModelBBox(self, event): if self.IsScanning(): dlg = wx.MessageDialog(self, 'In order to calibrate, please stop scanning process first.', 'Stop scanning', wx.OK | wx.ICON_WARNING) dlg.ShowModal() dlg.Destroy() return params = {} if self.calib_matrix: params['calib_matrix'] = self.calib_matrix params['rotate'] = self.scan['rotation_angle'] zrange = ','.join(self.scan['trim_nsewtb'].split(',')[4:]) params['zrange'] = zrange res = gscript.parse_command('r.in.kinect', flags='m', overwrite=True, **params) if not res['bbox']: gscript.message(_("Failed to find model extent")) offsetcm = 2 n, s, e, w = [int(round(float(each))) for each in res['bbox'].split(',')] self.scanning_panel.trim['n'].SetValue(str(n + offsetcm)) self.scanning_panel.trim['s'].SetValue(str(abs(s) + offsetcm)) self.scanning_panel.trim['e'].SetValue(str(e + offsetcm)) self.scanning_panel.trim['w'].SetValue(str(abs(w) + offsetcm))
def OnRegression(self, event): """Displays regression information in messagebox """ message = [] title = _("Regression Statistics for Scatterplot(s)") for rpair in self.rasterList: if isinstance(rpair, tuple) == False: continue rast1, rast2 = rpair rast1 = rast1.split("@")[0] rast2 = rast2.split("@")[0] ret = grass.parse_command( "r.regression.line", mapx=rast1, mapy=rast2, flags="g", quiet=True, parse=(grass.parse_key_val, {"sep": "="}), ) eqtitle = _("Regression equation for raster map <%(rast1)s> vs. <%(rast2)s>:\n\n") % { "rast1": rast1, "rast2": rast2, } eq = " %s = %s + %s(%s)\n\n" % (rast2, ret["a"], ret["b"], rast1) num = "N = %s\n" % ret["N"] rval = "R = %s\n" % ret["R"] rsq = "R-squared = %f\n" % pow(float(ret["R"]), 2) ftest = "F = %s\n" % ret["F"] str = eqtitle + eq + num + rval + rsq + ftest message.append(str) stats = PlotStatsFrame(self, id=wx.ID_ANY, message=message, title=title) if stats.Show() == wx.ID_CLOSE: stats.Destroy()