def mask_data(): bands1 = grass.list_grouped('raster', pattern='lndcal*.1$')[mapset] count = len(bands1) i = 0 for b1 in bands1: i += 1 oname = b1.split('.')[1] grass.message("Processing <{0}> ({1}/{2})...".format(oname, i, count)) grass.percent(i, count, 5) grass.run_command('g.region', raster=b1) mask = grass.find_file('fmask.' + oname, element='raster')['fullname'] if mask: grass.run_command('r.mask', raster='fmask.' + oname, maskcats=1, overwrite=True, quiet=True) else: grass.warning("Mask missing for <{0}>".format(oname)) bands = grass.list_grouped( 'raster', pattern='lndcal.{0}.*'.format(oname))[mapset] for b in bands: n = b.split('.')[-1] grass.mapcalc('{0}.{1}={2}'.format(oname, n, b), quiet=True, overwrite=True) if mask: grass.run_command('r.mask', flags='r', quiet=True) grass.run_command('g.remove', type='raster', pattern='fmask*,lndcal*', flags='f', quiet=True) grass.percent(0, 0, 0)
def clip_mask_data(maps): mapcalc_module = Module('r.mapcalc', overwrite=True, quiet=True, run_=False) colors_module = Module('r.colors', color='grey', quiet=True, run_=False) grass.message("Clipping/Masking ({} maps)...".format(len(maps))) for name in maps: grass.message('Clipping {}...'.format(name)) band1 = '{}_B1'.format(name) Module('g.copy', raster=['{}_MTLFmask'.format(name),'{}_MTLFmask'.format(name)], quiet=True) Module('r.mask', flags='i', raster='{}_MTLFmask'.format(name), maskcats="2 4", quiet=True) bands = grass.list_grouped('raster', pattern='{}_B*'.format(name))['PERMANENT'] for band in bands: new_mapcalc = copy.deepcopy(mapcalc_module) queue.put(new_mapcalc(expression='clip_{name}={name}'.format(name=band))) queue.wait() Module('r.mask', flags='r', quiet=True) Module('g.remove', quiet=True, flags='f', type='raster', name='{}_MTLFmask'.format(name)) grass.message("Setting up color table ({} maps)...".format(len(maps))) bands = grass.list_grouped('raster', pattern='clip_*')[mapset] for name in bands: new_colors = copy.deepcopy(colors_module) queue.put(new_colors(map=name)) queue.wait() return bands
def mask_data(band_filter, cfmask_filter, cloud_mask_value, file_separator): # do cleanup first grass.run_command('g.remove', type='raster', pattern='*_masked', flags='f', quiet=True) # find band1 raster maps first bands1 = grass.list_grouped('raster', pattern='*{}1*'.format(band_filter))[mapset] count = len(bands1) i = 0 for b1 in bands1: i += 1 basename = b1.split(file_separator)[0] grass.message("Processing <{0}> ({1}/{2})...".format(basename, i, count)) grass.percent(i, count, 5) # set computational region based on first band grass.run_command('g.region', raster=b1) maskname = '{}{}{}'.format(basename, file_separator, cfmask_filter) mask = grass.find_file(maskname, element='raster')['fullname'] # apply cloud mask if found if mask: grass.run_command('r.mask', flags='i', raster=maskname, maskcats=cloud_mask_value, overwrite=True, quiet=True) else: grass.warning("Mask missing for <{}>".format(basename)) # create copy of band with mask applied bands = grass.list_grouped('raster', pattern='{}{}{}*'.format(basename, file_separator, band_filter))[mapset] for b in bands: grass.mapcalc('{name}_masked={name}'.format(name=b), quiet=True, overwrite=True) grass.run_command('r.colors', map=b, color='grey.eq') # remove mask if applied if mask: grass.run_command('r.mask', flags='r', quiet=True)
def mask_data(band_filter, cfmask_filter, cloud_mask_value, file_separator): # do cleanup first Module('g.remove', type='raster', pattern='*_masked', flags='f', quiet=True) # find band1 raster maps first bands1 = grass.list_grouped('raster', pattern='*{}1*'.format(band_filter))[mapset] mapcalc_module = Module('r.mapcalc', quiet=True, overwrite=True, run_=False) color_module = Module('r.colors', color='grey.eq', quiet=True, run_=False) i = 0 count = len(bands1) for b1 in bands1: i += 1 basename = b1.split(file_separator)[0] grass.message("Processing <{0}> ({1}/{2})...".format(basename, i, count)) grass.percent(i, count, 5) # set computational region based on first band (ignore NULL values) Module('g.region', raster=b1, zoom=b1) maskname = '{}{}{}'.format(basename, file_separator, cfmask_filter) mask = grass.find_file(maskname, element='raster')['fullname'] # apply cloud mask if found if mask: Module('r.mask', flags='i', raster=maskname, maskcats=cloud_mask_value, overwrite=True, quiet=True) else: grass.warning("Mask missing for <{}>".format(basename)) # get list of bands bands = grass.list_grouped('raster', pattern='{}{}{}*'.format(basename, file_separator, band_filter))[mapset] # create copy of bands with mask applied (using r.mapcalc) for b in bands: new_mapcalc = copy.deepcopy(mapcalc_module) queue.put(new_mapcalc(expression='{name}_masked={name}'.format(name=b))) queue.wait() # set color table to grey.eq for masked bands masked_bands = grass.list_grouped('raster', pattern='*_masked')[mapset] for b in masked_bands: new_color = copy.deepcopy(color_module) queue.put(new_color(map=b)) queue.wait() # remove mask if applied if mask: Module('r.mask', flags='r', quiet=True)
def validateMapNames(names, etype): """Checks if maps exist and completes missing mapset. Input is list of map names. Raises GException if map doesn't exist. """ mapDict = grass.list_grouped(etype) newNames = [] for name in names: if name.find("@") >= 0: nameShort, mapset = name.split('@', 1) if nameShort in mapDict[mapset]: newNames.append(name) else: raise GException(_("Map <%s> not found.") % name) else: found = False for mapset, mapNames in mapDict.iteritems(): if name in mapNames: found = True newNames.append(name + "@" + mapset) if not found: raise GException(_("Map <%s> not found.") % name) return newNames
def test_3Corridors_OnClick_run_simulation(self): '''test Corridors OnClick 10 run simulation''' print 'test Corridors OnClick 10 run simulation' self.corr.remove_aux_maps = False os.chdir(self.path) #raw_input() self.corr.OnClick(self.evt10) list_rast = grass.list_grouped('raster', pattern = 'MSP*')['PERMANENT'] # number of outputs = 4 methods * (length_of_list + 1_final_output) self.assertTrue(len(list_rast), (4*(len(self.corr.patch_id_list)/2 + 1))) out_files = os.listdir('.') self.assertTrue(len(out_files), (4*(len(self.corr.patch_id_list)/2 + 1) + len(self.corr.patch_id_list)/2 + 1)) self.assertIn(self.corr.NEXPER_FINAL+'_M1_RSFI.tif', out_files) self.assertIn(self.corr.NEXPER_FINAL+'_M2_RSFI.tif', out_files) self.assertIn(self.corr.NEXPER_FINAL+'_M3_RSFI.tif', out_files) self.assertIn(self.corr.NEXPER_FINAL+'_M4_RSFI.tif', out_files) #self.assertIn(self.corr.NEXPER_FINAL+'_LargeZone_Corridors.tif', out_files) out_tif = [] out_txt = [] for i in out_files: if '.tif' in i: out_tif.append(i) if '.txt' in i: out_txt.append(i) self.assertTrue(len(out_tif), 4*(len(self.corr.patch_id_list)/2 + 1)) self.assertTrue(len(out_txt), (len(self.corr.patch_id_list)/2 + 1))
def test_1Corridors_OnClick_import_files(self): '''test Corridors OnClick 240 import files''' print 'test Corridors OnClick 240 import files' self.corr.OnClick(self.evt240) list_rast = grass.list_grouped('raster', pattern = '*test_rast')['PERMANENT'] self.assertTrue(list_rast, ['resist_test_rast', 'st_test_rast'])
def mask_data(band_filter, cfmask_filter, cloud_mask_value, file_separator): # do cleanup first grass.run_command('g.remove', type='raster', pattern='*_masked', flags='f', quiet=True) # find band1 raster maps first bands1 = grass.list_grouped('raster', pattern='*{}1*'.format(band_filter))[mapset] count = len(bands1) i = 0 for b1 in bands1: i += 1 basename = b1.split(file_separator)[0] grass.message("Processing <{0}> ({1}/{2})...".format( basename, i, count)) grass.percent(i, count, 5) # set computational region based on first band grass.run_command('g.region', raster=b1) maskname = '{}{}{}'.format(basename, file_separator, cfmask_filter) mask = grass.find_file(maskname, element='raster')['fullname'] # apply cloud mask if found if mask: grass.run_command('r.mask', flags='i', raster=maskname, maskcats=cloud_mask_value, overwrite=True, quiet=True) else: grass.warning("Mask missing for <{}>".format(basename)) # create copy of band with mask applied bands = grass.list_grouped('raster', pattern='{}{}{}*'.format( basename, file_separator, band_filter))[mapset] for b in bands: grass.mapcalc('{name}_masked={name}'.format(name=b), quiet=True, overwrite=True) grass.run_command('r.colors', map=b, color='grey.eq') # remove mask if applied if mask: grass.run_command('r.mask', flags='r', quiet=True)
def raster_exists(raster,mapset): #boocan = raster_exists(can,'PERMANENT') booexists = False raster_list = grass.list_grouped('rast')[mapset] for rast in raster_list: if(rast == raster): booexists = True return booexists
def main(): # temporary region gscript.use_temp_region() # set parameters overwrite = True tension = 25 smooth = 5 npmin = 300 dmin = 0.5 resolution = 10000 # set region region = "dem@PERMANENT" # list scanned DEMs for experiment 1 dems = gscript.list_grouped('rast', pattern='*dem_1')['data'] # iterate through scanned DEMs for dem in dems: # check alignment gscript.run_command('r.region', map=dem, raster=region) # reinterpolate DEM from random points using regularized spline with tension gscript.run_command('g.region', raster=region, res=3) gscript.run_command('r.random', input=dem, npoints=resolution, vector=dem.replace("dem","points"), flags='bd', overwrite=overwrite) gscript.run_command('v.surf.rst', input=dem.replace("dem","points"), elevation=dem, tension=tension, smooth=smooth, npmin=npmin, dmin=dmin, overwrite=overwrite) gscript.run_command('r.colors', map=dem, color="elevation") gscript.run_command('g.remove', type='vector', pattern='*points*', flags='f') # list scanned DEMs for experiment 2 dems = gscript.list_grouped('rast', pattern='*dem_2')['reinterpolation'] # iterate through scanned DEMs for dem in dems: # check alignment gscript.run_command('r.region', map=dem, raster=region) # reinterpolate DEM from random points using regularized spline with tension gscript.run_command('g.region', raster=region, res=3) gscript.run_command('r.random', input=dem, npoints=resolution, vector=dem.replace("dem","points"), flags='bd', overwrite=overwrite) gscript.run_command('v.surf.rst', input=dem.replace("dem","points"), elevation=dem, tension=tension, smooth=smooth, npmin=npmin, dmin=dmin, overwrite=overwrite) gscript.run_command('r.colors', map=dem, color="elevation") gscript.run_command('g.remove', type='vector', pattern='*points*', flags='f')
def main(): # # define variables # overwrite = False mapset = options['mapset'] # prefix for copied maps datatype = options['datatype'] # prefix for copied maps filter = options['filter'] # prefix for copied maps filter_type = options['filter_type'] # prefix for copied maps prefix = options['output_prefix'] # prefix for copied maps datalist = [] # list of GRASS data files to copy input = '' output = '' if grass.overwrite(): overwrite = True if filter_type == 'select all': filter = '*' filterflag = '' if filter_type == 'regular expressions': filterflag = 'r' if filter_type == 'extended regular expressions': filterflag = 'e' # # first run g.list to get list of maps to parse # l = grass.list_grouped(type=datatype, pattern=filter, check_search_path=True, flag=filterflag) if mapset not in l: grass.warning( _('You do not have access to mapset %s. Run g.mapsets (under settings menu) to change mapset access' ) % mapset) return datalist = l[mapset] # # then loop through the maps copying them with g.copy and optionally adding prefix # for input in datalist: if prefix: output = '%s_%s' % (prefix, input) else: output = input params = {datatype: '%s@%s,%s' % (input, mapset, output)} grass.run_command('g.copy', overwrite=overwrite, **params) if datatype == 'vector' and flags['t']: grass.run_command('v.build', map=output)
def main(): # # define variables # overwrite = False mapset = options["mapset"] # prefix for copied maps datatype = options["datatype"] # prefix for copied maps filter = options["filter"] # prefix for copied maps filter_type = options["filter_type"] # prefix for copied maps prefix = options["output_prefix"] # prefix for copied maps datalist = [] # list of GRASS data files to copy input = "" output = "" if grass.overwrite(): overwrite = True if filter_type == "select all": filter = "*" filterflag = "" if filter_type == "regular expressions": filterflag = "r" if filter_type == "extended regular expressions": filterflag = "e" # # first run g.list to get list of maps to parse # l = grass.list_grouped(type=datatype, pattern=filter, check_search_path=True, flag=filterflag) if mapset not in l: grass.warning( _("You do not have access to mapset %s. Run g.mapsets (under settings menu) to change mapset access" ) % mapset) return datalist = l[mapset] # # then loop through the maps copying them with g.copy and optionally adding prefix # for input in datalist: if prefix: output = "%s_%s" % (prefix, input) else: output = input params = {datatype: "%s@%s,%s" % (input, mapset, output)} grass.run_command("g.copy", overwrite=overwrite, **params) if datatype == "vector" and flags["t"]: grass.run_command("v.build", map=output)
def OnClick(self,event): #self.logger.AppendText(" Click on object with Id %d\n" %event.GetId()) #______________________________________________________________________________________________________________ if event.GetId()==10: #10==START if Form1.Chech_single==1: import Cria_grupo Form1.out_map=Form1.import_map.split('\\');Form1.out_map=Form1.out_map[-1].replace('.','_') grass.run_command('r.in.gdal',input=Form1.import_map,out=Form1.out_map,overwrite=True) Form1.group_img=grass.list_grouped('rast', pattern='*'+Form1.out_map+'*') ['PERMANENT'] Cria_grupo.CriaGrupo(Form1.group_img) grass.run_command('g.region', rast=Form1.group_img[0],verbose=False) grass.run_command('i.segment', group='Grupo',output=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,threshold=Form1.thrs, minsize=Form1.Misize) #grass.run_command('r.to.vect',input=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,output=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,type='area' ) if event.GetId()==11: if Form1.Chech_mult==1: import import_folder_imagens import_folder_imagens.import_fd(folder_files=Form1.dir_in) #______________________________________________________________________________________________________________ if event.GetId()==9: #9==CHANGE BACKGROUND if Form1.plotmovements==1: self.Refresh() Form1.background_filename=Form1.listMapsPng Form1.background_filename_start=Form1.background_filename[Form1.contBG] img =Image.open(Form1.background_filename[Form1.contBG]) # redimensionamos sem perder a qualidade img = img.resize((Form1.size,Form1.hsize),Image.ANTIALIAS) img.save(Form1.background_filename[Form1.contBG]) imageFile=Form1.background_filename[Form1.contBG] im1 = Image.open(imageFile) jpg1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() wx.StaticBitmap(self, -1, jpg1, (380,40), (jpg1.GetWidth(), jpg1.GetHeight()), style=wx.SIMPLE_BORDER) Form1.background_filename=Form1.background_filename_start Form1.contBG=Form1.contBG+1 self.Refresh() if len(Form1.listMapsPng)==Form1.contBG: Form1.contBG=0 self.Refresh() #______________________________________________________________________________________________________________ if event.GetId()==11: if Form1.Chech_mult==1: Form1.dir_in=selecdirectori() if Form1.Chech_single==1: Form1.import_map=selecdirectori()
def timestamp_data(directory, maps=None): mtl = process_mtl(directory) if maps is None: maps = grass.list_grouped('raster', pattern='*LT5*')[mapset] grass.message("Timestamping maps ({} maps)...".format(len(maps))) for name in maps: basename = name[name.find('LT'):].split('_')[0] starttime = datetime.datetime.strptime(mtl[basename]['date'], '%Y-%m-%d %H:%M:%S') endtime = starttime + datetime.timedelta(seconds=1) Module('r.timestamp', map=name, date='{start}/{end}'.format(start = starttime.strftime('%d %b %Y %H:%M:%S'), end = endtime.strftime('%d %b %Y %H:%M:%S')))
def timestamp_data(): maps = grass.list_grouped('raster')[mapset] grass.message("Timestamping maps...") count = len(maps) i = 0 for name in maps: i += 1 grass.percent(i, count, 5) date = name.split('_')[-1].split('.')[0] grass_date = datetime.datetime.strptime(date, '%Y%m%d').strftime('%d %b %Y') grass.run_command('r.timestamp', map=name, date=grass_date) grass.percent(0, 0, 0)
def main(): #setting up variables for use later on inmap = os.getenv('GIS_OPT_inmap') impacts = os.getenv('GIS_OPT_impacts') outmap = os.getenv('GIS_OPT_outmap') recovery = os.getenv('GIS_OPT_recovery') sf_color = os.getenv('GIS_OPT_sf_color') txtout = outmap + '_sfertil_stats.txt' #Check to see if there is a MASK already, and if so, temporarily rename it if "MASK" in grass.list_grouped('rast')[grass.gisenv()['MAPSET']]: ismask = 1 tempmask = 'mask_%i' % random.randint(0, 100) grass.run_command('g.rename', quiet="True", rast='MASK,' + tempmask) else: ismask = 0 #Set MASK to input DEM grass.run_command('r.mask', input=inmap) #updating raw soil fertility category numbers grass.mapcalc( '${outmap}=if(isnull(${impacts}) && ${inmap} >= 100 - ${recovery}, 100, if(isnull(${impacts}), (${inmap} + ${recovery}), if(${inmap} >= ${impacts}, (${inmap} - ${impacts}), 0 )))', outmap=outmap, inmap=inmap, recovery=recovery, impacts=impacts) grass.run_command('r.colors', quiet='True', map=outmap, rules=sf_color) #checking total area of updated cells totarea = grass.read_command('r.stats', flags='an', input=impacts, fs=',', nsteps='1').split(',') grass.message('\n\nTotal area of impacted zones = %s square meters\n\n' % totarea[1]) #creating optional output text file of stats if os.getenv('GIS_FLAG_s') == '1': f = file(txtout, 'wt') f.write('Stats for ' + outmap + '\n\nTotal area of impacted zones = ' + totarea[1] + ' square meters\n\nSoil Fertility Value,Area (sq. m)\n') areadict = grass.parse_command('r.stats', flags='an', input=impacts) for key in areadict: f.write(key + '\n') f.close() grass.message('\nCleaning up...\n\n') grass.run_command('r.mask', flags='r') if ismask == 1: grass.run_command('g.rename', quiet="True", rast=tempmask + ',MASK') grass.message('DONE!\n\n')
def export_end(gn): mlist=gscript.list_grouped('raster', pattern=gn+"_endmembers"+"*")[gisenv()['MAPSET']] gscript.run_command("g.region", rast=gn+"_endmembers."+str(1), zoom=gn+"_endmembers."+str(1)) for map in mlist: gscript.run_command("r.out.gdal",input=map, output=wd+"/"+map+".tif", format="GTiff") pat = mlist[0].split(".")[0] filelist=glob.glob(wd+"/"+pat+".?"+".tif") files=' '.join(filelist) expr="/usr/bin/gdalbuildvrt -separate {dir}/{vrt} {files}".format(dir=wd,vrt="temp.vrt",files=files) os.system(expr) tiffout=wd+"/"+pat+".tif" expr="/usr/bin/gdal_translate {dir}/{vrt} {out}".format(dir=wd,vrt="temp.vrt", out=tiffout) os.system(expr) gscript.run_command("g.region", region=roi) return
def mask_data(): bands1 = grass.list_grouped('raster', pattern='lndcal*.1$')[mapset] count = len(bands1) i = 0 for b1 in bands1: i += 1 oname = b1.split('.')[1] grass.message("Processing <{0}> ({1}/{2})...".format(oname, i, count)) grass.percent(i, count, 5) grass.run_command('g.region', raster=b1) mask = grass.find_file('fmask.' + oname, element='raster')['fullname'] if mask: grass.run_command('r.mask', raster='fmask.' + oname, maskcats=1, overwrite=True, quiet=True) else: grass.warning("Mask missing for <{0}>".format(oname)) bands = grass.list_grouped('raster', pattern='lndcal.{0}.*'.format(oname))[mapset] for b in bands: n = b.split('.')[-1] grass.mapcalc('{0}.{1}={2}'.format(oname, n, b), quiet=True, overwrite=True) if mask: grass.run_command('r.mask', flags='r', quiet=True) grass.run_command('g.remove', type='raster', pattern='fmask*,lndcal*', flags='f', quiet=True) grass.percent(0, 0, 0)
def clip_group_region(groupname,subfix,roi,nullval): # This functions clips images of a gruop as groupname+subfix, # Set the null value of the clipped raster (usefull for landsat null=0) # And creates a gruop of the recently created files gscript.run_command('g.region', region=roi) grouplist=gscript.read_command("i.group", group=groupname, flags="g" ).split() for raster in grouplist: name=raster.split("@")[0] band= name.split(".")[1] rout= name.split(".")[0]+"_"+subfix expression="{rout}.{b} = {rin}".format(rin=name,rout=rout,b=band) gscript.mapcalc(expression) gscript.run_command('r.null', map="{rout}.{b}".format(rout=rout,b=band),setnull=nullval) mlist=gscript.list_grouped('raster', pattern=groupname+"_"+subfix+"*")[gisenv()['MAPSET']] gscript.run_command("i.group", group=groupname+"_"+subfix, input=mlist) return
def timestamp_data(directory, maps=None): mtl = process_mtl(directory) if maps is None: maps = grass.list_grouped('raster', pattern='*LT5*')[mapset] grass.message("Timestamping maps ({} maps)...".format(len(maps))) for name in maps: basename = name[name.find('LT'):].split('_')[0] starttime = datetime.datetime.strptime(mtl[basename]['date'], '%Y-%m-%d %H:%M:%S') endtime = starttime + datetime.timedelta(seconds=1) Module('r.timestamp', map=name, date='{start}/{end}'.format( start=starttime.strftime('%d %b %Y %H:%M:%S'), end=endtime.strftime('%d %b %Y %H:%M:%S')))
def filter_data(cloud_cover=5): # filter scenes based on cloud cover maps = grass.list_grouped('raster', pattern='*_MTLFmask')['PERMANENT'] filtered = [] grass.message("Filtering...") for name in maps: stats = Module('r.stats', input=name, flags='pN', quiet=True, stdout_=PIPE, separator=':') for line in stats.outputs.stdout.splitlines(): k, v = line.split(':') if k == '4': v = float(v.rstrip('%')) if v <= cloud_cover: filtered.append(name.rstrip('_MTLFmask')) grass.message("{} from {} scenes selected".format(len(filtered), len(maps))) return filtered
def timestamp_data(directory, file_separator): # process metadata first file_datetime = process_metadata(directory) timestamp_module = Module('r.timestamp', run_=False) maps = grass.list_grouped('raster', pattern='*_masked')[mapset] grass.message("Timestamping masked maps...") for name in maps: basename = name.split(file_separator)[0] try: grass_date = file_datetime[basename] except KeyError: grass.warning("No timestamp available for <{}>".format(basename)) new_timestamp = copy.deepcopy(timestamp_module) queue.put(new_timestamp(map=name, date=grass_date)) queue.wait()
def timestamp_data(directory, file_separator): # process metadata first file_datetime = process_metadata(directory) maps = grass.list_grouped('raster', pattern='*_masked')[mapset] grass.message("Timestamping masked maps...") count = len(maps) i = 0 for name in maps: i += 1 grass.percent(i, count, 5) basename = name.split(file_separator)[0] try: grass_date = file_datetime[basename] except KeyError: grass.warning("No timestamp available for <{}>".format(basename)) grass.run_command('r.timestamp', map=name, date=grass_date) grass.percent(0, 0, 0)
def main(): old_database = options['old_database'] new_database = options['new_database'] old_schema = options['old_schema'] new_schema = options['new_schema'] mapset = grass.gisenv()['MAPSET'] nuldev = file(os.devnull, 'w') for vect in grass.list_grouped('vect')[mapset]: vect = "%s@%s" % (vect, mapset) grass.message(_("Reconnecting vector <%s>") % vect) for f in grass.vector_db(map, stderr = nuldev).itervalues(): layer = f['layer'] schema_table = f['table'] key = f['key'] database = f['database'] driver = f['driver'] if '.' in schema_table: st = schema_table.split('.', 1) schema = st[0] table = st[1] else: schema = '' table = schema_table if new_schema: new_schema_table = "%s.%s" % (new_schema, table) else: new_schema_table = table grass.message(_("SCHEMA = %s TABLE = %s NEW_SCHEMA_TABLE = %s") % (schema, table, new_schema_table)) if database == old_database and schema == old_schema: grass.message(_("Reconnecting layer ") + layer) grass.message(_("v.db.connect -o map=%s layer=%s driver=%s database=%s table=%s key=%s") % (vect, layer, driver, new_database, new_schema_table, key)) grass.run_command('v.db.connect', flags = 'o', map = vect, layer = layer, driver = driver, database = new_database, table = new_schema_table, key = key) else: grass.message(_("Layer <%s> will not be reconnected, database or schema do not match.") % layer)
def list_landscapes_habitat(use_random_landscapes = True, habmat_pattern = '*HABMAT'): ''' New function: this function returns the list of landscapes in GRASS database locations Input: use_random_landscapes: if True, seach for maps in the GRASS database of random landscapes; otherwise, use real landscapes ''' # If the user is going to use the database of random landscapes if use_random_landscapes: mapset_habmat = 'MS_HABMAT' # Or, if the user is going to use read landscapes else: # Assessing the name of the current mapset mapset_habmat = grass.read_command('g.mapset', flags = 'p').replace('\n','').replace('\r','') # List of maps of habitat list_binary_maps = grass.list_grouped('rast', pattern = habmat_pattern) [mapset_habmat] return list_binary_maps
def import_data(directory, file_filter): # collect files to be imported files = [] for f in os.listdir(directory): if f.endswith(".tif") and [x for x in file_filter if x in f]: files.append(f) # import selected files into GRASS imodule = 'r.external' ###imodule = 'r.in.gdal' import_module = Module(imodule, quiet=True, overwrite=True, run_=False) i = 0 count = len(files) for f in files: i += 1 grass.message("Importing <{0}> ({1}/{2})...".format(f, i, count)) grass.percent(i, count, 2) new_import = copy.deepcopy(import_module) map_name = os.path.splitext(f)[0] queue.put(new_import(input=os.path.join(directory, f), output=map_name)) queue.wait() # set color table for cfmask map # 0 clear # 1 water # 2 shadow # 3 snow # 4 cloud colors = """0 black 1 blue 2 grey 3 white 4 149 186 224""" color_module = Module('r.colors', rules='-', quiet=True, stdin_=colors, run_=False) cfmask_maps = grass.list_grouped('raster', pattern='*cfmask*')[mapset] for map_name in cfmask_maps: new_color = copy.deepcopy(color_module) queue.put(new_color(map=map_name)) queue.wait()
def dsm(): """import and patch digital surface models""" # import all dsm for root, dirs, files in os.walk(dirpath): i = 1 for file in files: if file.endswith(".tif"): rootpath = os.path.join(dirpath, root) filepath = os.path.join(rootpath, file) # import dsm tiles gscript.run_command('r.in.gdal', input=filepath, output='dsm_' + str(i), title='dsm_' + str(i), flags='ek', memory=9000, overwrite=overwrite) i = i + 1 else: pass # list dsm rasters dsm_list = gscript.list_grouped('rast', pattern='dsm*')[mapset] # set region gscript.run_command('g.region', raster=dsm_list, res=res) # patch dsm rasters gscript.run_command('r.patch', input=dsm_list, output='surface_2012', overwrite=overwrite)
# Import map folder_path = r'H:\_neojaguardatabase\Envdatabase\30m\Neotropic\Water frequency\2010' os.chdir(folder_path) # Change to this folder files = os.listdir(folder_path) # List files in the folder for i in files: if i[-3:] == 'tif': # Select tif files print i name = i.replace('.tif', '_rast') grass.run_command('r.import', input=i, output=name, overwrite=True) # Import maps # Mosaic of water frequency maps # List of maps maps_water = grass.list_grouped('rast', pattern='p*2010_rast')['PERMANENT'] # Region of study grass.run_command('g.region', rast=map_for_define_region, res=30, flags='ap') # Combine maps water_map_mosaic = 'water_frequency_2010_30m_tif_exp' grass.run_command('r.patch', input=maps_water, output=water_map_mosaic, overwrite=True) # Delete input maps grass.run_command('g.remove', type='raster', pattern='p*2010_rast', flags='f') # 2. Ecoregions 2017 - vector
def check_for_mask(): if "MASK" in grass.list_grouped('rast')[grass.gisenv()['MAPSET']]: grass.fatal(_('There is already a MASK in place.' 'Please remove it before using this module. '))
def OnClick(self,event): #self.logger.AppendText(" Click on object with Id %d\n" %event.GetId()) #______________________________________________________________________________________________________________ if event.GetId()==10: #10==START if Form1.Chech_single==1: import Cria_grupo import v_what Form1.out_map=Form1.import_map.split('\\');Form1.out_map=Form1.out_map[-1].replace('.','_') grass.run_command('r.in.gdal',input=Form1.import_map,out=Form1.out_map,overwrite=True) Form1.group_img=grass.list_grouped('rast', pattern='*'+Form1.out_map+'*') ['PERMANENT'] Cria_grupo.CriaGrupo(Form1.group_img) grass.run_command('g.region', rast=Form1.group_img[0],verbose=False) #grass.run_command('i.segment', group='Grupo',output=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,threshold=Form1.thrs, minsize=Form1.Misize) Form1.out_name_vect=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize` Form1.out_name_vect=Form1.out_name_vect.replace('.','_') Form1.out_name_vect=Form1.out_name_vect.replace('-','_') Form1.out_name_vect='A'+Form1.out_name_vect #grass.run_command('r.to.vect',input=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,output=Form1.out_name_vect,type='area',overwrite=True ) #v_what.v_what(Form1.group_img,Form1.out_name_vect) grass.run_command('v.out.ogr', input=Form1.out_name_vect, output=Form1.out_name_vect+'.shp',type='area',) p = Popen([r"C:\Program Files\R\R-2.15.3\bin\x64\Rscript.exe", "teste_R_sem_var_amb.R"], cwd=r"E:\data_2015\___john\Desenvolvimentos\aplications\Aplicacoes_grass\automatic classification") if event.GetId()==11: if Form1.Chech_mult==1: import import_folder_imagens import_folder_imagens.import_fd(folder_files=Form1.dir_in) #______________________________________________________________________________________________________________ if event.GetId()==9: #9==CHANGE BACKGROUND if Form1.plotmovements==1: self.Refresh() Form1.background_filename=Form1.listMapsPng Form1.background_filename_start=Form1.background_filename[Form1.contBG] img =Image.open(Form1.background_filename[Form1.contBG]) # redimensionamos sem perder a qualidade img = img.resize((Form1.size,Form1.hsize),Image.ANTIALIAS) img.save(Form1.background_filename[Form1.contBG]) imageFile=Form1.background_filename[Form1.contBG] im1 = Image.open(imageFile) jpg1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() wx.StaticBitmap(self, -1, jpg1, (380,40), (jpg1.GetWidth(), jpg1.GetHeight()), style=wx.SIMPLE_BORDER) Form1.background_filename=Form1.background_filename_start Form1.contBG=Form1.contBG+1 self.Refresh() if len(Form1.listMapsPng)==Form1.contBG: Form1.contBG=0 self.Refresh() #______________________________________________________________________________________________________________ if event.GetId()==11: if Form1.Chech_mult==1: Form1.dir_in=selecdirectori() if Form1.Chech_single==1: Form1.import_map=selecdirectori()
df_stats['stats'] = [ 'min', 'max', 'range', 'mean', 'median', 'stddev', 'skew', 'kurt', 'p25', 'p75' ] for dem in area: if not dem.startswith('tdx12'): diff_map = 'tdx12_diff_' + dem.split( '_')[0] + '_wgs84_' + area_name diff_map_array = raster_as_1d_array(diff_map) df_stats[diff_map] = calc_stats(diff_map_array) # export stats to latex print('\n\n') print df_stats.transpose().to_latex(float_format=lambda x: '%4.2f' % x) # lists of all diff maps diff_list = grass.list_grouped('raster', pattern='*diff*')['tandemX_brasil'] # fix colortable (color gradient from blue-white-red beteween -15/+15, then darker colors to min-max) rule = '''0% 0:30:110 \n-15 blue \n0 white \n15 red \n100% 125:25:15''' for diff_map in diff_list: grass.write_command('r.colors', map=diff_map, rules='-', stdin=rule) # export diffs maps as pngs for diff_map in diff_list: area_name = diff_map.split('_')[-1] grass.run_command('g.region', raster=diff_map, flags='pa', res='0:0:01') # diff map grass.run_command('d.mon', start='cairo', output=diff_map+'.png', resolution='3', \ height=500, width=500, overwrite=True) grass.run_command('d.rast', map=diff_map) grass.run_command('d.grid', size='0.25', text_color='black', flags='c')
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') if __name__ == '__main__': for vector in sys.argv[1:]: vectors = gscript.list_grouped(type='vector', pattern="cluster_{}*".format(vector))[gscript.gisenv()['MAPSET']] #### don't use cluster 1 (individuals) with couples, ... extract_clusters(vectors, [1], is_weekday, 'cluster_individuals_weekday') extract_clusters(vectors, [2], is_weekday, 'cluster_couples_weekday') extract_clusters(vectors, [3,4,5], is_weekday, 'cluster_smallgroups_weekday') extract_clusters(vectors, range(6,20), is_weekday, 'cluster_biggroups_weekday') extract_clusters(vectors, [1], is_sunday, 'cluster_individuals_sunday') extract_clusters(vectors, [2], is_sunday, 'cluster_couples_sunday') extract_clusters(vectors, [3,4,5], is_sunday, 'cluster_smallgroups_sunday') extract_clusters(vectors, range(6,20), is_sunday, 'cluster_biggroups_sunday') vectors = gscript.list_grouped(type='vector', pattern="cluster_*_weekday".format(vector))[gscript.gisenv()['MAPSET']] vectors += gscript.list_grouped(type='vector', pattern="cluster_*_sunday".format(vector))[gscript.gisenv()['MAPSET']] create_heatmaps(vectors, 'ortho_9106', 5, 660, 670)
def main(): #setting up variables for use later on elev = os.getenv('GIS_OPT_elev') vect = os.getenv('GIS_OPT_vect') lmbda = os.getenv('GIS_OPT_lambda') slpfct = os.getenv('GIS_OPT_slope_factor') a = os.getenv('GIS_OPT_a') b = os.getenv('GIS_OPT_b') c = os.getenv('GIS_OPT_c') d = os.getenv('GIS_OPT_d') sigma = os.getenv('GIS_OPT_sigma') area = float(os.getenv('GIS_OPT_area')) buffer = os.getenv('GIS_OPT_buffer') mapval = os.getenv('GIS_OPT_mapval') w_coefs = a + ',' + b + ',' + c + ',' + d if "MASK" in grass.list_grouped('rast')[grass.gisenv()['MAPSET']] and bool( os.getenv('GIS_OPT_sigma')) is True: grass.message( 'There is already a MASK in place, and you have also selected to mask slope values above %s.\n The high slope areas (slope mask) will be temporarily added to current MASKED areas for the calcualtion of the catchment geometry.\n The original MASK will be restored when the module finishes' % sigma) ismask = 2 elif "MASK" in grass.list_grouped('rast')[grass.gisenv()['MAPSET']]: grass.message( 'There is a MASK in place. The areas MASKed out will be ignored while calculating catchment geometry.' ) ismask = 1 else: ismask = 0 grass.message("Wanted buffer area=%s\n" % int(area)) #################################################### if bool(os.getenv('GIS_OPT_incost')) is True: grass.message('\n\nUsing input cost surface\n') cost = os.getenv('GIS_OPT_incost') else: grass.message('\n\nstep 1 of 4: Calculating cost surface\n') cost = 'temporary.cost' if bool(os.getenv('GIS_OPT_frict')) is True: grass.message('Calculating costs using input friction map\n') frict = os.getenv('GIS_OPT_frict') else: grass.message('Calculating for time costs only') frict = "temporary.friction" grass.mapcalc("${out} = if(isnull(${rast1}), null(), 1)", out=frict, rast1=elev) if os.getenv('GIS_FLAG_k') == '1': grass.message('Using Knight\'s move\n') #NOTE! because "lambda" is an internal python variable, it is impossible to enter the value for key "lambda" in r.walk. It ends up with a python error. grass.run_command('r.walk', quiet=True, flags='k', elevation=elev, friction=frict, output=cost, start_points=vect, walk_coeff=w_coefs, slope_factor=slpfct) else: grass.run_command('r.walk', quiet=True, elevation=elev, friction=frict, output=cost, start_points=vect, percent_memory='100', nseg='4', walk_coeff=w_coefs, slope_factor=slpfct) if bool(os.getenv('GIS_OPT_frict')) is False: grass.run_command('g.remove', quiet=True, rast=frict) ################################################# if bool(os.getenv('GIS_OPT_sigma')) is True: grass.message('\n\nCreating optional slope mask\n') slope = "temporary.slope" grass.run_command('r.slope.aspect', quiet=True, overwrite=True, elevation=elev, slope=slope) if ismask == 2: grass.mapcalc( "MASK=if(${rast1} <= %s, 1, if(${tempmask}, 1, null()))" % sigma, rast1=slope, tempmask=tempmask) else: grass.mapcalc("MASK=if(${rast1} <= %s, 1, null())" % sigma, rast1=slope) else: grass.message('No slope mask created') ################################################## if os.getenv('GIS_FLAG_l') == '1': grass.message( '\n\nCalculating list of possible catchment configurations\n') grass.message("cost value | catchment area") areadict = {} out2dictnum('r.stats -Aani input=' + cost + ' fs=, nv=* nsteps=255', ',', areadict) testarea = 0 #start the loop, and list the values for key in sorted(areadict): testarea = testarea + int(float(areadict[key])) grass.message("%s | %s" % (int(key), testarea)) if os.getenv('GIS_FLAG_c') == '1': if bool(os.getenv('GIS_OPT_incost')) is False: grass.run_command('g.rename', quiet=True, rast='temporary.cost,%s_cost_surface' % (buffer)) grass.message('Cleaning up...(keeping cost map)') grass.run_command('g.remove', quiet=True, rast='cost.reclass') else: grass.message('Cleaning up...1') grass.run_command('g.remove', quiet=True, rast='cost.reclass') else: if bool(os.getenv('GIS_OPT_incost')) is False: grass.message('Cleaning up...2') grass.run_command('g.remove', quiet=True, rast='cost.reclass,temporary.cost') else: grass.message('Cleaning up...3') grass.run_command('g.remove', quiet=True, rast='cost.reclass') if bool(os.getenv('GIS_OPT_sigma')) is True: grass.run_command('g.remove', quiet=True, rast=slope) if ismask == 2: grass.message('Reinstating original MASK...') grass.run_command('g.rename', quiet="True", rast=tempmask + ',MASK') elif ismask == 0 and bool(os.getenv('GIS_OPT_sigma')) is True: grass.run_command('g.remove', quiet=True, rast='MASK') elif ismask == 1: grass.message('Keeping original MASK') grass.message(' DONE!') return else: grass.message('\n\nCalculating buffer\n') areadict = {} out2dictnum('r.stats -Aani input=' + cost + ' fs=, nv=* nsteps=255', ',', areadict) tot_area = 0 for key in sorted(areadict): tot_area = tot_area + int(float(areadict[key])) maxcost = key grass.message( "Maximum cost distance value %s covers an area of %s square map units\n\nCommencing to find a catchment configuration.....\n\n" % (int(maxcost), tot_area)) testarea = 0 lastarea = 0 lastkey = 0 #start the loop, and home in on the target range for key in sorted(areadict): testarea = testarea + int(float(areadict[key])) if testarea >= area: break lastkey = key lastarea = testarea if (testarea - area) <= (area - lastarea): cutoff = key displayarea = testarea else: cutoff = lastkey displayarea = lastarea grass.message( "Catchment configuration found! Cost cutoff %s produces a catchment of %s square map units." % (int(cutoff), displayarea)) #################################################### grass.message('\n\nCreating output map\n') temp = tempfile.NamedTemporaryFile() temp.write('0 thru %s = %s\n' % (int(cutoff), mapval)) temp.flush() grass.run_command('r.reclass', overwrite=True, input=cost, output='cost.reclass', rules=temp.name) temp.close() grass.mapcalc("${out}=if(isnull(cost.reclass), null(), cost.reclass)", out=buffer) grass.message("\nThe output catchment map will be named %s" % buffer) grass.run_command('r.colors', quiet=True, map=buffer, color='ryb') if os.getenv('GIS_FLAG_c') == '1': if bool(os.getenv('GIS_OPT_incost')) is False: grass.run_command('g.rename', quiet=True, rast='temporary.cost,%s_cost_surface' % (buffer)) grass.message('Cleaning up...(keeping cost map)') grass.run_command('g.remove', quiet=True, rast='cost.reclass') else: grass.message('Cleaning up...1') grass.run_command('g.remove', quiet=True, rast='cost.reclass') else: if bool(os.getenv('GIS_OPT_incost')) is False: grass.message('Cleaning up...2') grass.run_command('g.remove', quiet=True, rast='cost.reclass,temporary.cost') else: grass.message('Cleaning up...3') grass.run_command('g.remove', quiet=True, rast='cost.reclass') if bool(os.getenv('GIS_OPT_sigma')) is True: grass.run_command('g.remove', quiet=True, rast=slope) if ismask == 2: grass.message('Reinstating original MASK...') grass.run_command('g.rename', quiet="True", rast=tempmask + ',MASK') elif ismask == 0 and bool(os.getenv('GIS_OPT_sigma')) is True: grass.run_command('g.remove', quiet=True, rast='MASK') elif ismask == 1: grass.message('Keeping original MASK') grass.message(' DONE!') return
def main(resistancemap, stmap, stlist, output_prefix, output_folder, variability, scale, simulations, method): # Checking parameters and passing them to LSCorridors instance #--------------------- # Initialize LSCorridors app class instance app = wx.PySimpleApp() frame = wx.Frame(None, -1, "LSCorridors", pos=(0,0), size=(560,450)) corr = Corridors(frame, -1) #--------------------- # Define folder for output files if output_folder == '': output_folder = os.getcwd() # Try to change to output_folder try: os.chdir(output_folder) corr.path = os.getcwd() g.message('Folder for output files: '+corr.path) except: grass.fatal(_('GRASS GIS cannot access the folder '+output_folder+'. Please check if the folder exists'+ 'and its access permissions.')) corr.OutDir_files_TXT = corr.path #--------------------- # Check if the resistance map and ST map are inside the GRASS GIS mapset # If they are, define them as inputs for LSCorridors current_mapset = grass.read_command('g.mapset', flags = 'p').replace('\n','').replace('\r','') maps=grass.list_grouped('rast')[current_mapset] # Resistance map if resistancemap in maps: # Name of the input resistance map corr.OutArqResist = resistancemap g.message('Input resistance map: '+resistancemap) else: grass.fatal(_('Input: resistance map. There is no raster map called '+resistancemap+ ' inside the GRASS GIS mapset '+current_mapset+ '. Please import the map into GRASS GIS.')) if stmap in maps: # Name of the input resistance map corr.OutArqST = stmap g.message('Input Source-Target (ST) map: '+stmap) else: grass.fatal(_('Input: ST map. There is no raster map called '+stmap+ ' inside the GRASS GIS mapset '+current_mapset+ '. Please import the map into GRASS GIS.')) #--------------------- # Transforms stlist into a python list nad checks if the list is ok corr.patch_id_list = stlist.split(',') corr.patch_id_list_bkp = corr.patch_id_list lenlist = len(corr.patch_id_list) ########################## # include a flag with the possibility of being a txt file # Tests if the list has more than one element if lenlist <= 1: grass.fatal(_("Incorrect ST list. List length is smaller than 2! "+ "Please check the list.")) # Tests if the length of the ST list is even elif lenlist > 1 and int(lenlist)%2 == 1: grass.fatal(_("Incorrect ST list. List length cannot be odd. "+ "Please check the list.")) else: g.message('ST list checked and OK.') #--------------------- # Gets prefix for output files and puts that into output final names if output_prefix == '': corr.NEXPER_FINAL = corr.NEXPER_AUX+'_'+resistancemap corr.NEXPER_FINAL_txt = corr.NEXPER_AUX_txt+'_'+resistancemap else: corr.NEXPER_FINAL = corr.NEXPER_AUX+'_'+output_prefix corr.NEXPER_FINAL_txt = corr.NEXPER_AUX_txt+'_'+output_prefix g.message('Prefix of the output files: '+corr.NEXPER_FINAL) #--------------------- # Variability parameter # Checks if the values are positive and real try: # If no varibility parameter was passed, use the standard option if variability == '' or variability == '2.0': # Define the LSCorridors variability list corr.ruidos_float = [2.0] # If values of variability were passed, check that else: varlist = [float(i) for i in variability.split(',')] if len(varlist) < 1: grass.fatal(_('The list has no elements. Please check it.')) elif any(i < 0.0 for i in varlist): grass.fatal(_('Incorrect variability parameter(s). Variability must be '+ 'a number equal to or greater than zero! '+ 'Please check the parameter(s).')) else: # Define the LSCorridors variability list corr.ruidos_float = varlist g.message("Variability parameter(s): "+', '.join(str(i) for i in corr.ruidos_float)) except: grass.fatal(_('One or more of the variability values is not a real number. Check it.')) #--------------------- # Methods and number of simulations # Checks if number of simulations is integer and positive try: if int(simulations) < 1: grass.fatal(_('The number of simulations must be a positive integer value.')) except: grass.fatal(_('The number of simulations must be numeric.')) # Checks if the methods are valid if method == '': # If not parameter was passed, used the standard 'MP' method = 'MP' else: possible_methods = ['MP', 'MLmin', 'MLavg', 'MLmax'] method_list = method.split(',') if all(i in possible_methods for i in method_list): pass else: grass.fatal(_('At list one of the methods is not valid. They must be one of these:'+ 'MP, MLmin, MLavg, or MLmax')) # Define number of simulations for each method if 'MP' in method_list: corr.Nsimulations1 = int(simulations) g.message('Number of simulations for method MP: '+simulations) else: corr.Nsimulations1 = 0 if 'MLmin' in method_list: corr.Nsimulations2 = int(simulations) g.message('Number of simulations for method MLmin: '+simulations) else: corr.Nsimulations2 = 0 if 'MLavg' in method_list: corr.Nsimulations3 = int(simulations) g.message('Number of simulations for method MLavg: '+simulations) else: corr.Nsimulations3 = 0 if 'MLmax' in method_list: corr.Nsimulations4 = int(simulations) g.message('Number of simulations for method MLmax: '+simulations) else: corr.Nsimulations4 = 0 #--------------------- # Scale parameter # Checks if the values are positive and real try: # If no scale parameter was passed, use the standard option if scale == '' or scale == '100': # Define the LSCorridors scale list corr.escalas = [100] # If values of scale were passed, check that else: scalelist = [int(i) for i in scale.split(',')] # First, defining GRASS GIS region as output map region grass.run_command('g.region', rast=resistancemap) # Second, reading map resolution res = grass.read_command('g.region', rast=resistancemap, flags='m') res2 = res.split('\n') res3 = res2[5] res3 = float(res3.replace('ewres=','')) # Third, calculate window size (landscape scale x 2) in number of pixels escalas_pixels = [float(i)*2/res3 for i in scalelist] # Finally, tests if any of the scales are lower than the pixel size # (this only matters if methods MLmin, MLavg, or MLmax are going to be simulated) if any(i < 2.0 for i in escalas_pixels) and (corr.Nsimulations2 > 0 or corr.Nsimulations3 > 0 or corr.Nsimulations4 > 0): #print 'oi '+res3 grass.fatal(_("There may a problem with scale parameter. "+ "Input map resolution is "+`round(res3,1)`+" meters, scale should be greater than that! "+ "Please check the parameter(s).")) else: # Define the LSCorridors variability list corr.escalas = scalelist g.message("Scale parameter(s): "+', '.join(str(i) for i in corr.escalas)) except: grass.fatal(_('One or more of the scale values is not a real number. Check it.')) # Tests are going to be performed; however, as one do not want dialog boxes to be # shown, we keep this option as True corr.perform_tests = True # Event to test RUN SIMULATIONS evt10 = wx.PyCommandEvent(wx.EVT_BUTTON.typeId, 10) corr.OnClick(evt10) # Delete .pyc created #os.remove('LS_corridors_v1_0_0.pyc') os.remove('*.pyc') # call run START button return 0
def main(): # old connection old_database = options["old_database"] old_schema = options["old_schema"] # new connection default_connection = gscript.db_connection() if options["new_driver"]: new_driver = options["new_driver"] else: new_driver = default_connection["driver"] if options["new_database"]: new_database = options["new_database"] else: new_database = default_connection["database"] if options["new_schema"]: new_schema = options["new_schema"] else: new_schema = default_connection["schema"] if old_database == "": old_database = None old_database_subst = None if old_database is not None: old_database_subst = substitute_db(old_database) new_database_subst = substitute_db(new_database) if old_database_subst == new_database_subst and old_schema == new_schema: gscript.fatal( _("Old and new database connection is identical. " "Nothing to do.")) mapset = gscript.gisenv()["MAPSET"] vectors = gscript.list_grouped("vect")[mapset] num_vectors = len(vectors) if flags["c"]: # create new database if not existing create_db(new_driver, new_database) i = 0 for vect in vectors: vect = "%s@%s" % (vect, mapset) i += 1 gscript.message( _("%s\nReconnecting vector map <%s> " "(%d of %d)...\n%s") % ("-" * 80, vect, i, num_vectors, "-" * 80)) for f in gscript.vector_db(vect, stderr=nuldev).values(): layer = f["layer"] schema_table = f["table"] key = f["key"] database = f["database"] driver = f["driver"] # split schema.table if "." in schema_table: schema, table = schema_table.split(".", 1) else: schema = "" table = schema_table if new_schema: new_schema_table = "%s.%s" % (new_schema, table) else: new_schema_table = table gscript.debug( "DATABASE = '%s' SCHEMA = '%s' TABLE = '%s' ->\n" " NEW_DATABASE = '%s' NEW_SCHEMA_TABLE = '%s'" % (old_database, schema, table, new_database, new_schema_table)) do_reconnect = True if old_database_subst is not None: if database != old_database_subst: do_reconnect = False if database == new_database_subst: do_reconnect = False if schema != old_schema: do_reconnect = False if do_reconnect: gscript.verbose(_("Reconnecting layer %d...") % layer) if flags["c"]: # check if table exists in new database copy_tab( driver, database, schema_table, new_driver, new_database, new_schema_table, ) # drop original table if required if flags["d"]: drop_tab(vect, layer, schema_table, driver, substitute_db(database)) # reconnect tables (don't use substituted new_database) # NOTE: v.db.connect creates an index on the key column try: gscript.run_command( "v.db.connect", flags="o", quiet=True, map=vect, layer=layer, driver=new_driver, database=new_database, table=new_schema_table, key=key, ) except CalledModuleError: gscript.warning( _("Unable to connect table <%s> to vector " "<%s> on layer <%s>") % (table, vect, str(layer))) else: if database != new_database_subst: gscript.warning( _("Layer <%d> will not be reconnected " "because database or schema do not " "match.") % layer) return 0
def main(): global TMPLOC, SRCGISRC, GISDBASE, TMP_REG_NAME GDALdatasource = options['input'] output = options['output'] method = options['resample'] memory = options['memory'] bands = options['band'] tgtres = options['resolution'] title = options["title"] if flags['e'] and not output: output = 'rimport_tmp' # will be removed with the entire tmp location if options['resolution_value']: if tgtres != 'value': grass.fatal( _("To set custom resolution value, select 'value' in resolution option" )) tgtres_value = float(options['resolution_value']) if tgtres_value <= 0: grass.fatal(_("Resolution value can't be smaller than 0")) elif tgtres == 'value': grass.fatal( _("Please provide the resolution for the imported dataset or change to 'estimated' resolution" )) # try r.in.gdal directly first additional_flags = 'l' if flags['l'] else '' if flags['o']: additional_flags += 'o' region_flag = '' if options['extent'] == 'region': region_flag += 'r' if flags['o'] or is_projection_matching(GDALdatasource): parameters = dict(input=GDALdatasource, output=output, memory=memory, flags='ak' + additional_flags + region_flag) if bands: parameters['band'] = bands try: grass.run_command('r.in.gdal', **parameters) grass.verbose( _("Input <%s> successfully imported without reprojection") % GDALdatasource) return 0 except CalledModuleError as e: grass.fatal( _("Unable to import GDAL dataset <%s>") % GDALdatasource) grassenv = grass.gisenv() tgtloc = grassenv['LOCATION_NAME'] # make sure target is not xy if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected': grass.fatal( _("Coordinate reference system not available for current location <%s>" ) % tgtloc) tgtmapset = grassenv['MAPSET'] GISDBASE = grassenv['GISDBASE'] TMPLOC = grass.append_node_pid("tmp_r_import_location") TMP_REG_NAME = grass.append_node_pid("tmp_r_import_region") SRCGISRC, src_env = grass.create_environment(GISDBASE, TMPLOC, 'PERMANENT') # create temp location from input without import grass.verbose( _("Creating temporary location for <%s>...") % GDALdatasource) # creating a new location with r.in.gdal requires a sanitized env env = os.environ.copy() env = grass.sanitize_mapset_environment(env) parameters = dict(input=GDALdatasource, output=output, memory=memory, flags='c', title=title, location=TMPLOC, quiet=True) if bands: parameters['band'] = bands try: grass.run_command('r.in.gdal', env=env, **parameters) except CalledModuleError: grass.fatal(_("Unable to read GDAL dataset <%s>") % GDALdatasource) # prepare to set region in temp location if 'r' in region_flag: tgtregion = TMP_REG_NAME grass.run_command('v.in.region', output=tgtregion, flags='d') # switch to temp location # print projection at verbose level grass.verbose( grass.read_command('g.proj', flags='p', env=src_env).rstrip(os.linesep)) # make sure input is not xy if grass.parse_command('g.proj', flags='g', env=src_env)['name'] == 'xy_location_unprojected': grass.fatal( _("Coordinate reference system not available for input <%s>") % GDALdatasource) # import into temp location grass.verbose( _("Importing <%s> to temporary location...") % GDALdatasource) parameters = dict(input=GDALdatasource, output=output, memory=memory, flags='ak' + additional_flags) if bands: parameters['band'] = bands if 'r' in region_flag: grass.run_command('v.proj', location=tgtloc, mapset=tgtmapset, input=tgtregion, output=tgtregion, env=src_env) grass.run_command('g.region', vector=tgtregion, env=src_env) parameters['flags'] = parameters['flags'] + region_flag try: grass.run_command('r.in.gdal', env=src_env, **parameters) except CalledModuleError: grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource) outfiles = grass.list_grouped('raster', env=src_env)['PERMANENT'] # is output a group? group = False path = os.path.join(GISDBASE, TMPLOC, 'group', output) if os.path.exists(path): group = True path = os.path.join(GISDBASE, TMPLOC, 'group', output, 'POINTS') if os.path.exists(path): grass.fatal(_("Input contains GCPs, rectification is required")) if 'r' in region_flag: grass.run_command('g.remove', type="vector", flags="f", name=tgtregion, env=src_env) # switch to target location if 'r' in region_flag: grass.run_command('g.remove', type="vector", flags="f", name=tgtregion) region = grass.region() rflags = None if flags['n']: rflags = 'n' vreg = TMP_REG_NAME for outfile in outfiles: n = region['n'] s = region['s'] e = region['e'] w = region['w'] env = os.environ.copy() if options['extent'] == 'input': # r.proj -g try: tgtextents = grass.read_command('r.proj', location=TMPLOC, mapset='PERMANENT', input=outfile, flags='g', memory=memory, quiet=True) except CalledModuleError: grass.fatal(_("Unable to get reprojected map extent")) try: srcregion = grass.parse_key_val(tgtextents, val_type=float, vsep=' ') n = srcregion['n'] s = srcregion['s'] e = srcregion['e'] w = srcregion['w'] except ValueError: # import into latlong, expect 53:39:06.894826N srcregion = grass.parse_key_val(tgtextents, vsep=' ') n = grass.float_or_dms(srcregion['n'][:-1]) * \ (-1 if srcregion['n'][-1] == 'S' else 1) s = grass.float_or_dms(srcregion['s'][:-1]) * \ (-1 if srcregion['s'][-1] == 'S' else 1) e = grass.float_or_dms(srcregion['e'][:-1]) * \ (-1 if srcregion['e'][-1] == 'W' else 1) w = grass.float_or_dms(srcregion['w'][:-1]) * \ (-1 if srcregion['w'][-1] == 'W' else 1) env['GRASS_REGION'] = grass.region_env(n=n, s=s, e=e, w=w) # v.in.region in tgt grass.run_command('v.in.region', output=vreg, quiet=True, env=env) # reproject to src # switch to temp location try: grass.run_command('v.proj', input=vreg, output=vreg, location=tgtloc, mapset=tgtmapset, quiet=True, env=src_env) # test if v.proj created a valid area if grass.vector_info_topo(vreg, env=src_env)['areas'] != 1: grass.fatal(_("Please check the 'extent' parameter")) except CalledModuleError: grass.fatal(_("Unable to reproject to source location")) # set region from region vector grass.run_command('g.region', raster=outfile, env=src_env) grass.run_command('g.region', vector=vreg, env=src_env) # align to first band grass.run_command('g.region', align=outfile, env=src_env) # get number of cells cells = grass.region(env=src_env)['cells'] estres = math.sqrt((n - s) * (e - w) / cells) # remove from source location for multi bands import grass.run_command('g.remove', type='vector', name=vreg, flags='f', quiet=True, env=src_env) # switch to target location grass.run_command('g.remove', type='vector', name=vreg, flags='f', quiet=True) grass.message( _("Estimated target resolution for input band <{out}>: {res}"). format(out=outfile, res=estres)) if flags['e']: continue env = os.environ.copy() if options['extent'] == 'input': env['GRASS_REGION'] = grass.region_env(n=n, s=s, e=e, w=w) res = None if tgtres == 'estimated': res = estres elif tgtres == 'value': res = tgtres_value grass.message( _("Using given resolution for input band <{out}>: {res}"). format(out=outfile, res=res)) # align to requested resolution env['GRASS_REGION'] = grass.region_env(res=res, flags='a', env=env) else: curr_reg = grass.region() grass.message( _("Using current region resolution for input band " "<{out}>: nsres={ns}, ewres={ew}").format( out=outfile, ns=curr_reg['nsres'], ew=curr_reg['ewres'])) # r.proj grass.message(_("Reprojecting <%s>...") % outfile) try: grass.run_command('r.proj', location=TMPLOC, mapset='PERMANENT', input=outfile, method=method, resolution=res, memory=memory, flags=rflags, quiet=True, env=env) except CalledModuleError: grass.fatal(_("Unable to to reproject raster <%s>") % outfile) if grass.raster_info(outfile)['min'] is None: grass.fatal(_("The reprojected raster <%s> is empty") % outfile) if flags['e']: return 0 if group: grass.run_command('i.group', group=output, input=','.join(outfiles)) # TODO: write metadata with r.support return 0
# separating mapsets and transforming it into a list mapsets2 = mapsets.split(' ') mapsets2 = [i for i in mapsets2 if len(i) > 0] mapsets2 print len(mapsets2) # Parameters res=30 hectare=10000 for ms in mapsets2: print ms grass.run_command('g.mapset', mapset=ms) maps = grass.list_grouped ('rast', pattern='*') [ms] for mm in maps: print mm grass.run_command('r.region', map=mm, n=512*res, s=0, w=0, e=512*res) ############ # Setting area units in hectares # we want to lead with these mapsets (those in which pixels represent area): mapsets=["MS_HABMAT_AREA", "MS_HABMAT_DILA01_AREA", "MS_HABMAT_DILA01_AREAqual", "MS_HABMAT_DILA01_AREAqualONE", "MS_HABMAT_DILA02_AREA", "MS_HABMAT_DILA02_AREAqual", "MS_HABMAT_DILA02_AREAqualONE", "MS_HABMAT_FRAG_AREA", "MS_HABMAT_FRAG_AREAqual", "MS_HIQOTH_AREA", "MS_HQMQLQ_AREAqual", "MS_MEQOTH_AREA"] patt=['AREApix', 'AREApix', 'AREAqual', 'AREAqualONE', 'AREApix', 'AREAqual', 'AREAqualONE', 'AREApix', 'AREAqual', 'AREApix', 'AREAqual', 'AREApix'] newpatt=['AreaHA', 'AreaHA', 'AREAqualHA', 'AREAqualONEHA', 'AreaHA', 'AREAqualHA', 'AREAqualONEHA', 'AreaHA', 'AREAqualHA', 'AreaHA', 'AREAqualHA', 'AreaHA']
def list_landscape_variables(use_random_landscapes = True, variables = []): ''' New function: this function returns the list of landscapes in GRASS database locations Input: use_random_landscapes: if True, seach for maps in the GRASS database of random landscapes; otherwise, use real landscapes variables: variables to be registered. Options: 'edge_dist', 'pid', 'patch_area', 'fid', 'frag_area', 'cross1_pid', 'cross1_patch_area', 'cross2_pid', 'cross2_patch_area' ''' # Lists of landscape variable names list_names = {} # Possible variables variables_options = ['edge_dist', 'pid', 'patch_area', 'fid', 'frag_area', 'cross1_pid', 'cross1_patch_area', 'cross2_pid', 'cross2_patch_area'] # Correspondent possible mapsets and possible patterns # If random maps will be used if use_random_landscapes: # Possible mapsets mapset_names = ['MS_HABMAT_DIST', 'MS_HABMAT_PID', 'MS_HABMAT_AREA', 'MS_HABMAT_FRAG_PID', 'MS_HABMAT_FRAG_AREA', 'MS_HABMAT_DILA01_PID', 'MS_HABMAT_DILA01_AREA', 'MS_HABMAT_DILA02_PID', 'MS_HABMAT_DILA02_AREA'] # Possible patterns possible_patterns = ['*DIST', '*PID', '*grassclump_AreaHA', '*FRAG_PID', '*FRAG_AreaHA', '*dila01_clean_PID', '*dila01_clean_AreaHA', '*dila02_clean_AreaHA', '*dila02_clean_AreaHA'] # update possibilities of quality etc for BioDIM birds # If real maps will be used else: # Assessing the name of the current mapset - this may be used within the metrics functions current_mapset = grass.read_command('g.mapset', flags = 'p').replace('\n','').replace('\r','') # Possible mapsets mapset_names = [current_mapset] * len(variables) # Possible patterns # complete later! see patterns from LSMetrics # For each variable for i in (variables): # If the variable is one of the possible ones if i in variables_options: # Get index of the variable i in the list of variable options index = variables_options.index(i) # Define the list of map names as a dictionary entry list_names[i] = grass.list_grouped('rast', pattern = possible_patterns[index]) [mapset_names[index]] # Check if the list has at least one map; otherwise there may be a problem if len(list_names[i]) == 0: raise Exception('There are no maps with the pattern '+possible_patterns[index]+' in the mapset '+mapset_names[index]+'! Please check it.') # If the variable in another, gives a warning else: raise Exception('There is some issue with maps related to variable '+i+'! Please check it.') return list_names
if previous_landscape == '': landscape_grassname_habmat=habmat[0].replace("\n","") else: landscape_grassname_habmat=random.sample(habmat, 1)[0].replace("\n","") elif select_form == 'order': if previous_landscape == '' or previous_landscape == habmat[(len(habmat)-1)].replace("\n",""): landscape_grassname_habmat=habmat[0].replace("\n","") else: index = habmat.index(previous_landscape+'\n') landscape_grassname_habmat=habmat[(index+1)].replace("\n","") elif select_form == 'type': landscape_grassname_habmat=previous_landscape landscape_index=landscape_grassname_habmat[11:17] lista = grass.list_grouped('rast', pattern = '*HABMAT*') ['MS_HABMAT'] def fun_read_GRASS(): select_form = 'random' previous_landscape = '' habmat = lista if select_form == 'random': if previous_landscape == '': landscape_grassname_habmat=habmat[0].replace("\n","") else: landscape_grassname_habmat=random.sample(habmat, 1)[0].replace("\n","") elif select_form == 'order': if previous_landscape == '' or previous_landscape == habmat[(len(habmat)-1)].replace("\n",""):
# reads data from GRASS rasters and calculates stats - display as latex tables for area in areas: area_name = area[0].split('_')[-1] df_stats = pd.DataFrame() df_stats['stats'] = ['min','max','range','mean','median','stddev','skew','kurt','p25','p75'] for dem in area: if not dem.startswith('tdx12'): diff_map = 'tdx12_diff_' + dem.split('_')[0] + '_wgs84_' + area_name diff_map_array = raster_as_1d_array(diff_map) df_stats[diff_map] = calc_stats(diff_map_array) # export stats to latex print('\n\n') print df_stats.transpose().to_latex(float_format=lambda x:'%4.2f' % x) # lists of all diff maps diff_list = grass.list_grouped('raster', pattern='*diff*')['tandemX_brasil'] # fix colortable (color gradient from blue-white-red beteween -15/+15, then darker colors to min-max) rule = '''0% 0:30:110 \n-15 blue \n0 white \n15 red \n100% 125:25:15''' for diff_map in diff_list: grass.write_command('r.colors', map=diff_map, rules='-', stdin=rule) # export diffs maps as pngs for diff_map in diff_list: area_name = diff_map.split('_')[-1] grass.run_command('g.region', raster=diff_map, flags='pa', res='0:0:01') # diff map grass.run_command('d.mon', start='cairo', output=diff_map+'.png', resolution='3', \ height=500, width=500, overwrite=True) grass.run_command('d.rast', map=diff_map) grass.run_command('d.grid', size='0.25', text_color='black', flags='c')
def OnClick(self,event): #self.logger.AppendText(" Click on object with Id %d\n" %event.GetId()) #______________________________________________________________________________________________________________ if event.GetId()==10: #10==START d= wx.MessageDialog( self, " Please select the folder where the files will be saved \n" " ","", wx.OK) # Create a message dialog box d.ShowModal() # Shows it d.Destroy() # finally destroy it when finished. Form1.dir_out=selecdirectori() self.logger.AppendText('Directory output :'+Form1.dir_out+' \n ') self.logger.AppendText('runing... :'+Form1.import_map+' \n ') if Form1.Chech_single==1: import Cria_grupo import v_what import prepara_kmeans Form1.out_map=Form1.import_map.split('\\');Form1.out_map=Form1.out_map[-1].replace('.','_') grass.run_command('r.in.gdal',input=Form1.import_map,out=Form1.out_map,overwrite=True) Form1.group_img=grass.list_grouped('rast', pattern='*'+Form1.out_map+'*') ['PERMANENT'] Cria_grupo.CriaGrupo(Form1.group_img) grass.run_command('g.region', rast=Form1.group_img[0],verbose=False) grass.run_command('i.segment', group='Grupo',output=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,threshold=Form1.thrs, minsize=Form1.Misize,overwrite=True) Form1.out_name_vect=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize` Form1.out_name_vect=Form1.out_name_vect.replace('.','_') Form1.out_name_vect=Form1.out_name_vect.replace('-','_') Form1.out_name_vect='A'+Form1.out_name_vect grass.run_command('r.to.vect',input=Form1.group_img[0]+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,output=Form1.out_name_vect,type='area',overwrite=True ) if Form1.classify==1: v_what.v_what(Form1.group_img,Form1.out_name_vect) os.chdir(Form1.dir_out) Form1.dir_out_vect=Form1.dir_out.replace("\\",'/').replace("\n",'/n').replace('\a','/a') print Form1.dir_out_vect grass.run_command('v.out.ogr', input=Form1.out_name_vect, output=Form1.dir_out_vect+'/'+Form1.out_name_vect+'.shp',type='area') if Form1.classify==1: prepara_kmeans.prep_kmean(Form1.out_name_vect, Form1.dir_out, Form1.nclass) p = Popen([r"C:\Program Files\R\R-2.15.3\bin\x64\Rscript.exe",Form1.dir_out_vect+'/'+"Kmeans_final.txt"]) if Form1.Chech_mult==1: import import_folder_imagens import Cria_grupo import v_what import prepara_kmeans Form1.list_pattern=import_folder_imagens.import_fd(folder_files=Form1.dir_in) for i in Form1.list_pattern: Form1.group_img=grass.list_grouped('rast', pattern='*'+i+'*') ['PERMANENT'] Cria_grupo.CriaGrupo(Form1.group_img) grass.run_command('g.region', rast=Form1.group_img[0],verbose=False) grass.run_command('i.segment', group='Grupo',output=i+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,threshold=Form1.thrs, minsize=Form1.Misize,overwrite=True ) Form1.out_name_vect=i+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize` Form1.out_name_vect=Form1.out_name_vect.replace('.','_') Form1.out_name_vect=Form1.out_name_vect.replace('-','_') Form1.out_name_vect='A'+Form1.out_name_vect grass.run_command('r.to.vect',input=i+'_segment_thre'+`Form1.thrs`+'_'+`Form1.Misize`,output=Form1.out_name_vect,type='area',overwrite=True ) if Form1.classify==1: v_what.v_what(Form1.group_img,Form1.out_name_vect) Form1.dir_out_vect=Form1.dir_out.replace("\\",'/').replace("\n",'/n').replace('\a','/a') grass.run_command('v.out.ogr', input=Form1.out_name_vect, output=Form1.dir_out+'/'+Form1.out_name_vect+'.shp',type='area',overwrite=True) if Form1.classify==1: prepara_kmeans.prep_kmean(Form1.out_name_vect, Form1.dir_out, Form1.nclass) p = Popen([r"C:\Program Files\R\R-2.15.3\bin\x64\Rscript.exe",Form1.dir_out_vect+'/'+"Kmeans_final.txt"]) d= wx.MessageDialog( self, " Finish \n" " ","", wx.OK) # Create a message dialog box d.ShowModal() # Shows it d.Destroy() # finally destroy it when finished. #______________________________________________________________________________________________________________ if event.GetId()==11: if Form1.Chech_mult==1: Form1.dir_in=selecdirectori() self.logger.AppendText('Directory input :'+Form1.dir_in+' \n ') if Form1.Chech_single==1: Form1.import_map=selecdirectori() self.logger.AppendText('File input :'+Form1.import_map+' \n ')
tbl = np.array(zip(*convertedvals), dtype=[(c, dtypes[c]) for c in cols]) tbl.sort() return tbl if __name__ == '__main__': # start time st = dt.datetime.now() # get options and flags o, f = grass.parser() fmt = lambda d: '\n'.join(['%s: %s' % (k, v) for k, v in d.items()])+'\n' grass.message('GIS Environment:\n'+fmt(grass.gisenv())) grass.message('Parameters:\n'+fmt(o)+fmt(f)) # warn if MASKED if 'MASK' in grass.list_grouped('rast')[grass.gisenv()['MAPSET']]: maskcells = gread('r.stats', input='MASK', flags='nc').split()[1] grass.message('!!! MASK active with %s cells, will only process those !!!' % maskcells) # send all to main keywords = o keywords.update(f) main = main(**keywords) main.execute() # report time it took delta = dt.datetime.now() - st grass.message('Execution took %s hh:mm:ss' % delta)
def main(): # temporary region gscript.use_temp_region() # set parameters overwrite = True rain_value = 50.0 man_value = 0.05 niterations = 25 nwalkers = 40000 mapset = "fusion" # assign variables elevation = "fusion" depth = "depth" dx = "dx" dy = "dy" # set temporal parameters temporaltype = "relative" strds = "depth_timeseries" title = "depth_timeseries" description = "timeseries of depth maps" # assign temporal variables datatype = "strds" increment = str(niterations) + " minutes" raster = "raster" # water flow gscript.run_command("g.region", raster=elevation, res=1) gscript.run_command( "r.sim.water", elevation=elevation, dx=dx, dy=dy, rain_value=rain_value, depth=depth, man_value=man_value, nwalkers=nwalkers, niterations=niterations, flags="t", overwrite=overwrite, ) # create a raster space time dataset gscript.run_command( "t.create", type=datatype, temporaltype=temporaltype, output=strds, title=title, description=description, overwrite=overwrite, ) # list rasters timeseries = gscript.list_grouped("rast", pattern="depth.*")[mapset] # register the rasters gscript.run_command( "t.register", type=raster, input=strds, maps=timeseries, increment=increment, overwrite=overwrite )
# separating mapsets and transforming it into a list mapsets2 = mapsets.split(' ') mapsets2 = [i for i in mapsets2 if len(i) > 0] mapsets2 print len(mapsets2) # Parameters res = 30 hectare = 10000 for ms in mapsets2: print ms grass.run_command('g.mapset', mapset=ms) maps = grass.list_grouped('rast', pattern='*')[ms] for mm in maps: print mm grass.run_command('r.region', map=mm, n=512 * res, s=0, w=0, e=512 * res) ############ # Setting area units in hectares # we want to lead with these mapsets (those in which pixels represent area):
def main(): repeat = int(options.pop("repeat")) nprocs = int(options.pop("nprocs")) subregions = options["subregions"] tosplit = flags["d"] # filter unused optional params for key in list(options.keys()): if options[key] == "": options.pop(key) if tosplit and "output_series" in options: gscript.fatal( _("Parallelization on subregion level is not supported together with <output_series> option" )) if (not gscript.overwrite() and gscript.list_grouped( "raster", pattern=options["output"] + "_run1")[gscript.gisenv()["MAPSET"]]): gscript.fatal( _("Raster map <{r}> already exists." " To overwrite, use the --overwrite flag").format( r=options["output"] + "_run1")) global TMP_RASTERS cats = [] if tosplit: gscript.message(_("Splitting subregions")) cats = (gscript.read_command("r.stats", flags="n", input=subregions).strip().splitlines()) if len(cats) < 2: gscript.fatal( _("Not enough subregions to split computation. Do not use -d flag." )) mapcalcs = [] for cat in cats: new = PREFIX + cat TMP_RASTERS.append(new) mapcalcs.append("{new} = if({sub} == {cat}, {sub}, null())".format( sub=subregions, cat=cat, new=new)) pool = Pool(nprocs) p = pool.map_async(split_subregions, mapcalcs) try: p.wait() except (KeyboardInterrupt, CalledModuleError): return options_list = [] for i in range(repeat): if cats: for cat in cats: op = options.copy() op["random_seed"] = i + 1 if "output_series" in op: op["output_series"] += "_run" + str(i + 1) + "_" + cat TMP_RASTERS.append(op["output_series"]) op["output"] += "_run" + str(i + 1) + "_" + cat op["subregions"] = PREFIX + cat options_list.append((repeat, i + 1, cat, op)) TMP_RASTERS.append(op["output"]) else: op = options.copy() op["random_seed"] = i + 1 if "output_series" in op: op["output_series"] += "_run" + str(i + 1) op["output"] += "_run" + str(i + 1) options_list.append((repeat, i + 1, None, op)) pool = Pool(nprocs) p = pool.map_async(futures_process, options_list) try: p.wait() except (KeyboardInterrupt, CalledModuleError): return if cats: gscript.message(_("Patching subregions")) for i in range(repeat): patch_input = [ options["output"] + "_run" + str(i + 1) + "_" + cat for cat in cats ] gscript.run_command( "r.patch", input=patch_input, output=options["output"] + "_run" + str(i + 1), ) return 0
def main(): global TMPLOC, SRCGISRC, GISDBASE overwrite = grass.overwrite() # list formats and exit if flags['f']: grass.run_command('v.in.ogr', flags='f') return 0 # list layers and exit if flags['l']: try: grass.run_command('v.in.ogr', flags='l', input=options['input']) except CalledModuleError: return 1 return 0 OGRdatasource = options['input'] output = options['output'] layers = options['layer'] vflags = '' if options['extent'] == 'region': vflags += 'r' if flags['o']: vflags += 'o' vopts = {} if options['encoding']: vopts['encoding'] = options['encoding'] if options['datum_trans'] and options['datum_trans'] == '-1': # list datum transform parameters if not options['epsg']: grass.fatal(_("Missing value for parameter <%s>") % 'epsg') return grass.run_command('g.proj', epsg=options['epsg'], datum_trans=options['datum_trans']) grassenv = grass.gisenv() tgtloc = grassenv['LOCATION_NAME'] tgtmapset = grassenv['MAPSET'] GISDBASE = grassenv['GISDBASE'] tgtgisrc = os.environ['GISRC'] SRCGISRC = grass.tempfile() TMPLOC = 'temp_import_location_' + str(os.getpid()) f = open(SRCGISRC, 'w') f.write('MAPSET: PERMANENT\n') f.write('GISDBASE: %s\n' % GISDBASE) f.write('LOCATION_NAME: %s\n' % TMPLOC) f.write('GUI: text\n') f.close() tgtsrs = grass.read_command('g.proj', flags='j', quiet=True) # create temp location from input without import grass.verbose(_("Creating temporary location for <%s>...") % OGRdatasource) if layers: vopts['layer'] = layers if output: vopts['output'] = output vopts['snap'] = options['snap'] try: grass.run_command('v.in.ogr', input=OGRdatasource, location=TMPLOC, flags='i', quiet=True, overwrite=overwrite, **vopts) except CalledModuleError: grass.fatal(_("Unable to create location from OGR datasource <%s>") % OGRdatasource) # switch to temp location os.environ['GISRC'] = str(SRCGISRC) if options['epsg']: # force given EPSG kwargs = {} if options['datum_trans']: kwargs['datum_trans'] = options['datum_trans'] grass.run_command('g.proj', flags='c', epsg=options['epsg'], **kwargs) # switch to target location os.environ['GISRC'] = str(tgtgisrc) # try v.in.ogr directly if flags['o'] or grass.run_command('v.in.ogr', input=OGRdatasource, flags='j', errors='status', quiet=True, overwrite=overwrite) == 0: try: grass.run_command('v.in.ogr', input=OGRdatasource, flags=vflags, overwrite=overwrite, **vopts) grass.message( _("Input <%s> successfully imported without reprojection") % OGRdatasource) return 0 except CalledModuleError: grass.fatal(_("Unable to import <%s>") % OGRdatasource) # make sure target is not xy if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected': grass.fatal( _("Coordinate reference system not available for current location <%s>") % tgtloc) # switch to temp location os.environ['GISRC'] = str(SRCGISRC) # print projection at verbose level grass.verbose(grass.read_command('g.proj', flags='p').rstrip(os.linesep)) # make sure input is not xy if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected': grass.fatal(_("Coordinate reference system not available for input <%s>") % OGRdatasource) if options['extent'] == 'region': # switch to target location os.environ['GISRC'] = str(tgtgisrc) # v.in.region in tgt vreg = 'vreg_' + str(os.getpid()) grass.run_command('v.in.region', output=vreg, quiet=True) # reproject to src # switch to temp location os.environ['GISRC'] = str(SRCGISRC) try: grass.run_command('v.proj', input=vreg, output=vreg, location=tgtloc, mapset=tgtmapset, quiet=True, overwrite=overwrite) except CalledModuleError: grass.fatal(_("Unable to reproject to source location")) # set region from region vector grass.run_command('g.region', res='1') grass.run_command('g.region', vector=vreg) # import into temp location grass.message(_("Importing <%s> ...") % OGRdatasource) try: grass.run_command('v.in.ogr', input=OGRdatasource, flags=vflags, overwrite=overwrite, **vopts) except CalledModuleError: grass.fatal(_("Unable to import OGR datasource <%s>") % OGRdatasource) # if output is not define check source mapset if not output: output = grass.list_grouped('vector')['PERMANENT'][0] # switch to target location os.environ['GISRC'] = str(tgtgisrc) # check if map exists if not grass.overwrite() and \ grass.find_file(output, element='vector', mapset='.')['mapset']: grass.fatal(_("option <%s>: <%s> exists.") % ('output', output)) if options['extent'] == 'region': grass.run_command('g.remove', type='vector', name=vreg, flags='f', quiet=True) # v.proj grass.message(_("Reprojecting <%s>...") % output) try: grass.run_command('v.proj', location=TMPLOC, mapset='PERMANENT', input=output, overwrite=overwrite) except CalledModuleError: grass.fatal(_("Unable to to reproject vector <%s>") % output) return 0
stdev_diff_colors_3d = """\ nv 192:192:192 default 192:192:192 0 247:252:253 3 224:236:244 6 191:211:230 9 158:188:218 12 140:150:198 15 140:107:177 18 136:65:157 21 129:15:124 24 77:0:75 """ # list scanned DEMs dems = gscript.list_grouped("rast", pattern="dem_*")["PERMANENT"] # iterate through scanned DEMs for dem in dems: # reference variables region = dem slope = dem.replace("dem", "slope") forms = dem.replace("dem", "forms") depth = dem.replace("dem", "depth") dem_difference = dem.replace("dem", "dem_difference") depressions = dem.replace("dem", "depressions") concentrated_flow = dem.replace("dem", "concentrated_flow") peaks = dem.replace("dem", "peaks") ridges = dem.replace("dem", "ridges") valleys = dem.replace("dem", "valleys")
def main(): # old connection old_database = options['old_database'] old_schema = options['old_schema'] # new connection default_connection = grass.db_connection() if options['new_driver']: new_driver = options['new_driver'] else: new_driver = default_connection['driver'] if options['new_database']: new_database = options['new_database'] else: new_database = default_connection['database'] if options['new_schema']: new_schema = options['new_schema'] else: new_schema = default_connection['schema'] if old_database == '': old_database = None old_database_subst = None if old_database is not None: old_database_subst = substitute_db(old_database) new_database_subst = substitute_db(new_database) if old_database_subst == new_database_subst and old_schema == new_schema: grass.fatal(_("Old and new database connection is identical. Nothing to do.")) mapset = grass.gisenv()['MAPSET'] vectors = grass.list_grouped('vect')[mapset] num_vectors = len(vectors) if flags['c']: # create new database if not existing create_db(new_driver, new_database) i = 0 for vect in vectors: vect = "%s@%s" % (vect, mapset) i += 1 grass.message(_("%s\nReconnecting vector map <%s> (%d of %d)...\n%s") % \ ('-' * 80, vect, i, num_vectors, '-' * 80)) for f in grass.vector_db(vect, stderr = nuldev).itervalues(): layer = f['layer'] schema_table = f['table'] key = f['key'] database = f['database'] driver = f['driver'] # split schema.table if '.' in schema_table: schema, table = schema_table.split('.', 1) else: schema = '' table = schema_table if new_schema: new_schema_table = "%s.%s" % (new_schema, table) else: new_schema_table = table grass.debug("DATABASE = '%s' SCHEMA = '%s' TABLE = '%s' ->\n" " NEW_DATABASE = '%s' NEW_SCHEMA_TABLE = '%s'" % \ (old_database, schema, table, new_database, new_schema_table)) do_reconnect = True if old_database_subst is not None: if database != old_database_subst: do_reconnect = False if database == new_database_subst: do_reconnect = False if schema != old_schema: do_reconnect = False if do_reconnect == True: grass.verbose(_("Reconnecting layer %d...") % layer) if flags['c']: # check if table exists in new database copy_tab(driver, database, schema_table, new_driver, new_database, new_schema_table) # drop original table if required if flags['d']: drop_tab(vect, layer, schema_table, driver, substitute_db(database)) # reconnect tables (don't use substituted new_database) # NOTE: v.db.connect creates an index on the key column try: grass.run_command('v.db.connect', flags = 'o', quiet = True, map = vect, layer = layer, driver = new_driver, database = new_database, table = new_schema_table, key = key) except CalledModuleError: grass.warning(_("Unable to connect table <%s> to vector <%s> on layer <%s>") % (table, vect, str(layer))) else: if database != new_database_subst: grass.warning(_("Layer <%d> will not be reconnected because " "database or schema do not match.") % layer) return 0
def main(): global TMPLOC, SRCGISRC, GISDBASE, TMP_REG_NAME GDALdatasource = options['input'] output = options['output'] method = options['resample'] memory = options['memory'] bands = options['band'] tgtres = options['resolution'] title = options["title"] if options['resolution_value']: if tgtres != 'value': grass.fatal(_("To set custom resolution value, select 'value' in resolution option")) tgtres_value = float(options['resolution_value']) if tgtres_value <= 0: grass.fatal(_("Resolution value can't be smaller than 0")) elif tgtres == 'value': grass.fatal(_("Please provide the resolution for the imported dataset or change to 'estimated' resolution")) grassenv = grass.gisenv() tgtloc = grassenv['LOCATION_NAME'] tgtmapset = grassenv['MAPSET'] GISDBASE = grassenv['GISDBASE'] tgtgisrc = os.environ['GISRC'] SRCGISRC = grass.tempfile() TMPLOC = 'temp_import_location_' + str(os.getpid()) f = open(SRCGISRC, 'w') f.write('MAPSET: PERMANENT\n') f.write('GISDBASE: %s\n' % GISDBASE) f.write('LOCATION_NAME: %s\n' % TMPLOC) f.write('GUI: text\n') f.close() tgtsrs = grass.read_command('g.proj', flags='j', quiet=True) # create temp location from input without import grass.verbose(_("Creating temporary location for <%s>...") % GDALdatasource) parameters = dict(input=GDALdatasource, output=output, memory=memory, flags='c', title=title, location=TMPLOC, quiet=True) if bands: parameters['band'] = bands try: grass.run_command('r.in.gdal', **parameters) except CalledModuleError: grass.fatal(_("Unable to read GDAL dataset <%s>") % GDALdatasource) # switch to temp location os.environ['GISRC'] = str(SRCGISRC) # switch to target location os.environ['GISRC'] = str(tgtgisrc) # try r.in.gdal directly first additional_flags = 'l' if flags['l'] else '' if flags['o']: additional_flags += 'o' if flags['o'] or grass.run_command('r.in.gdal', input=GDALdatasource, flags='j', errors='status', quiet=True) == 0: parameters = dict(input=GDALdatasource, output=output, memory=memory, flags='k' + additional_flags) if bands: parameters['band'] = bands try: grass.run_command('r.in.gdal', **parameters) grass.verbose(_("Input <%s> successfully imported without reprojection") % GDALdatasource) return 0 except CalledModuleError as e: grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource) # make sure target is not xy if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected': grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc) # switch to temp location os.environ['GISRC'] = str(SRCGISRC) # make sure input is not xy if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected': grass.fatal(_("Coordinate reference system not available for input <%s>") % GDALdatasource) # import into temp location grass.verbose(_("Importing <%s> to temporary location...") % GDALdatasource) parameters = dict(input=GDALdatasource, output=output, memory=memory, flags='k' + additional_flags) if bands: parameters['band'] = bands try: grass.run_command('r.in.gdal', **parameters) except CalledModuleError: grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource) outfiles = grass.list_grouped('raster')['PERMANENT'] # is output a group? group = False path = os.path.join(GISDBASE, TMPLOC, 'group', output) if os.path.exists(path): group = True path = os.path.join(GISDBASE, TMPLOC, 'group', output, 'POINTS') if os.path.exists(path): grass.fatal(_("Input contains GCPs, rectification is required")) # switch to target location os.environ['GISRC'] = str(tgtgisrc) region = grass.region() rflags = None if flags['n']: rflags = 'n' for outfile in outfiles: n = region['n'] s = region['s'] e = region['e'] w = region['w'] grass.use_temp_region() if options['extent'] == 'input': # r.proj -g try: tgtextents = grass.read_command('r.proj', location=TMPLOC, mapset='PERMANENT', input=outfile, flags='g', memory=memory, quiet=True) except CalledModuleError: grass.fatal(_("Unable to get reprojected map extent")) try: srcregion = grass.parse_key_val(tgtextents, val_type=float, vsep=' ') n = srcregion['n'] s = srcregion['s'] e = srcregion['e'] w = srcregion['w'] except ValueError: # import into latlong, expect 53:39:06.894826N srcregion = grass.parse_key_val(tgtextents, vsep=' ') n = grass.float_or_dms(srcregion['n'][:-1]) * \ (-1 if srcregion['n'][-1] == 'S' else 1) s = grass.float_or_dms(srcregion['s'][:-1]) * \ (-1 if srcregion['s'][-1] == 'S' else 1) e = grass.float_or_dms(srcregion['e'][:-1]) * \ (-1 if srcregion['e'][-1] == 'W' else 1) w = grass.float_or_dms(srcregion['w'][:-1]) * \ (-1 if srcregion['w'][-1] == 'W' else 1) grass.run_command('g.region', n=n, s=s, e=e, w=w) # v.in.region in tgt vreg = TMP_REG_NAME = 'vreg_tmp_' + str(os.getpid()) grass.run_command('v.in.region', output=vreg, quiet=True) grass.del_temp_region() # reproject to src # switch to temp location os.environ['GISRC'] = str(SRCGISRC) try: grass.run_command('v.proj', input=vreg, output=vreg, location=tgtloc, mapset=tgtmapset, quiet=True) except CalledModuleError: grass.fatal(_("Unable to reproject to source location")) # set region from region vector grass.run_command('g.region', raster=outfile) grass.run_command('g.region', vector=vreg) # align to first band grass.run_command('g.region', align=outfile) # get number of cells cells = grass.region()['cells'] estres = math.sqrt((n - s) * (e - w) / cells) # remove from source location for multi bands import grass.run_command('g.remove', type='vector', name=vreg, flags='f', quiet=True) os.environ['GISRC'] = str(tgtgisrc) grass.run_command('g.remove', type='vector', name=vreg, flags='f', quiet=True) grass.message(_("Estimated target resolution for input band <{out}>: {res}").format(out=outfile, res=estres)) if flags['e']: continue if options['extent'] == 'input': grass.use_temp_region() grass.run_command('g.region', n=n, s=s, e=e, w=w) res = None if tgtres == 'estimated': res = estres elif tgtres == 'value': res = tgtres_value grass.message(_("Using given resolution for input band <{out}>: {res}").format(out=outfile, res=res)) # align to requested resolution grass.run_command('g.region', res=res, flags='a') else: curr_reg = grass.region() grass.message(_("Using current region resolution for input band " "<{out}>: nsres={ns}, ewres={ew}").format(out=outfile, ns=curr_reg['nsres'], ew=curr_reg['ewres'])) # r.proj grass.message(_("Reprojecting <%s>...") % outfile) try: grass.run_command('r.proj', location=TMPLOC, mapset='PERMANENT', input=outfile, method=method, resolution=res, memory=memory, flags=rflags, quiet=True) except CalledModuleError: grass.fatal(_("Unable to to reproject raster <%s>") % outfile) if grass.raster_info(outfile)['min'] is None: grass.fatal(_("The reprojected raster <%s> is empty") % outfile) if options['extent'] == 'input': grass.del_temp_region() if flags['e']: return 0 if group: grass.run_command('i.group', group=output, input=','.join(outfiles)) # TODO: write metadata with r.support return 0