def main(): input = options['input'] maskcats = options['maskcats'] remove = flags['r'] invert = flags['i'] if not remove and not input: grass.fatal(_("Required parameter <input> not set")) #check if input file exists if not grass.find_file(input)['file'] and not remove: grass.fatal(_("<%s> does not exist.") % input) if not 'MASKCATS' in grass.gisenv() and not remove: ## beware: next check is made with != , not with 'is', otherwise: #>>> grass.raster_info("basin_50K")['datatype'] is "CELL" #False # even if: #>>> "CELL" is "CELL" #True if grass.raster_info(input)['datatype'] != "CELL": grass.fatal(_("Raster map %s must be integer for maskcats parameter") % input) mapset = grass.gisenv()['MAPSET'] exists = bool(grass.find_file('MASK', element = 'cell', mapset = mapset)['file']) if remove: if exists: grass.run_command('g.remove', rast = 'MASK') grass.message(_("Raster MASK removed")) else: grass.fatal(_("No existing MASK to remove")) else: if exists: if not grass.overwrite(): grass.fatal(_("MASK already found in current mapset. Delete first or overwrite.")) else: grass.warning(_("MASK already exists and will be overwritten")) p = grass.feed_command('r.reclass', input = input, output = 'MASK', overwrite = True, rules = '-') p.stdin.write("%s = 1" % maskcats) p.stdin.close() p.wait() if invert: global tmp tmp = "r_mask_%d" % os.getpid() grass.run_command('g.rename', rast = ('MASK',tmp), quiet = True) grass.mapcalc("MASK=if(isnull($tmp),1,null())", tmp = tmp) grass.run_command('g.remove', rast = tmp, quiet = True) grass.message(_("Inverted MASK created.")) else: grass.message(_("MASK created.")) grass.message(_("All subsequent raster operations will be limited to MASK area. ") + "Removing or renaming raster file named MASK will " + "restore raster operations to normal")
def main(): if options['hdfs'] == '@grass_data_hdfs': LOCATION_NAME = grass.gisenv()['LOCATION_NAME'] MAPSET = grass.gisenv()['MAPSET'] MAPSET_PATH = os.path.join('grass_data_hdfs', LOCATION_NAME, MAPSET, 'external') options['hdfs'] = MAPSET_PATH if options['local']: transf = GrassHdfs(options['driver']) transf.upload(options['local'], options['hdfs'])
def substitute_db(database): gisenv = grass.gisenv() tmpl = string.Template(database) return tmpl.substitute(GISDBASE = gisenv['GISDBASE'], LOCATION_NAME = gisenv['LOCATION_NAME'], MAPSET = gisenv['MAPSET'])
def main(): # Get the options input = options["input"] output = options["output"] method = options["method"] order = options["order"] where = options["where"] add_time = flags["t"] nulls = flags["n"] # Make sure the temporal database exists tgis.init() sp = tgis.open_old_stds(input, "strds") rows = sp.get_registered_maps("id", where, order, None) if rows: # Create the r.series input file filename = grass.tempfile(True) file = open(filename, 'w') for row in rows: string = "%s\n" % (row["id"]) file.write(string) file.close() flag = "" if len(rows) > 1000: grass.warning(_("Processing over 1000 maps: activating -z flag of r.series which slows down processing")) flag += "z" if nulls: flag += "n" try: grass.run_command("r.series", flags=flag, file=filename, output=output, overwrite=grass.overwrite(), method=method) except CalledModuleError: grass.fatal(_("%s failed. Check above error messages.") % 'r.series') if not add_time: # Create the time range for the output map if output.find("@") >= 0: id = output else: mapset = grass.gisenv()["MAPSET"] id = output + "@" + mapset map = sp.get_new_map_instance(id) map.load() map.set_temporal_extent(sp.get_temporal_extent()) # Register the map in the temporal database if map.is_in_db(): map.update_all() else: map.insert()
def OnDeleteMap(self, event): """Delete layer or mapset""" name = self.selected_layer.label gisrc, env = gscript.create_environment( gisenv()["GISDBASE"], self.selected_location.label, self.selected_mapset.label ) if ( self._confirmDialog( question=_( "Do you really want to delete map <{m}> of type <{etype}> from mapset " "<{mapset}> in location <{loc}>?" ).format( m=name, mapset=self.selected_mapset.label, etype=self.selected_type.label, loc=self.selected_location.label, ), title=_("Delete map"), ) == wx.ID_YES ): label = _("Deleting {name}...").format(name=name) self.showNotification.emit(message=label) if self.selected_type.label == "vector": removed, cmd = self._runCommand("g.remove", flags="f", type="vector", name=name, env=env) elif self.selected_type.label == "raster": removed, cmd = self._runCommand("g.remove", flags="f", type="raster", name=name, env=env) else: removed, cmd = self._runCommand("g.remove", flags="f", type="raster_3d", name=name, env=env) if removed == 0: self._model.RemoveNode(self.selected_layer) self.RefreshNode(self.selected_type, recursive=True) Debug.msg(1, "LAYER " + name + " DELETED") self.showNotification.emit(message=_("g.remove completed").format(cmd=cmd)) gscript.try_remove(gisrc)
def ReloadCurrentMapset(self): """Reload current mapset tree only.""" def get_first_child(node): try: child = mapsetItem.children[0] except IndexError: child = None return child genv = gisenv() locationItem, mapsetItem = self.GetCurrentLocationMapsetNode() if not locationItem or not mapsetItem: return if mapsetItem.children: node = get_first_child(mapsetItem) while node: self._model.RemoveNode(node) node = get_first_child(mapsetItem) q = Queue() p = Process( target=getLocationTree, args=(genv["GISDBASE"], locationItem.data["name"], q, mapsetItem.data["name"]) ) p.start() maps, error = q.get() if error: raise CalledModuleError(error) self._populateMapsetItem(mapsetItem, maps[mapsetItem.data["name"]]) self._orig_model = copy.deepcopy(self._model) self.RefreshNode(mapsetItem) self.RefreshItems()
def OnSwitchLocationMapset(self, event): genv = gisenv() if self.selected_location.label == genv["LOCATION_NAME"]: self.changeMapset.emit(mapset=self.selected_mapset.label) else: self.changeLocation.emit(mapset=self.selected_mapset.label, location=self.selected_location.label) self.ExpandCurrentMapset()
def OnDisplayLayer(self, event): """Display layer in current graphics view""" layerName = [] if self.selected_location.label == gisenv()["LOCATION_NAME"] and self.selected_mapset: string = self.selected_layer.label + "@" + self.selected_mapset.label layerName.append(string) label = _("Displaying {name}...").format(name=string) self.showNotification.emit(message=label) label = ( "d." + self.selected_type.label[:4] + " --q map=" + string + _(" -- completed. Go to Layers tab for further operations.") ) if self.selected_type.label == "vector": self._giface.lmgr.AddMaps(layerName, "vector", True) elif self.selected_type.label == "raster": self._giface.lmgr.AddMaps(layerName, "raster", True) else: self._giface.lmgr.AddMaps(layerName, "raster_3d", True) # generate this message (command) automatically? label = "d.rast --q map=" + string + _(" -- completed. Go to Layers tab for further operations.") self.showNotification.emit(message=label) Debug.msg(1, "LAYER " + self.selected_layer.label + " DISPLAYED") else: GError(_("Failed to display layer: not in current mapset or invalid layer"), parent=self)
def __layout(self): """Do layout""" sizer = wx.BoxSizer(wx.VERTICAL) dataSizer = wx.BoxSizer(wx.VERTICAL) dataSizer.Add( item=wx.StaticText(parent=self.panel, id=wx.ID_ANY, label=_("Enter name of new vector map:")), proportion=0, flag=wx.ALL, border=3, ) self.vectorNameCtrl = Select( parent=self.panel, type="raster", mapsets=[grass.gisenv()["MAPSET"]], size=globalvar.DIALOG_GSELECT_SIZE ) if self.rasterName: self.vectorNameCtrl.SetValue(self.rasterName) dataSizer.Add(item=self.vectorNameCtrl, proportion=0, flag=wx.ALL | wx.EXPAND, border=3) # buttons btnSizer = wx.StdDialogButtonSizer() btnSizer.AddButton(self.btnCancel) btnSizer.AddButton(self.btnOK) btnSizer.Realize() sizer.Add(item=dataSizer, proportion=1, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5) sizer.Add(item=btnSizer, proportion=0, flag=wx.EXPAND | wx.ALL | wx.ALIGN_CENTER, border=5) self.panel.SetSizer(sizer) sizer.Fit(self) self.SetMinSize(self.GetSize())
def RunCmd(self, command, compReg = True, switchPage = False, onDone = None): """!Run command typed into console command prompt (GPrompt). @todo Display commands (*.d) are captured and processed separately by mapdisp.py. Display commands are rendered in map display widget that currently has the focus (as indicted by mdidx). @param command command given as a list (produced e.g. by utils.split()) @param compReg True use computation region @param switchPage switch to output page @param onDone function to be called when command is finished @return 0 on success @return 1 on failure """ if len(command) == 0: Debug.msg(2, "GPrompt:RunCmd(): empty command") return 0 # update history file env = grass.gisenv() try: fileHistory = codecs.open(os.path.join(env['GISDBASE'], env['LOCATION_NAME'], env['MAPSET'], '.bash_history'), encoding = 'utf-8', mode = 'a') except IOError, e: self.WriteError(e) fileHistory = None
def OnSelectMap (self, event): """Select vector map layer for editing If there is a vector map layer already edited, this action is firstly terminated. The map layer is closed. After this the selected map layer activated for editing. """ if event.GetSelection() == 0: # create new vector map layer if self.mapLayer: openVectorMap = self.mapLayer.GetName(fullyQualified = False)['name'] else: openVectorMap = None dlg = CreateNewVector(self.parent, exceptMap=openVectorMap, giface=self._giface, cmd=(('v.edit', {'tool': 'create'}, 'map')), disableAdd = True) if dlg and dlg.GetName(): # add layer to map layer tree if self._giface.GetLayerTree(): mapName = dlg.GetName() + '@' + grass.gisenv()['MAPSET'] self._giface.GetLayerList().AddLayer(ltype='vector', name=mapName, cmd=['d.vect', 'map=%s' % mapName]) vectLayers = self.UpdateListOfLayers(updateTool = True) selection = vectLayers.index(mapName) # create table ? if dlg.IsChecked('table'): # TODO: replace this by signal # also note that starting of tools such as atm, iclass, # plots etc. should be handled in some better way # than starting randomly from mapdisp and lmgr lmgr = self.parent.GetLayerManager() if lmgr: lmgr.OnShowAttributeTable(None, selection = 'table') dlg.Destroy() else: self.combo.SetValue(_('Select vector map')) if dlg: dlg.Destroy() return else: selection = event.GetSelection() - 1 # first option is 'New vector map' # skip currently selected map if self.layers[selection] == self.mapLayer: return if self.mapLayer: # deactive map layer for editing self.StopEditing() # select the given map layer for editing self.StartEditing(self.layers[selection]) event.Skip()
def main(): map = options['map'] layer = options['layer'] column = options['column'] mapset = grass.gisenv()['MAPSET'] if not grass.find_file(map, element = 'vector', mapset = mapset): grass.fatal(_("Vector map <%s> not found in current mapset") % map) f = grass.vector_layer_db(map, layer) table = f['table'] keycol = f['key'] database = f['database'] driver = f['driver'] if not table: grass.fatal(_("There is no table connected to the input vector map. Cannot rename any column")) cols = column.split(',') oldcol = cols[0] newcol = cols[1] if driver == "dbf": if len(newcol) > 10: grass.fatal(_("Column name <%s> too long. The DBF driver supports column names not longer than 10 characters") % newcol) if oldcol == keycol: grass.fatal(_("Cannot rename column <%s> as it is needed to keep table <%s> connected to the input vector map") % (oldcol, table)) # describe old col oldcoltype = None for f in grass.db_describe(table)['cols']: if f[0] != oldcol: continue oldcoltype = f[1] oldcollength = f[2] # old col there? if not oldcoltype: grass.fatal(_("Column <%s> not found in table <%s>") % (oldcol, table)) # some tricks if driver in ['sqlite', 'dbf']: if oldcoltype.upper() == "CHARACTER": colspec = "%s varchar(%s)" % (newcol, oldcollength) else: colspec = "%s %s" % (newcol, oldcoltype) grass.run_command('v.db.addcolumn', map = map, layer = layer, column = colspec) sql = "UPDATE %s SET %s=%s" % (table, newcol, oldcol) grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql) grass.run_command('v.db.dropcolumn', map = map, layer = layer, column = oldcol) else: sql = "ALTER TABLE %s RENAME %s TO %s" % (table, oldcol, newcol) grass.write_command('db.execute', input = '-', database = database, driver = driver, stdin = sql) # write cmd history: grass.vector_history(map)
def function(elem): #print elem mymapset = 'm'+str(elem) grass.run_command('g.mapset',mapset=mymapset,loc=MYLOC,flags='c') spn0= str(GRASSDBASE)+'/'+str(MYLOC)+'/'+str(mymapset)+'WIND' print elem+' '+spn0 checkit = os.path.isfile(spn0) while checkit == False: time.sleep(0.1) else: gge = grass.gisenv() spn= str(GRASSDBASE)+'/'+str(MYLOC)+'/'+str(mymapset)+'/SEARCH_PATH' wb = open(spn,'a') wb.write('PERMANENT') wb.write('\n') wb.write('user1') wb.write('\n') wb.write(str(mymapset)) wb.write('\n') wb.close() pa0 = 's'+str(elem) comm2 = 'cat = '+str(elem) grass.run_command('g.region',rast='elevation') grass.run_command('g.region',res=elem) varx = grass.read_command ('g.region',flags='g'). splitlines () wb = open('results.txt','a') var = str(elem)+' '+str(gge)+' '+str(varx) wb.write(var) wb.write('\n') wb.close() elem=None mymapset=None
def main(): name = options["input"] type_ = options["type"] shellstyle = flags['g'] system = flags['d'] history = flags['h'] # Make sure the temporal database exists tgis.init() dbif, connected = tgis.init_dbif(None) rows = tgis.get_tgis_metadata(dbif) if system and not shellstyle: # 0123456789012345678901234567890 print(" +------------------- Temporal DBMI backend information ----------------------+") print(" | DBMI Python interface:...... " + str(dbif.get_dbmi().__name__)) print(" | Temporal database string:... " + str( tgis.get_tgis_database_string())) print(" | SQL template path:.......... " + str( tgis.get_sql_template_path())) if rows: for row in rows: print(" | %s .......... %s"%(row[0], row[1])) print(" +----------------------------------------------------------------------------+") return elif system: print("dbmi_python_interface=\'" + str(dbif.get_dbmi().__name__) + "\'") print("dbmi_string=\'" + str(tgis.get_tgis_database_string()) + "\'") print("sql_template_path=\'" + str(tgis.get_sql_template_path()) + "\'") if rows: for row in rows: print("%s=\'%s\'"%(row[0], row[1])) return if not system and not name: grass.fatal(_("Please specify %s=") % ("name")) if name.find("@") >= 0: id_ = name else: id_ = name + "@" + grass.gisenv()["MAPSET"] dataset = tgis.dataset_factory(type_, id_) if dataset.is_in_db(dbif) == False: grass.fatal(_("Dataset <%s> not found in temporal database") % (id_)) dataset.select(dbif) if history == True and type in ["strds", "stvds", "str3ds"]: dataset.print_history() return if shellstyle == True: dataset.print_shell_info() else: dataset.print_info()
def fsegm(pa): ### mp.current_process().cnt += 1 current = multiprocessing.current_process() mn = current._identity[0] print 'running:', mn GISBASE = os.environ['GISBASE'] = "/home/majavie/hierba_hanks/grass-7.1.svn" GRASSDBASE = "/home/majavie/hanksgrass7" MYLOC = "global_MW" mapset = 'm' sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python")) import grass.script as grass import grass.script.setup as gsetup gsetup.init(GISBASE, GRASSDBASE, MYLOC, mapset) mapset2 = 'm'+str(mn)#ehabitat' os.system ('rm -rf /home/majavie/hanksgrass7/global_MW/'+mapset2) grass.run_command('g.mapset',mapset=mapset2,location='global_MW',gisdbase='/home/majavie/hanksgrass7',flags='c') gsetup.init(GISBASE, GRASSDBASE, MYLOC, mapset2) print mapset2, grass.gisenv() print pa grass.run_command('g.mapsets',mapset='ehabitat,rasterized_parks',operation='add') grass. message ("Deleting tmp layers") os.system ('rm -rf /home/majavie/hanksgrass7/global_MW/'+mapset2) gc.collect()
def test_raster3d_dataset(): # Create a test map grass.raster3d.mapcalc3d("test = sin(x()) + cos(y()) + sin(z())", overwrite = True) name = "test" mapset = grass.gisenv()["MAPSET"] print "Create a raster object" # We need to specify the name and the mapset as identifier r3ds = raster3d_dataset(name + "@" + mapset) # Load data from the raster map in the mapset r3ds.load() print "Is in db: ", r3ds.is_in_db() if r3ds.is_in_db(): # Remove the entry if it is in the db r3ds.delete() # Set the absolute valid time r3ds.set_absolute_time(start_time= datetime(year=2000, month=1, day=1), \ end_time= datetime(year=2010, month=1, day=1)) # Insert the map data into the SQL database r3ds.insert() # Print self info r3ds.print_self() # The temporal relation must be equal print r3ds.temporal_relation(r3ds)
def test_vector_dataset(): # Create a test map grass.run_command("v.random", output="test", n=20, column="height", zmin=0, \ zmax=100, flags="z", overwrite = True) name = "test" mapset = grass.gisenv()["MAPSET"] print "Create a vector object" # We need to specify the name and the mapset as identifier vds = vector_dataset(name + "@" + mapset) # Load data from the raster map in the mapset vds.load() print "Is in db: ", vds.is_in_db() if vds.is_in_db(): # Remove the entry if it is in the db vds.delete() # Set the absolute valid time vds.set_absolute_time(start_time= datetime(year=2000, month=1, day=1), \ end_time= datetime(year=2010, month=1, day=1)) # Insert the map data into the SQL database vds.insert() # Print self info vds.print_self() # The temporal relation must be equal print vds.temporal_relation(vds)
def main(): # Get the options input = options["input"] output = options["output"] type = options["type"] # Make sure the temporal database exists tgis.init() #Get the current mapset to create the id of the space time dataset mapset = grass.gisenv()["MAPSET"] if input.find("@") >= 0: old_id = input else: old_id = input + "@" + mapset if output.find("@") >= 0: new_id = output else: new_id = output + "@" + mapset # Do not overwrite yourself if new_id == old_id: return dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() stds = tgis.dataset_factory(type, old_id) if new_id.split("@")[1] != mapset: grass.fatal(_("Space time %s dataset <%s> can not be renamed. " "Mapset of the new identifier differs from the current " "mapset.") % (stds.get_new_map_instance(None).get_type(), old_id)) if stds.is_in_db(dbif=dbif) == False: dbif.close() grass.fatal(_("Space time %s dataset <%s> not found") % ( stds.get_new_map_instance(None).get_type(), old_id)) # Check if the new id is in the database new_stds = tgis.dataset_factory(type, new_id) if new_stds.is_in_db(dbif=dbif) == True and grass.overwrite() == False: dbif.close() grass.fatal(_("Unable to rename Space time %s dataset <%s>. Name <%s> " "is in use, please use the overwrite flag.") % ( stds.get_new_map_instance(None).get_type(), old_id, new_id)) # Remove an already existing space time dataset if new_stds.is_in_db(dbif=dbif) == True: new_stds.delete(dbif=dbif) stds.select(dbif=dbif) stds.rename(ident=new_id, dbif=dbif) stds.update_command_string(dbif=dbif)
def UpdateHistoryFile(self, command): """Update history file :param command: the command given as a string """ env = grass.gisenv() try: filePath = os.path.join(env['GISDBASE'], env['LOCATION_NAME'], env['MAPSET'], '.bash_history') fileHistory = codecs.open(filePath, encoding='utf-8', mode='a') except IOError as e: GError(_("Unable to write file '%(filePath)s'.\n\nDetails: %(error)s") % {'filePath': filePath, 'error': e}, parent=self._guiparent) return try: fileHistory.write(command + os.linesep) finally: fileHistory.close() # update wxGUI prompt if self._giface: self._giface.UpdateCmdHistory(command)
def main(): first = options['first'] second = options['second'] output = options['output_prefix'] percent = options['percent'] mapset = grass.gisenv()['MAPSET'] if not grass.overwrite(): for ch in ['r','g','b']: map = '%s.%s' % (output, ch) if grass.find_file(map, element = 'cell', mapset = mapset)['file']: grass.fatal(_("Raster map <%s> already exists.") % map) percent = float(percent) perc_inv = 100.0 - percent frac1 = percent / 100.0 frac2 = perc_inv / 100.0 grass.message(_("Calculating the three component maps...")) template = string.Template("$$output.$ch = if(isnull($$first), $ch#$$second, if(isnull($$second), $ch#$$first, $$frac1 * $ch#$$first + $$frac2 * $ch#$$second))") cmd = [template.substitute(ch = ch) for ch in ['r','g','b']] cmd = ';'.join(cmd) grass.mapcalc(cmd, output = output, first = first, second = second, frac1 = frac1, frac2 = frac2) for ch in ['r','g','b']: map = "%s.%s" % (output, ch) grass.run_command('r.colors', map = map, color = 'grey255') grass.run_command('r.support', map = map, history="", title = "Color blend of %s and %s" % (first, second), description = "generated by r.blend") grass.run_command('r.support', map = map, history = "r.blend %s channel." % ch) grass.run_command('r.support', map = map, history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second)) grass.run_command('r.support', map = map, history = "") grass.run_command('r.support', map = map, history = os.environ['CMDLINE']) if flags['c']: grass.run_command('r.composite', r = '%s.r' % output, g = '%s.g' % output, b = '%s.b' % output, output = output) grass.run_command('r.support', map = output, history="", title = "Color blend of %s and %s" % (first, second), description = "generated by r.blend") grass.run_command('r.support', map = output, history = " %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second)) grass.run_command('r.support', map = output, history = "") grass.run_command('r.support', map = output, history = os.environ['CMDLINE']) else: grass.message(_("Done. Use the following command to visualize the result:")) grass.message(_("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
def ExpandCurrentLocation(self): """Expand current location""" location = gscript.gisenv()['LOCATION_NAME'] item = self._model.SearchNodes(name=location, type='location') if item: self.Select(item[0], select=True) self.ExpandNode(item[0], recursive=False) else: Debug.msg(1, "Location <%s> not found" % location)
def _initVariables(self): """Init variables.""" self.selected_layer = None self.selected_type = None self.selected_mapset = None self.selected_location = None self.gisdbase = grass.gisenv()['GISDBASE'] self.ctrldown = False
def InitTreeItems(self): """Add locations, mapsets and layers to the tree.""" gisenv = grass.gisenv() location = gisenv['LOCATION_NAME'] mapset = gisenv['MAPSET'] self._initTreeItems(locations=[location], mapsets=[mapset]) self.ExpandAll()
def OnBeginDrag(self, node, event): """Just copy necessary data""" self.DefineItems(node) if self.selected_layer and not (self._restricted and gisenv()["LOCATION_NAME"] != self.selected_location.label): event.Allow() self.OnCopyMap(event) Debug.msg(1, "DRAG") else: event.Veto()
def main(): if flags['l']: # list of available layers list_layers() return 0 elif not options['output']: grass.fatal(_("No output map specified")) if options['cap_file'] and not flags['l']: grass.warning(_("Option <cap_file> ignored. It requires '-l' flag.")) # set directory for download if not options['folder']: options['folder'] = os.path.join(grass.gisenv()['GISDBASE'], 'wms_download') # region settings if options['region']: if not grass.find_file(name = options['region'], element = 'windows')['name']: grass.fatal(_("Region <%s> not found") % options['region']) request = wms_request.WMSRequest(flags, options) if not flags['d']: # request data first request.GetTiles() if not request: grass.fatal(_("WMS request failed")) if flags['a']: # use GDAL WMS driver ### TODO: use GDAL Python bindings instead if not wms_gdal.checkGdalWms(): grass.fatal(_("GDAL WMS driver is not available")) # create local service description XML file gdalWms = wms_gdal.GdalWms(options, request) options['input'] = gdalWms.GetFile() else: # download data download = wms_download.WMSDownload(flags, options) download.GetTiles(request.GetRequests()) # list of files files = [] for item in request.GetRequests(): files.append(item['output']) files = ','.join(files) options['input'] = files # add flags for r.in.gdalwarp flags['e'] = False flags['c'] = True options['warpoptions'] = '' return gdalwarp.GDALWarp(flags, options).run() return 0
def cleanup(): # remove temp location if TMPLOC: grass.try_rmdir(os.path.join(GISDBASE, TMPLOC)) if SRCGISRC: grass.try_remove(SRCGISRC) if TMP_REG_NAME and grass.find_file(name=TMP_REG_NAME, element='vector', mapset=grass.gisenv()['MAPSET'])['fullname']: grass.run_command('g.remove', type='vector', name=TMP_REG_NAME, flags='f', quiet=True)
def ExpandCurrentLocation(self): """Expand current location""" location = grass.gisenv()['LOCATION_NAME'] item = self.getItemByName(location, self.root) if item is not None: self.SelectItem(item) self.ExpandAllChildren(item) self.EnsureVisible(item) else: Debug.msg(1, "Location <%s> not found" % location)
def SetLevel(self): """Initialize gui debug level """ try: self.debuglevel = int(grass.gisenv().get('WX_DEBUG', 0)) if self.debuglevel < 0 or self.debuglevel > 5: raise ValueError(_("Wx debug level {}.").format(self.debuglevel)) except ValueError as e: self.debuglevel = 0 sys.stderr.write(_("WARNING: Ignoring unsupported wx debug level (must be >=0 and <=5). {}\n").format(e))
def _initVariables(self): """Init variables.""" self.selected_layer = None self.selected_type = None self.selected_mapset = None self.selected_location = None gisenv = gscript.gisenv() self.gisdbase = gisenv['GISDBASE'] self.glocation = gisenv['LOCATION_NAME'] self.gmapset = gisenv['MAPSET']
def main(): # Get the options name = options["dataset"] type = options["type"] shellstyle = flags['g'] tmatrix = flags['t'] # Make sure the temporal database exists tgis.create_temporal_database() #Get the current mapset to create the id of the space time dataset if name.find("@") >= 0: id = name else: mapset = grass.gisenv()["MAPSET"] id = name + "@" + mapset if type == "strds": sp = tgis.space_time_raster_dataset(id) if type == "str3ds": sp = tgis.space_time_raster3d_dataset(id) if type == "stvds": sp = tgis.space_time_vector_dataset(id) if type == "raster": sp = tgis.raster_dataset(id) tmatrix = False if type == "raster3d": sp = tgis.raster3d_dataset(id) tmatrix = False if type == "vector": sp = tgis.vector_dataset(id) tmatrix = False if sp.is_in_db() == False: grass.fatal("Dataset <" + name + "> not found in temporal database") # Insert content from db sp.select() if tmatrix: matrix = sp.get_temporal_relation_matrix() for row in matrix: for col in row: print col, print " " print " " if shellstyle == True: sp.print_shell_info() else: sp.print_info()
def main(): tavg = options['tavg'] tmin = options['tmin'] tmax = options['tmax'] prec = options['precipitation'] outpre = options['output'] tinscale = options['tinscale'] toutscale = options['toutscale'] workers = int(options['workers']) quartals = int(options['quartals']) qstep = 12 / quartals mapset = grass.gisenv()['MAPSET'] # count input maps if len(tmin.split(',')) != 12: grass.fatal(_("12 maps with minimum temperatures are required")) if len(tmax.split(',')) != 12: grass.fatal(_("12 maps with maximum temperatures are required")) if tavg and len(tavg.split(',')) != 12: grass.fatal(_("12 maps with average temperatures are required")) if prec: if len(prec.split(',')) != 12: grass.fatal(_("12 maps with precipitation are required")) tinscale = int(tinscale) if tinscale <= 0: grass.fatal(_("Input temperature scale must be positive")) toutscale = int(toutscale) if toutscale <= 0: grass.fatal(_("Output temperature scale must be positive")) pid = os.getpid() # all temporary raster maps must follow this naming pattern tmp_pattern = "%s.*.%d" % (outpre, pid) tminl = tmin.split(',') tmaxl = tmax.split(',') ps = {} if not tavg: # calculate monthly averages from min and max grass.message(_("Calculating monthly averages from min and max")) e = "$ta = ($tmax + $tmin) / 2.0" if workers > 1: for i in range(0, 12, workers): jend = 12 - i if jend > workers: jend = workers for j in range(jend): ta = "%s.tavg%02d.%d" % (outpre, (i + j + 1), pid) ps[j] = grass.mapcalc_start(e, ta=ta, tmax=tmaxl[i + j], tmin=tminl[i + j]) for j in range(jend): ps[j].wait() else: for i in range(12): ta = "%s.tavg%02d.%d" % (outpre, (i + 1), pid) grass.mapcalc(e, ta=ta, tmax=tmaxl[i], tmin=tminl[i]) tavg = grass.read_command("g.list", quiet=True, type='raster', pattern='%s.tavg??.%d' % (outpre, pid), separator=',', mapset='.') tavg = tavg.strip('\n') tavgl = tavg.split(',') # BIO1 = Annual Mean Temperature grass.message(_("BIO1 = Annual Mean Temperature ...")) output = outpre + 'bio01.' + str(pid) grass.run_command('r.series', input=tavg, output=output, method='average') grass.mapcalc("$bio = round(double($oscale) * $input / $iscale)", bio=outpre + 'bio01', oscale=toutscale, input=output, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio01', description='BIOCLIM01: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio01', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', name=output, quiet=True) # BIO2 = Mean Diurnal Range (Mean of monthly (max temp - min temp)) grass.message(_("BIO2 = Mean Diurnal Range ...")) e = "$tr = $tmax - $tmin" if workers > 1: for i in range(0, 12, workers): jend = 12 - i if jend > workers: jend = workers for j in range(jend): tr = "%s.tr%02d.%d" % (outpre, (i + j + 1), pid) ps[j] = grass.mapcalc_start(e, tr=tr, tmax=tmaxl[i + j], tmin=tminl[i + j]) for j in range(jend): ps[j].wait() else: for i in range(12): tr = "%s.tr%02d.%d" % (outpre, (i + 1), pid) grass.mapcalc(e, tr=tr, tmax=tmaxl[i], tmin=tminl[i]) tr = grass.read_command("g.list", quiet=True, type='raster', pattern='%s.tr??.%d' % (outpre, pid), separator=',', mapset='.') tr = tr.strip('\n') output = outpre + 'bio02.' + str(pid) grass.run_command('r.series', input=tr, output=output, method='average') grass.mapcalc("$bio = round(double($oscale) * $input / $iscale)", bio=outpre + 'bio02', oscale=toutscale, input=output, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio02', description='BIOCLIM02: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio02', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', name=output, quiet=True) grass.run_command('g.remove', flags='f', type='raster', pattern='%s.tr??.%d' % (outpre, pid), quiet=True) # BIO4 = Temperature Seasonality (standard deviation * 100) grass.message(_("BIO4 = Temperature Seasonality ...")) output = outpre + 'bio04.' + str(pid) grass.run_command('r.series', input=tavg, output=output, method='stddev') grass.mapcalc("$bio = round(100.0 * $biotmp / $iscale * $oscale)", bio=outpre + 'bio04', biotmp=output, iscale=tinscale, oscale=toutscale) grass.run_command('r.support', map=outpre + 'bio04', description='BIOCLIM04: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio04', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', name=output, quiet=True) # BIO5 = Max Temperature of Warmest Month grass.message(_("BIO5 = Max Temperature of Warmest Month ...")) output = outpre + 'bio05.' + str(pid) grass.run_command('r.series', input=tmax, output=output, method='maximum') grass.mapcalc("$bio = round(double($oscale) * $biotmp / $iscale)", bio=outpre + 'bio05', oscale=toutscale, iscale=tinscale, biotmp=output) grass.run_command('r.support', map=outpre + 'bio05', description='BIOCLIM05: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio05', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', name=output, quiet=True) # BIO6 = Min Temperature of Coldest Month grass.message(_("BIO6 = Min Temperature of Coldest Month ...")) output = outpre + 'bio06.' + str(pid) grass.run_command('r.series', input=tmin, output=output, method='minimum') grass.mapcalc("$bio = round(double($oscale) * $biotmp / $iscale)", bio=outpre + 'bio06', oscale=toutscale, biotmp=output, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio06', description='BIOCLIM06: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio06', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', name=output, quiet=True) # BIO7 = Temperature Annual Range (BIO5-BIO6) grass.message(_("BIO7 = Temperature Annual Range ...")) grass.mapcalc("$bio = $bio5 - $bio6", bio=outpre + 'bio07', bio5=outpre + 'bio05', bio6=outpre + 'bio06') grass.run_command('r.support', map=outpre + 'bio07', description='BIOCLIM07: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio07', history=os.environ['CMDLINE']) # BIO3 = Isothermality (BIO2/BIO7) (* 100) grass.message(_("BIO3 = Isothermality (BIO2/BIO7) ...")) grass.mapcalc("$bio = round(100.0 * $bio2 / $bio7)", bio=outpre + 'bio03', bio2=outpre + 'bio02', bio7=outpre + 'bio07') grass.run_command('r.support', map=outpre + 'bio03', description='BIOCLIM03: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio03', history=os.environ['CMDLINE']) # mean of mean for each quarter year grass.message(_("Mean temperature for each quarter year ...")) for i in range(quartals): tavgq = "%s.tavgq.%02d.%d" % (outpre, i, pid) m1 = int(i * qstep) m2 = m1 + 1 if m2 > 11: m2 = m2 - 12 m3 = m1 + 2 if m3 > 11: m3 = m3 - 12 grass.run_command('r.series', input="%s,%s,%s" % (tavgl[m1], tavgl[m2], tavgl[m3]), output=tavgq, method='average') # BIO10 = Mean Temperature of Warmest Quarter # BIO11 = Mean Temperature of Coldest Quarter grass.message(_("BIO10 = Mean Temperature of Warmest Quarter,")) grass.message(_("BIO11 = Mean Temperature of Coldest Quarter ...")) tavgq = grass.read_command("g.list", quiet=True, type='raster', pattern='%s.tavgq.??.%d' % (outpre, pid), separator=',', mapset='.') tavgq = tavgq.strip("\n") bio10 = outpre + 'bio10.' + str(pid) bio11 = outpre + 'bio11.' + str(pid) grass.run_command('r.series', input=tavgq, output="%s,%s" % (bio10, bio11), method='maximum,minimum') grass.mapcalc("$bio = round(double($oscale) * $biotmp / $iscale)", bio=outpre + 'bio10', oscale=toutscale, biotmp=bio10, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio10', description='BIOCLIM10: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio10', history=os.environ['CMDLINE']) grass.mapcalc("$bio = round(double($oscale) * $biotmp / $iscale)", bio=outpre + 'bio11', oscale=toutscale, biotmp=bio11, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio11', description='BIOCLIM11: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio11', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', name="%s,%s" % (bio10, bio11), quiet=True) if not prec: grass.run_command('g.remove', flags='f', type='raster', pattern=tmp_pattern, quiet=True) sys.exit(1) precl = prec.split(',') # sum for each quarter year grass.message(_("Precipitation for each quarter year ...")) for i in range(quartals): precq = "%s.precq.%02d.%d" % (outpre, i + 1, pid) m1 = int(i * qstep) m2 = m1 + 1 if m2 > 11: m2 = m2 - 12 m3 = m1 + 2 if m3 > 11: m3 = m3 - 12 grass.run_command('r.series', input="%s,%s,%s" % (precl[m1], precl[m2], precl[m3]), output=precq, method='sum') precq = grass.read_command("g.list", quiet=True, type='raster', pattern='%s.precq.??.%d' % (outpre, pid), separator=',', mapset='.') precq = precq.strip("\n") # warmest and coldest quarter warmestq = "%s.warmestq.%d" % (outpre, pid) coldestq = "%s.coldestq.%d" % (outpre, pid) grass.run_command('r.series', input=tavgq, output="%s,%s" % (warmestq, coldestq), method='max_raster,min_raster') tavgql = tavgq.split(',') # wettest and driest quarter wettestq = "%s.wettestq.%d" % (outpre, pid) driestq = "%s.driestq.%d" % (outpre, pid) grass.run_command('r.series', input=precq, output="%s,%s" % (wettestq, driestq), method='max_raster,min_raster') # BIO8 = Mean Temperature of Wettest Quarter grass.message(_("BIO8 = Mean Temperature of Wettest Quarter ...")) if quartals == 4: grass.mapcalc("$bio = round(if($wettestq == 0, $tavgq0, \ if($wettestq == 1, $tavgq1, \ if($wettestq == 2, $tavgq2, \ if($wettestq == 3, $tavgq3, null())))) \ * $oscale / $iscale)", bio=outpre + 'bio08', wettestq=wettestq, tavgq0=tavgql[0], tavgq1=tavgql[1], tavgq2=tavgql[2], tavgq3=tavgql[3], oscale=toutscale, iscale=tinscale) else: # quartals == 12 grass.mapcalc("$bio = round(if($wettestq == 0, $tavgq0, \ if($wettestq == 1, $tavgq1, \ if($wettestq == 2, $tavgq2, \ if($wettestq == 3, $tavgq3, \ if($wettestq == 4, $tavgq4, \ if($wettestq == 5, $tavgq5, \ if($wettestq == 6, $tavgq6, \ if($wettestq == 7, $tavgq7, \ if($wettestq == 8, $tavgq8, \ if($wettestq == 9, $tavgq9, \ if($wettestq == 10, $tavgq10, \ if($wettestq == 11, $tavgq11, null())))))))))))) \ * $oscale / $iscale)", bio=outpre + 'bio08', wettestq=wettestq, tavgq0=tavgql[0], tavgq1=tavgql[1], tavgq2=tavgql[2], tavgq3=tavgql[3], tavgq4=tavgql[4], tavgq5=tavgql[5], tavgq6=tavgql[6], tavgq7=tavgql[7], tavgq8=tavgql[8], tavgq9=tavgql[9], tavgq10=tavgql[10], tavgq11=tavgql[11], oscale=toutscale, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio08', description='BIOCLIM08: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio08', history=os.environ['CMDLINE']) # BIO9 = Mean Temperature of Driest Quarter grass.message(_("BIO9 = Mean Temperature of Driest Quarter ...")) if quartals == 4: grass.mapcalc("$bio = round(if($driestq == 0, $tavgq0, \ if($driestq == 1, $tavgq1, \ if($driestq == 2, $tavgq2, \ if($driestq == 3, $tavgq3, null())))) \ * $oscale / $iscale)", bio=outpre + 'bio09', driestq=driestq, tavgq0=tavgql[0], tavgq1=tavgql[1], tavgq2=tavgql[2], tavgq3=tavgql[3], oscale=toutscale, iscale=tinscale) else: # quartals == 12 grass.mapcalc("$bio = round(if($driestq == 0, $tavgq0, \ if($driestq == 1, $tavgq1, \ if($driestq == 2, $tavgq2, \ if($driestq == 3, $tavgq3, \ if($driestq == 4, $tavgq4, \ if($driestq == 5, $tavgq5, \ if($driestq == 6, $tavgq6, \ if($driestq == 7, $tavgq7, \ if($driestq == 8, $tavgq8, \ if($driestq == 9, $tavgq9, \ if($driestq == 10, $tavgq10, \ if($driestq == 11, $tavgq11, null())))))))))))) \ * $oscale / $iscale)", bio=outpre + 'bio09', driestq=driestq, tavgq0=tavgql[0], tavgq1=tavgql[1], tavgq2=tavgql[2], tavgq3=tavgql[3], tavgq4=tavgql[4], tavgq5=tavgql[5], tavgq6=tavgql[6], tavgq7=tavgql[7], tavgq8=tavgql[8], tavgq9=tavgql[9], tavgq10=tavgql[10], tavgq11=tavgql[11], oscale=toutscale, iscale=tinscale) grass.run_command('r.support', map=outpre + 'bio09', description='BIOCLIM09: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio09', history=os.environ['CMDLINE']) # BIO12 = Annual Precipitation grass.message(_("BIO12 = Annual Precipitation ...")) output = outpre + 'bio12' grass.run_command('r.series', input=prec, output=output, method='sum') grass.run_command('r.support', map=outpre + 'bio12', description='BIOCLIM12: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio12', history=os.environ['CMDLINE']) # BIO13 = Precipitation of Wettest Month # BIO14 = Precipitation of Driest Month grass.message(_("BIO13 = Precipitation of Wettest Month,")) grass.message(_("BIO14 = Precipitation of Driest Month ...")) bio13 = outpre + 'bio13' bio14 = outpre + 'bio14' grass.run_command('r.series', input=prec, output="%s,%s" % (bio13, bio14), method='maximum,minimum') grass.run_command('r.support', map=outpre + 'bio13', description='BIOCLIM13: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio13', history=os.environ['CMDLINE']) grass.run_command('r.support', map=outpre + 'bio14', description='BIOCLIM14: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio14', history=os.environ['CMDLINE']) # BIO15 = Precipitation Seasonality (Coefficient of Variation) grass.message(_("BIO15 = Precipitation Seasonality ...")) precavg = "%s.precavg.%d" % (outpre, pid) precstddev = "%s.precstddev.%d" % (outpre, pid) grass.run_command('r.series', input=prec, output="%s,%s" % (precavg, precstddev), method='average,stddev') grass.mapcalc("$bio = if($precavg == 0, 0, round(100.0 * $precstddev / $precavg))", bio=outpre + 'bio15', precstddev=precstddev, precavg=precavg) grass.run_command('r.support', map=outpre + 'bio15', description='BIOCLIM15: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio15', history=os.environ['CMDLINE']) # BIO16 = Precipitation of Wettest Quarter # BIO17 = Precipitation of Driest Quarter grass.message(_("BIO16 = Precipitation of Wettest Quarter,")) grass.message(_("BIO17 = Precipitation of Driest Quarter ...")) bio16 = outpre + 'bio16' bio17 = outpre + 'bio17' grass.run_command('r.series', input=precq, output="%s,%s" % (bio16, bio17), method='maximum,minimum') grass.run_command('r.support', map=outpre + 'bio16', description='BIOCLIM16: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio16', history=os.environ['CMDLINE']) grass.run_command('r.support', map=outpre + 'bio17', description='BIOCLIM17: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio17', history=os.environ['CMDLINE']) precql = precq.split(',') # BIO18 = Precipitation of Warmest Quarter grass.message(_("BIO18 = Precipitation of Warmest Quarter ...")) if quartals == 4: grass.mapcalc("$bio = round(if($warmestq == 0, $precq0, \ if($warmestq == 1, $precq1, \ if($warmestq == 2, $precq2, \ if($warmestq == 3, $precq3, null())))))", bio=outpre + 'bio18', warmestq=warmestq, precq0=precql[0], precq1=precql[1], precq2=precql[2], precq3=precql[3]) else: # quartals == 12 grass.mapcalc("$bio = round(if($warmestq == 0, $precq0, \ if($warmestq == 1, $precq1, \ if($warmestq == 2, $precq2, \ if($warmestq == 3, $precq3, \ if($warmestq == 4, $precq4, \ if($warmestq == 5, $precq5, \ if($warmestq == 6, $precq6, \ if($warmestq == 7, $precq7, \ if($warmestq == 8, $precq8, \ if($warmestq == 9, $precq9, \ if($warmestq == 10, $precq10, \ if($warmestq == 11, $precq11, null())))))))))))))", bio=outpre + 'bio18', warmestq=warmestq, precq0=precql[0], precq1=precql[1], precq2=precql[2], precq3=precql[3], precq4=precql[4], precq5=precql[5], precq6=precql[6], precq7=precql[7], precq8=precql[8], precq9=precql[9], precq10=precql[10], precq11=precql[11]) grass.run_command('r.support', map=outpre + 'bio18', description='BIOCLIM18: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio18', history=os.environ['CMDLINE']) # BIO19 = Precipitation of Coldest Quarter grass.message(_("BIO19 = Precipitation of Coldest Quarter ...")) if quartals == 4: grass.mapcalc("$bio = round(if($coldestq == 0, $precq0, \ if($coldestq == 1, $precq1, \ if($coldestq == 2, $precq2, \ if($coldestq == 3, $precq3, null())))))", bio=outpre + 'bio19', coldestq=coldestq, precq0=precql[0], precq1=precql[1], precq2=precql[2], precq3=precql[3]) else: # quartals == 12 grass.mapcalc("$bio = round(if($coldestq == 0, $precq0, \ if($coldestq == 1, $precq1, \ if($coldestq == 2, $precq2, \ if($coldestq == 3, $precq3, \ if($coldestq == 4, $precq4, \ if($coldestq == 5, $precq5, \ if($coldestq == 6, $precq6, \ if($coldestq == 7, $precq7, \ if($coldestq == 8, $precq8, \ if($coldestq == 9, $precq9, \ if($coldestq == 10, $precq10, \ if($coldestq == 11, $precq11, null())))))))))))))", bio=outpre + 'bio19', coldestq=coldestq, precq0=precql[0], precq1=precql[1], precq2=precql[2], precq3=precql[3], precq4=precql[4], precq5=precql[5], precq6=precql[6], precq7=precql[7], precq8=precql[8], precq9=precql[9], precq10=precql[10], precq11=precql[11]) grass.run_command('r.support', map=outpre + 'bio19', description='BIOCLIM19: Generated by r.bioclim') grass.run_command('r.support', map=outpre + 'bio19', history=os.environ['CMDLINE']) grass.run_command('g.remove', flags='f', type='raster', pattern=tmp_pattern, quiet=True)
def main(): # Get the options input = options["input"] base = options["basename"] where = options["where"] nprocs = options["nprocs"] mapset = grass.gisenv()["MAPSET"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(input, "strds") maps = sp.get_registered_maps_as_objects_with_gaps(where, dbif) num = len(maps) # Configure the r.to.vect module gapfill_module = pymod.Module( "r.series.interp", overwrite=grass.overwrite(), quiet=True, run_=False, finish_=False, ) process_queue = pymod.ParallelModuleQueue(int(nprocs)) gap_list = [] overwrite_flags = {} # Identify all gaps and create new names count = 0 for _map in maps: if _map.get_id() is None: count += 1 _id = "%s_%d@%s" % (base, num + count, mapset) _map.set_id(_id) gap_list.append(_map) if len(gap_list) == 0: grass.message(_("No gaps found")) return # Build the temporal topology tb = tgis.SpatioTemporalTopologyBuilder() tb.build(maps) # Do some checks before computation for _map in gap_list: if not _map.get_precedes() or not _map.get_follows(): grass.fatal( _("Unable to determine successor " "and predecessor of a gap.")) if len(_map.get_precedes()) > 1: grass.warning( _("More than one successor of the gap found. " "Using the first found.")) if len(_map.get_follows()) > 1: grass.warning( _("More than one predecessor of the gap found. " "Using the first found.")) # Interpolate the maps using parallel processing result_list = [] for _map in gap_list: predecessor = _map.get_follows()[0] successor = _map.get_precedes()[0] gran = sp.get_granularity() tmpval, start = predecessor.get_temporal_extent_as_tuple() end, tmpval = successor.get_temporal_extent_as_tuple() # Now resample the gap map_matrix = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity( (_map, ), start, end, gran) map_names = [] map_positions = [] increment = 1.0 / (len(map_matrix) + 1.0) position = increment count = 0 for intp_list in map_matrix: new_map = intp_list[0] count += 1 new_id = "%s_%i@%s" % (_map.get_name(), count, tgis.get_current_mapset()) new_map.set_id(new_id) overwrite_flags[new_id] = False if new_map.map_exists() or new_map.is_in_db(dbif): if not grass.overwrite: grass.fatal( _("Map with name <%s> already exists. " "Please use another base name." % (_id))) else: if new_map.is_in_db(dbif): overwrite_flags[new_id] = True map_names.append(new_map.get_name()) map_positions.append(position) position += increment result_list.append(new_map) mod = copy.deepcopy(gapfill_module) mod(input=(predecessor.get_map_id(), successor.get_map_id()), datapos=(0, 1), output=map_names, samplingpos=map_positions) sys.stderr.write(mod.get_bash() + "\n") process_queue.put(mod) # Wait for unfinished processes process_queue.wait() # Insert new interpolated maps in temporal database and dataset for _map in result_list: id = _map.get_id() if overwrite_flags[id] == True: if _map.is_time_absolute(): start, end = _map.get_absolute_time() if _map.is_in_db(): _map.delete(dbif) _map = sp.get_new_map_instance(id) _map.set_absolute_time(start, end) else: start, end, unit = _map.get_relative_time() if _map.is_in_db(): _map.delete(dbif) _map = sp.get_new_map_instance(id) _map.set_relative_time(start, end, unit) _map.load() _map.insert(dbif) sp.register_map(_map, dbif) sp.update_from_registered_maps(dbif) sp.update_command_string(dbif=dbif) dbif.close()
def main(): map = options["map"] layer = options["layer"] column = options["column"] mapset = grass.gisenv()["MAPSET"] if not grass.find_file(map, element="vector", mapset=mapset): grass.fatal(_("Vector map <%s> not found in current mapset") % map) f = grass.vector_layer_db(map, layer) table = f["table"] keycol = f["key"] database = f["database"] driver = f["driver"] if not table: grass.fatal( _("There is no table connected to the input vector map. Cannot rename any column" )) cols = column.split(",") oldcol = cols[0] newcol = cols[1] if driver == "dbf": if len(newcol) > 10: grass.fatal( _("Column name <%s> too long. The DBF driver supports column names not longer than 10 characters" ) % newcol) if oldcol == keycol: grass.fatal( _("Cannot rename column <%s> as it is needed to keep table <%s> connected to the input vector map" ) % (oldcol, table)) # describe old col oldcoltype = None for f in grass.db_describe(table)["cols"]: if f[0] != oldcol: continue oldcoltype = f[1] oldcollength = f[2] # old col there? if not oldcoltype: grass.fatal(_("Column <%s> not found in table <%s>") % (oldcol, table)) # some tricks if driver in ["sqlite", "dbf"]: if oldcoltype.upper() == "CHARACTER": colspec = "%s varchar(%s)" % (newcol, oldcollength) else: colspec = "%s %s" % (newcol, oldcoltype) grass.run_command("v.db.addcolumn", map=map, layer=layer, column=colspec) sql = "UPDATE %s SET %s=%s" % (table, newcol, oldcol) grass.write_command("db.execute", input="-", database=database, driver=driver, stdin=sql) grass.run_command("v.db.dropcolumn", map=map, layer=layer, column=oldcol) elif driver == "mysql": if oldcoltype.upper() == "CHARACTER": newcoltype = "varchar(%s)" % (oldcollength) else: newcoltype = oldcoltype sql = "ALTER TABLE %s CHANGE %s %s %s" % (table, oldcol, newcol, newcoltype) grass.write_command("db.execute", input="-", database=database, driver=driver, stdin=sql) else: sql = "ALTER TABLE %s RENAME %s TO %s" % (table, oldcol, newcol) grass.write_command("db.execute", input="-", database=database, driver=driver, stdin=sql) # write cmd history: grass.vector_history(map)
def main(): # lazy imports import grass.temporal as tgis name = options["input"] type_ = options["type"] shellstyle = flags['g'] system = flags['d'] history = flags['h'] # Make sure the temporal database exists tgis.init() dbif, connection_state_changed = tgis.init_dbif(None) rows = tgis.get_tgis_metadata(dbif) if system and not shellstyle and not history: # 0123456789012345678901234567890 print( " +------------------- Temporal DBMI backend information ----------------------+" ) print(" | DBMI Python interface:...... " + str(dbif.get_dbmi().__name__)) print(" | Temporal database string:... " + str(tgis.get_tgis_database_string())) print(" | SQL template path:.......... " + str(tgis.get_sql_template_path())) if rows: for row in rows: print(" | %s .......... %s" % (row[0], row[1])) print( " +----------------------------------------------------------------------------+" ) return elif system and not history: print("dbmi_python_interface=\'" + str(dbif.get_dbmi().__name__) + "\'") print("dbmi_string=\'" + str(tgis.get_tgis_database_string()) + "\'") print("sql_template_path=\'" + str(tgis.get_sql_template_path()) + "\'") if rows: for row in rows: print("%s=\'%s\'" % (row[0], row[1])) return if not system and not name: grass.fatal(_("Please specify %s=") % ("name")) if name.find("@") >= 0: id_ = name else: id_ = name + "@" + grass.gisenv()["MAPSET"] dataset = tgis.dataset_factory(type_, id_) if not dataset.is_in_db(dbif): grass.fatal(_("Dataset <%s> not found in temporal database") % (id_)) dataset.select(dbif) if history and type_ in ["strds", "stvds", "str3ds"]: dataset.print_history() return if shellstyle: dataset.print_shell_info() else: dataset.print_info()
def main(): # lazy imports import grass.temporal as tgis # Get the options input = options["input"] output = options["output"] # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()["MAPSET"] sp = tgis.open_old_stds(input, "strds") grass.use_temp_region() maps = sp.get_registered_maps_as_objects_by_granularity() num_maps = len(maps) # get datatype of the first map if maps: maps[0][0].select() datatype = maps[0][0].metadata.get_datatype() else: datatype = None # Get the granularity and set bottom, top and top-bottom resolution granularity = sp.get_granularity() # This is the reference time to scale the z coordinate reftime = datetime(1900, 1, 1) # We set top and bottom according to the start time in relation # to the date 1900-01-01 00:00:00 # In case of days, hours, minutes and seconds, a double number # is used to represent days and fracs of a day # Space time voxel cubes with montly or yearly granularity can not be # mixed with other temporal units # Compatible temporal units are : days, hours, minutes and seconds # Incompatible are years and moths start, end = sp.get_temporal_extent_as_tuple() if sp.is_time_absolute(): unit = granularity.split(" ")[1] granularity = float(granularity.split(" ")[0]) print("Gran from stds %0.15f" % (granularity)) if unit == "years" or unit == "year": bottom = float(start.year - 1900) top = float(granularity * num_maps) elif unit == "months" or unit == "month": bottom = float((start.year - 1900) * 12 + start.month) top = float(granularity * num_maps) else: bottom = float(tgis.time_delta_to_relative_time(start - reftime)) days = 0.0 hours = 0.0 minutes = 0.0 seconds = 0.0 if unit == "days" or unit == "day": days = float(granularity) if unit == "hours" or unit == "hour": hours = float(granularity) if unit == "minutes" or unit == "minute": minutes = float(granularity) if unit == "seconds" or unit == "second": seconds = float(granularity) granularity = float(days + hours / 24.0 + minutes / 1440.0 + seconds / 86400.0) else: unit = sp.get_relative_time_unit() bottom = start top = float(bottom + granularity * float(num_maps)) try: grass.run_command("g.region", t=top, b=bottom, tbres=granularity) except CalledModuleError: grass.fatal(_("Unable to set 3D region")) # Create a NULL map to fill the gaps null_map = "temporary_null_map_%i" % os.getpid() if datatype == "DCELL": grass.mapcalc("%s = double(null())" % (null_map)) elif datatype == "FCELL": grass.mapcalc("%s = float(null())" % (null_map)) else: grass.mapcalc("%s = null()" % (null_map)) if maps: count = 0 map_names = "" for map in maps: # Use the first map id = map[0].get_id() # None ids will be replaced by NULL maps if id is None: id = null_map if count == 0: map_names = id else: map_names += ",%s" % id count += 1 try: grass.run_command( "r.to.rast3", input=map_names, output=output, overwrite=grass.overwrite(), ) except CalledModuleError: grass.fatal(_("Unable to create 3D raster map <%s>" % output)) grass.run_command("g.remove", flags="f", type="raster", name=null_map) title = _("Space time voxel cube") descr = _("This space time voxel cube was created with t.rast.to.rast3") # Set the unit try: grass.run_command( "r3.support", map=output, vunit=unit, title=title, description=descr, overwrite=grass.overwrite(), ) except CalledModuleError: grass.warning(_("%s failed to set units.") % "r3.support") # Register the space time voxel cube in the temporal GIS if output.find("@") >= 0: id = output else: id = output + "@" + mapset start, end = sp.get_temporal_extent_as_tuple() r3ds = tgis.Raster3DDataset(id) if r3ds.is_in_db(): r3ds.select() r3ds.delete() r3ds = tgis.Raster3DDataset(id) r3ds.load() if sp.is_time_absolute(): r3ds.set_absolute_time(start, end) else: r3ds.set_relative_time(start, end, sp.get_relative_time_unit()) r3ds.insert()
def main(): global TMPLOC, SRCGISRC, TGTGISRC, 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']) if layers: vopts['layer'] = layers if output: vopts['output'] = output vopts['snap'] = options['snap'] # try v.in.ogr directly if flags['o'] or is_projection_matching(OGRdatasource): 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) 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'] 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) try: if OGRdatasource.lower().endswith("gml"): try: from osgeo import gdal except: grass.fatal( _("Unable to load GDAL Python bindings (requires package 'python-gdal' being installed)" )) if int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION( 2, 4, 1): fix_gfsfile(OGRdatasource) 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) # 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: if OGRdatasource.lower().endswith("gml"): try: from osgeo import gdal except: grass.fatal( _("Unable to load GDAL Python bindings (requires package 'python-gdal' being installed)" )) if int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION( 2, 4, 1): fix_gfsfile(OGRdatasource) 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
def main(): global usermask, mapset, tmp_rmaps, tmp_vmaps input = options['input'] output = options['output'] tension = options['tension'] smooth = options['smooth'] method = options['method'] edge = int(options['edge']) segmax = int(options['segmax']) npmin = int(options['npmin']) lambda_ = float(options['lambda']) memory = options['memory'] quiet = True # FIXME mapset = grass.gisenv()['MAPSET'] unique = str(os.getpid()) # Shouldn't we use temp name? prefix = 'r_fillnulls_%s_' % unique failed_list = list() # a list of failed holes. Caused by issues with v.surf.rst. Connected with #1813 # check if input file exists if not grass.find_file(input)['file']: grass.fatal(_("Raster map <%s> not found") % input) # save original region reg_org = grass.region() # check if a MASK is already present # and remove it to not interfere with NULL lookup part # as we don't fill MASKed parts! if grass.find_file('MASK', mapset=mapset)['file']: usermask = "usermask_mask." + unique grass.message(_("A user raster mask (MASK) is present. Saving it...")) grass.run_command('g.rename', quiet=quiet, raster=('MASK', usermask)) # check if method is rst to use v.surf.rst if method == 'rst': # idea: filter all NULLS and grow that area(s) by 3 pixel, then # interpolate from these surrounding 3 pixel edge filling = prefix + 'filled' grass.use_temp_region() grass.run_command('g.region', align=input, quiet=quiet) region = grass.region() ns_res = region['nsres'] ew_res = region['ewres'] grass.message(_("Using RST interpolation...")) grass.message(_("Locating and isolating NULL areas...")) # creating binary (0/1) map if usermask: grass.message(_("Skipping masked raster parts")) grass.mapcalc("$tmp1 = if(isnull(\"$input\") && !($mask == 0 || isnull($mask)),1,null())", tmp1=prefix + 'nulls', input=input, mask=usermask) else: grass.mapcalc("$tmp1 = if(isnull(\"$input\"),1,null())", tmp1=prefix + 'nulls', input=input) tmp_rmaps.append(prefix + 'nulls') # restoring user's mask, if present # to ignore MASKed original values if usermask: grass.message(_("Restoring user mask (MASK)...")) try: grass.run_command('g.rename', quiet=quiet, raster=(usermask, 'MASK')) except CalledModuleError: grass.warning(_("Failed to restore user MASK!")) usermask = None # grow identified holes by X pixels grass.message(_("Growing NULL areas")) tmp_rmaps.append(prefix + 'grown') try: grass.run_command('r.grow', input=prefix + 'nulls', radius=edge + 0.01, old=1, new=1, out=prefix + 'grown', quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary map, restoring " "user mask if needed:")) # assign unique IDs to each hole or hole system (holes closer than edge distance) grass.message(_("Assigning IDs to NULL areas")) tmp_rmaps.append(prefix + 'clumped') try: grass.run_command( 'r.clump', input=prefix + 'grown', output=prefix + 'clumped', quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary map, restoring " "user mask if needed:")) # get a list of unique hole cat's grass.mapcalc("$out = if(isnull($inp), null(), $clumped)", out=prefix + 'holes', inp=prefix + 'nulls', clumped=prefix + 'clumped') tmp_rmaps.append(prefix + 'holes') # use new IDs to identify holes try: grass.run_command('r.to.vect', flags='v', input=prefix + 'holes', output=prefix + 'holes', type='area', quiet=quiet) except: grass.fatal(_("abandoned. Removing temporary maps, restoring " "user mask if needed:")) tmp_vmaps.append(prefix + 'holes') # get a list of unique hole cat's cats_file_name = grass.tempfile(False) grass.run_command( 'v.db.select', flags='c', map=prefix + 'holes', columns='cat', file=cats_file_name, quiet=quiet) cat_list = list() cats_file = open(cats_file_name) for line in cats_file: cat_list.append(line.rstrip('\n')) cats_file.close() os.remove(cats_file_name) if len(cat_list) < 1: grass.fatal(_("Input map has no holes. Check region settings.")) # GTC Hole is NULL area in a raster map grass.message(_("Processing %d map holes") % len(cat_list)) first = True hole_n = 1 for cat in cat_list: holename = prefix + 'hole_' + cat # GTC Hole is a NULL area in a raster map grass.message(_("Filling hole %s of %s") % (hole_n, len(cat_list))) hole_n = hole_n + 1 # cut out only CAT hole for processing try: grass.run_command('v.extract', input=prefix + 'holes', output=holename + '_pol', cats=cat, quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary maps, restoring " "user mask if needed:")) tmp_vmaps.append(holename + '_pol') # zoom to specific hole with a buffer of two cells around the hole to # remove rest of data try: grass.run_command('g.region', vector=holename + '_pol', align=input, w='w-%d' % (edge * 2 * ew_res), e='e+%d' % (edge * 2 * ew_res), n='n+%d' % (edge * 2 * ns_res), s='s-%d' % (edge * 2 * ns_res), quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary maps, restoring " "user mask if needed:")) # remove temporary map to not overfill disk try: grass.run_command('g.remove', flags='fb', type='vector', name=holename + '_pol', quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary maps, restoring " "user mask if needed:")) tmp_vmaps.remove(holename + '_pol') # copy only data around hole grass.mapcalc("$out = if($inp == $catn, $inp, null())", out=holename, inp=prefix + 'holes', catn=cat) tmp_rmaps.append(holename) # If here loop is split into two, next part of loop can be run in parallel # (except final result patching) # Downside - on large maps such approach causes large disk usage # grow hole border to get it's edge area tmp_rmaps.append(holename + '_grown') try: grass.run_command('r.grow', input=holename, radius=edge + 0.01, old=-1, out=holename + '_grown', quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary map, restoring " "user mask if needed:")) # no idea why r.grow old=-1 doesn't replace existing values with NULL grass.mapcalc("$out = if($inp == -1, null(), \"$dem\")", out=holename + '_edges', inp=holename + '_grown', dem=input) tmp_rmaps.append(holename + '_edges') # convert to points for interpolation tmp_vmaps.append(holename) try: grass.run_command('r.to.vect', input=holename + '_edges', output=holename, type='point', flags='z', quiet=quiet) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary maps, restoring " "user mask if needed:")) # count number of points to control segmax parameter for interpolation: pointsnumber = grass.vector_info_topo(map=holename)['points'] grass.verbose(_("Interpolating %d points") % pointsnumber) if pointsnumber < 2: grass.verbose(_("No points to interpolate")) failed_list.append(holename) continue # Avoid v.surf.rst warnings if pointsnumber < segmax: use_npmin = pointsnumber use_segmax = pointsnumber * 2 else: use_npmin = npmin use_segmax = segmax # launch v.surf.rst tmp_rmaps.append(holename + '_dem') try: grass.run_command('v.surf.rst', quiet=quiet, input=holename, elev=holename + '_dem', tension=tension, smooth=smooth, segmax=use_segmax, npmin=use_npmin) except CalledModuleError: # GTC Hole is NULL area in a raster map grass.fatal(_("Failed to fill hole %s") % cat) # v.surf.rst sometimes fails with exit code 0 # related bug #1813 if not grass.find_file(holename + '_dem')['file']: try: tmp_rmaps.remove(holename) tmp_rmaps.remove(holename + '_grown') tmp_rmaps.remove(holename + '_edges') tmp_rmaps.remove(holename + '_dem') tmp_vmaps.remove(holename) except: pass grass.warning( _("Filling has failed silently. Leaving temporary maps " "with prefix <%s> for debugging.") % holename) failed_list.append(holename) continue # append hole result to interpolated version later used to patch into original DEM if first: tmp_rmaps.append(filling) grass.run_command('g.region', align=input, raster=holename + '_dem', quiet=quiet) grass.mapcalc("$out = if(isnull($inp), null(), $dem)", out=filling, inp=holename, dem=holename + '_dem') first = False else: tmp_rmaps.append(filling + '_tmp') grass.run_command( 'g.region', align=input, raster=( filling, holename + '_dem'), quiet=quiet) grass.mapcalc( "$out = if(isnull($inp), if(isnull($fill), null(), $fill), $dem)", out=filling + '_tmp', inp=holename, dem=holename + '_dem', fill=filling) try: grass.run_command('g.rename', raster=(filling + '_tmp', filling), overwrite=True, quiet=quiet) except CalledModuleError: grass.fatal( _("abandoned. Removing temporary maps, restoring user " "mask if needed:")) # this map has been removed. No need for later cleanup. tmp_rmaps.remove(filling + '_tmp') # remove temporary maps to not overfill disk try: tmp_rmaps.remove(holename) tmp_rmaps.remove(holename + '_grown') tmp_rmaps.remove(holename + '_edges') tmp_rmaps.remove(holename + '_dem') except: pass try: grass.run_command('g.remove', quiet=quiet, flags='fb', type='raster', name=(holename, holename + '_grown', holename + '_edges', holename + '_dem')) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary maps, restoring " "user mask if needed:")) try: tmp_vmaps.remove(holename) except: pass try: grass.run_command('g.remove', quiet=quiet, flags='fb', type='vector', name=holename) except CalledModuleError: grass.fatal(_("abandoned. Removing temporary maps, restoring user mask if needed:")) # check if method is different from rst to use r.resamp.bspline if method != 'rst': grass.message(_("Using %s bspline interpolation") % method) # clone current region grass.use_temp_region() grass.run_command('g.region', align=input) reg = grass.region() # launch r.resamp.bspline tmp_rmaps.append(prefix + 'filled') # If there are no NULL cells, r.resamp.bslpine call # will end with an error although for our needs it's fine # Only problem - this state must be read from stderr new_env = dict(os.environ) new_env['LC_ALL'] = 'C' if usermask: try: p = grass.core.start_command( 'r.resamp.bspline', input=input, mask=usermask, output=prefix + 'filled', method=method, ew_step=3 * reg['ewres'], ns_step=3 * reg['nsres'], lambda_=lambda_, memory=memory, flags='n', stderr=subprocess.PIPE, env=new_env) stdout, stderr = p.communicate() if "No NULL cells found" in stderr: grass.run_command('g.copy', raster='%s,%sfilled' % (input, prefix), overwrite=True) p.returncode = 0 grass.warning(_("Input map <%s> has no holes. Copying to output without modification.") % (input,)) except CalledModuleError as e: grass.fatal(_("Failure during bspline interpolation. Error message: %s") % stderr) else: try: p = grass.core.start_command( 'r.resamp.bspline', input=input, output=prefix + 'filled', method=method, ew_step=3 * reg['ewres'], ns_step=3 * reg['nsres'], lambda_=lambda_, memory=memory, flags='n', stderr=subprocess.PIPE, env=new_env) stdout, stderr = p.communicate() if "No NULL cells found" in stderr: grass.run_command('g.copy', raster='%s,%sfilled' % (input, prefix), overwrite=True) p.returncode = 0 grass.warning(_("Input map <%s> has no holes. Copying to output without modification.") % (input,)) except CalledModuleError as e: grass.fatal(_("Failure during bspline interpolation. Error message: %s") % stderr) # restoring user's mask, if present: if usermask: grass.message(_("Restoring user mask (MASK)...")) try: grass.run_command('g.rename', quiet=quiet, raster=(usermask, 'MASK')) except CalledModuleError: grass.warning(_("Failed to restore user MASK!")) usermask = None # set region to original extents, align to input grass.run_command('g.region', n=reg_org['n'], s=reg_org['s'], e=reg_org['e'], w=reg_org['w'], align=input) # patch orig and fill map grass.message(_("Patching fill data into NULL areas...")) # we can use --o here as g.parser already checks on startup grass.run_command('r.patch', input=(input, prefix + 'filled'), output=output, overwrite=True) # restore the real region grass.del_temp_region() grass.message(_("Filled raster map is: %s") % output) # write cmd history: grass.raster_history(output) if len(failed_list) > 0: grass.warning( _("Following holes where not filled. Temporary maps with are left " "in place to allow examination of unfilled holes")) outlist = failed_list[0] for hole in failed_list[1:]: outlist = ', ' + outlist grass.message(outlist) grass.message(_("Done."))
def main(): vmap = options['map'] curr_mapset = grass.gisenv()['MAPSET'] mapset = grass.find_file(name=vmap, element='vector')['mapset'] # check if map exists in the current mapset if not mapset: grass.fatal(_("Vector map <%s> not found") % vmap) if mapset != curr_mapset: grass.fatal( _("Vector map <%s> not found in the current mapset") % vmap) # check for format vInfo = grass.vector_info(vmap) if vInfo['format'] != 'PostGIS,PostgreSQL': grass.fatal(_("Vector map <%s> is not a PG-link") % vmap) # default connection global pg_conn pg_conn = {'driver': 'pg', 'database': vInfo['pg_dbname']} # default topo schema if not options['topo_schema']: options['topo_schema'] = 'topo_%s' % options['map'] # check if topology schema already exists topo_found = False ret = grass.db_select(sql = "SELECT COUNT(*) FROM topology.topology " \ "WHERE name = '%s'" % options['topo_schema'], **pg_conn) if not ret or int(ret[0][0]) == 1: topo_found = True if topo_found: if int(os.getenv('GRASS_OVERWRITE', '0')) == 1: # -> overwrite grass.warning(_("Topology schema <%s> already exists and will be overwritten") % \ options['topo_schema']) else: grass.fatal(_("option <%s>: <%s> exists.") % \ ('topo_schema', options['topo_schema'])) # drop topo schema if exists execute(sql="SELECT topology.DropTopology('%s')" % options['topo_schema'], msg=_("Unable to remove topology schema")) # create topo schema schema, table = vInfo['pg_table'].split('.') grass.message(_("Creating new topology schema...")) execute("SELECT topology.createtopology('%s', find_srid('%s', '%s', '%s'), %s)" % \ (options['topo_schema'], schema, table, vInfo['geometry_column'], options['tolerance'])) # add topo column to the feature table grass.message(_("Adding new topology column...")) execute("SELECT topology.AddTopoGeometryColumn('%s', '%s', '%s', '%s', '%s')" % \ (options['topo_schema'], schema, table, options['topo_column'], vInfo['feature_type'])) # build topology grass.message(_("Building PostGIS topology...")) execute("UPDATE %s.%s SET %s = topology.toTopoGeom(%s, '%s', 1, %s)" % \ (schema, table, options['topo_column'], vInfo['geometry_column'], options['topo_schema'], options['tolerance']), useSelect = False) # report summary execute("SELECT topology.TopologySummary('%s')" % options['topo_schema']) return 0
def _pageInfo(self): """Info page""" # get version and web site vInfo = grass.version() if not vInfo: sys.stderr.write(_("Unable to get GRASS version\n")) infoTxt = ScrolledPanel(self.aboutNotebook) infoTxt.SetBackgroundColour('WHITE') infoTxt.SetupScrolling() infoSizer = wx.BoxSizer(wx.VERTICAL) infoGridSizer = wx.GridBagSizer(vgap=5, hgap=5) logo = os.path.join(globalvar.ICONDIR, "grass-64x64.png") logoBitmap = wx.StaticBitmap(parent=infoTxt, id=wx.ID_ANY, bitmap=wx.Bitmap(name=logo, type=wx.BITMAP_TYPE_PNG)) infoSizer.Add(item=logoBitmap, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER, border=20) infoLabel = 'GRASS GIS %s' % vInfo.get('version', _('unknown version')) if 'x86_64' in vInfo.get('build_platform', ''): infoLabel += ' (64bit)' info = wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label=infoLabel + os.linesep) info.SetFont(wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, "")) info.SetForegroundColour(wx.Colour(35, 142, 35)) infoSizer.Add(item=info, proportion=0, flag=wx.BOTTOM | wx.ALIGN_CENTER, border=1) team = wx.StaticText(parent=infoTxt, label=_grassDevTeam(1999) + '\n') infoSizer.Add(item=team, proportion=0, flag=wx.BOTTOM | wx.ALIGN_CENTER, border=1) row = 0 infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label=_('Official GRASS site:')), pos=(row, 0), flag=wx.ALIGN_RIGHT) infoGridSizer.Add(item=HyperLinkCtrl(parent=infoTxt, id=wx.ID_ANY, label='http://grass.osgeo.org'), pos=(row, 1), flag=wx.ALIGN_LEFT) row += 2 infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label='%s:' % _('Code Revision')), pos=(row, 0), flag=wx.ALIGN_RIGHT) infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label=vInfo.get('revision', '?')), pos=(row, 1), flag=wx.ALIGN_LEFT) row += 1 infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label='%s:' % _('Build Date')), pos=(row, 0), flag=wx.ALIGN_RIGHT) infoGridSizer.Add( item=wx.StaticText( parent=infoTxt, id=wx.ID_ANY, label=vInfo.get( 'build_date', '?')), pos=( row, 1), flag=wx.ALIGN_LEFT) # show only basic info # row += 1 # infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY, # label = '%s:' % _('GIS Library Revision')), # pos = (row, 0), # flag = wx.ALIGN_RIGHT) # infoGridSizer.Add(item = wx.StaticText(parent = infoTxt, id = wx.ID_ANY, # label = vInfo['libgis_revision'] + ' (' + # vInfo['libgis_date'].split(' ')[0] + ')'), # pos = (row, 1), # flag = wx.ALIGN_LEFT) row += 2 infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label='Python:'), pos=(row, 0), flag=wx.ALIGN_RIGHT) infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label=platform.python_version()), pos=(row, 1), flag=wx.ALIGN_LEFT) row += 1 infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label='wxPython:'), pos=(row, 0), flag=wx.ALIGN_RIGHT) infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label=wx.__version__), pos=(row, 1), flag=wx.ALIGN_LEFT) infoGridSizer.AddGrowableCol(0) infoGridSizer.AddGrowableCol(1) infoSizer.Add( item=infoGridSizer, proportion=1, flag=wx.EXPAND | wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL) row += 2 infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label="%s:" % _('Language')), pos=(row, 0), flag=wx.ALIGN_RIGHT) self.langUsed = grass.gisenv().get('LANG', None) if not self.langUsed: import locale loc = locale.getdefaultlocale() if loc == (None, None): self.langUsed = _('unknown') else: self.langUsed = u'%s.%s' % (loc[0], loc[1]) infoGridSizer.Add(item=wx.StaticText(parent=infoTxt, id=wx.ID_ANY, label=self.langUsed), pos=(row, 1), flag=wx.ALIGN_LEFT) infoTxt.SetSizer(infoSizer) infoSizer.Fit(infoTxt) return infoTxt
def OnSave(self, event): """Import WMS raster data into GRASS as raster layer. """ self.thread.abort(abortall=True) currmapset = grass.gisenv()['MAPSET'] self.output = self.params['output'].GetValue().strip() l_spl = self.output.strip().split("@") # check output layer msg = None if not self.output: msg = _('Missing output raster.') elif len(l_spl) > 1 and \ l_spl[1] != currmapset: msg = _('Output map can be added only to current mapset.') elif not self.overwrite.IsChecked() and\ grass.find_file(self.output, 'cell', '.')['fullname']: msg = _('Output map <%s> already exists' % self.output) if msg: GMessage(parent=self, message=msg) return self.output = l_spl[0] # check region region = self.params['region'].GetValue().strip() reg_spl = region.strip().split("@") reg_mapset = '.' if len(reg_spl) > 1: reg_mapset = reg_spl[1] if self.region_types['named'].GetValue(): if not grass.find_file(reg_spl[0], 'windows', reg_mapset)[ 'fullname']: msg = _( 'Region <%s> does not exist.' % self.params['region'].GetValue()) GWarning(parent=self, message=msg) return # create r.in.wms command cmd = ('r.in.wms', deepcopy(self.cmd[1])) if 'map' in cmd[1]: del cmd[1]['map'] cmd[1]['output'] = self.output if self.overwrite.IsChecked(): cmd[1]['overwrite'] = True env = os.environ.copy() if self.region_types['named'].GetValue(): cmd[1]['region'] = region elif self.region_types['display'].GetValue(): region = self._giface.GetMapWindow().GetMap().SetRegion() env['GRASS_REGION'] = region cmdList = cmdtuple_to_list(cmd) self.currentPid = self.thread.GetId() self.thread.RunCmd(cmdList, env=env, stderr=self.cmdStdErr) self.statusbar.SetStatusText(_("Downloading data..."))
""" import os import sys import atexit import grass.script as gscript from grass.exceptions import CalledModuleError # set graphics driver driver = "cairo" # temporary region gscript.use_temp_region() # set environment env = gscript.gisenv() overwrite = True env['GRASS_OVERWRITE'] = overwrite env['GRASS_VERBOSE'] = False env['GRASS_MESSAGE_FORMAT'] = 'standard' gisdbase = env['GISDBASE'] location = env['LOCATION_NAME'] mapset = env['MAPSET'] # set variables res = 0.3 # resolution of the region brighten = 0 # percent brightness of shaded relief render_multiplier = 1 # multiplier for rendering size whitespace = 1.5 fontsize = 36 * render_multiplier # legend font size
def main(): global rm_regions, rm_rasters, rm_vectors, tmpfolder # parameters strds = options['input'] strdsout = options['output_clouds'] threshold = float(options['threshold']) test_nprocs() # test if necessary GRASS GIS addons are installed if not grass.find_program('i.sentinel.mask', '--help'): grass.fatal( _("The 'i.sentinel.mask' module was not found, install it first:") + "\n" + "g.extension i.sentinel") if not grass.find_program('i.sentinel.mask.worker', '--help'): grass.fatal( _("The 'i.sentinel.mask.worker' module was not found, install it first:" ) + "\n" + "g.extension i.sentinel.mask.worker url=...") strdsrasters = [ x.split('|')[0] for x in grass.parse_command('t.rast.list', input=strds, flags='u') ] times = [ x.split('|')[2] for x in grass.parse_command('t.rast.list', input=strds, flags='u') ] s2_scenes = dict() for strdsrast, time in zip(strdsrasters, times): # check if strdsrast has data, skip otherwise stats = grass.parse_command("r.info", map=strdsrast, flags="r") if stats["min"] == "NULL" and stats["max"] == "NULL": grass.warning( _("Raster {} only consists of NULL() in current " "region. Cloud/shadow detection " "is skipped.").format(strdsrast)) continue parts = strdsrast.split('_') name = "%s_%s" % (parts[0], parts[1]) band = parts[2] if name not in s2_scenes: s2_scene = { 'B02': None, 'B03': None, 'B04': None, 'B08': None, 'B8A': None, 'B11': None, 'B12': None, 'date': None } s2_scene['clouds'] = "%s_clouds" % name if options['output_shadows']: s2_scene['shadows'] = "%s_shadows" % name s2_scene['shadows'] = "%s_shadows" % name if threshold > 0 or options['output_shadows']: if options['metadata'] == 'default': env = grass.gisenv() json_standard_folder = os.path.join( env['GISDBASE'], env['LOCATION_NAME'], env['MAPSET'], 'cell_misc') s2_scene['metadata'] = os.path.join( json_standard_folder, strdsrast, "description.json") elif options['metadata']: json_standard_folder = options['metadata'] s2_scene['metadata'] = os.path.join( json_standard_folder, strdsrast, "description.json") s2_scenes[name] = s2_scene s2_scenes[name][band] = strdsrast if not s2_scenes[name]['date']: if '.' in time: dateformat = '%Y-%m-%d %H:%M:%S.%f' else: dateformat = '%Y-%m-%d %H:%M:%S' s2_scenes[name]['date'] = datetime.strptime(time, dateformat) # check if all input bands are in strds for key in s2_scenes: if any([val is None for key2, val in s2_scenes[key].items()]): grass.fatal(_("Not all needed bands are given")) grass.message(_("Find clouds (and shadows) in Sentinel scenes ...")) env = grass.gisenv() start_gisdbase = env['GISDBASE'] start_location = env['LOCATION_NAME'] start_cur_mapset = env['MAPSET'] queue = ParallelModuleQueue(nprocs=options['nprocs']) bands = ['B02', 'B03', 'B04', 'B08', 'B8A', 'B11', 'B12'] number_of_scenes = len(s2_scenes) number = 0 for s2_scene_name in s2_scenes: s2_scene = s2_scenes[s2_scene_name] number += 1 grass.message( _("Processing %d of %d scenes") % (number, number_of_scenes)) if threshold > 0: with open(s2_scene['metadata'], 'r') as f: data = json.load(f) if threshold > float(data['CLOUDY_PIXEL_PERCENTAGE']): computingClouds = False else: computingClouds = True else: computingClouds = True for band in bands: rm_rasters.append("%s_double" % s2_scene[band]) if computingClouds: kwargs = dict() if options['output_shadows']: kwargs['shadow_raster'] = s2_scene['shadows'] kwargs['metadata'] = s2_scene['metadata'] kwargs['shadow_threshold'] = 1000 flags = 's' else: flags = 'sc' newmapset = s2_scene['clouds'] # grass.run_command( i_sentinel_mask = Module( 'i.sentinel.mask.worker', blue="%s@%s" % (s2_scene['B02'], start_cur_mapset), green="%s@%s" % (s2_scene['B03'], start_cur_mapset), red="%s@%s" % (s2_scene['B04'], start_cur_mapset), nir="%s@%s" % (s2_scene['B08'], start_cur_mapset), nir8a="%s@%s" % (s2_scene['B8A'], start_cur_mapset), swir11="%s@%s" % (s2_scene['B11'], start_cur_mapset), swir12="%s@%s" % (s2_scene['B12'], start_cur_mapset), flags=flags, cloud_raster=s2_scene['clouds'], newmapset=newmapset, quiet=True, run_=False, **kwargs) queue.put(i_sentinel_mask) queue.wait() # verify that switching the mapset worked env = grass.gisenv() gisdbase = env['GISDBASE'] location = env['LOCATION_NAME'] cur_mapset = env['MAPSET'] if cur_mapset != start_cur_mapset: grass.fatal("New mapset is <%s>, but should be <%s>" % (cur_mapset, start_cur_mapset)) # copy maps to current mapset for s2_scene_name in s2_scenes: s2_scene = s2_scenes[s2_scene_name] newmapset = s2_scene['clouds'] if grass.find_file(s2_scene['clouds'], element='raster', mapset=newmapset)['file']: if options['min_size_clouds']: try: grass.run_command('r.reclass.area', input="%s@%s" % (s2_scene['clouds'], newmapset), output=s2_scene['clouds'], value=options['min_size_clouds'], mode='greater', quiet=True) except Exception as e: # todo: remove workaround once r.reclass.area is updated grass.message( _('No clouds larger than %s ha detected. Image is considered cloudfree.' % options['min_size_clouds'])) exp_null = '%s = null()' % s2_scene['clouds'] grass.run_command('r.mapcalc', expression=exp_null, quiet=True) else: grass.run_command( 'g.copy', raster="%s@%s,%s" % (s2_scene['clouds'], newmapset, s2_scene['clouds'])) else: grass.run_command('r.mapcalc', expression="%s = null()" % s2_scene['clouds']) if options['output_shadows']: if grass.find_file(s2_scene['shadows'], element='raster', mapset=newmapset)['file']: if options['min_size_shadows']: try: grass.run_command('r.reclass.area', input="%s@%s" % (s2_scene['shadows'], newmapset), output=s2_scene['shadows'], value=options['min_size_shadows'], mode='greater', quiet=True) except Exception as e: # todo: remove workaround once r.reclass.area is updated grass.message( _('No shadows larger than %s ha detected. Image is considered shadowfree.' % options['min_size_shadows'])) exp_null = '%s = null()' % s2_scene['shadows'] grass.run_command('r.mapcalc', expression=exp_null, quiet=True) else: grass.run_command( 'g.copy', raster="%s@%s,%s" % (s2_scene['shadows'], newmapset, s2_scene['shadows'])) else: grass.run_command('r.mapcalc', expression="%s = null()" % s2_scene['shadows']) grass.utils.try_rmdir(os.path.join(gisdbase, location, newmapset)) # patch together clouds (and shadows) if they have the same date all_dates = [] dates_scenes = [] for s2_scene in s2_scenes: all_dates.append(s2_scenes[s2_scene]['date']) unique_dates = list(set(all_dates)) for date in unique_dates: tempdict = {} tempdict['date'] = date scenelist = [] cloudlist = [] shadowlist = [] for s2_scene in s2_scenes: if s2_scenes[s2_scene]['date'] == date: scenelist.append(s2_scene) cloudlist.append(s2_scenes[s2_scene]['clouds']) if options['output_shadows']: shadowlist.append(s2_scenes[s2_scene]['shadows']) tempdict['scenes'] = scenelist tempdict['clouds'] = cloudlist tempdict['shadows'] = shadowlist dates_scenes.append(tempdict) for date_scenes in dates_scenes: if len(date_scenes['scenes']) > 1: cloud_patch = 'clouds_patched_{}'.format( date_scenes['date'].strftime('%Y%m%d')) rm_rasters.extend(date_scenes['clouds']) grass.run_command('r.patch', input=date_scenes['clouds'], output=cloud_patch, quiet=True) if options['output_shadows']: shadow_patch = 'shadows_patched_{}'.format( date_scenes['date'].strftime('%Y%m%d')) rm_rasters.extend(date_scenes['shadows']) grass.run_command('r.patch', input=date_scenes['shadows'], output=shadow_patch, quiet=True) for scene in date_scenes['scenes']: s2_scenes[scene]['clouds'] = cloud_patch if options['output_shadows']: s2_scenes[scene]['shadows'] = shadow_patch grass.message(_("Create space time raster data set of clouds ...")) grass.run_command('t.create', output=strdsout, title="Sentinel-2 cloud mask", desc="Sentinel-2 cloud mask", quiet=True) # create register file registerfile = grass.tempfile() file = open(registerfile, 'w') clouds_registered = [] for s2_scene_name in s2_scenes: s2_scene = s2_scenes[s2_scene_name] clouds = s2_scene['clouds'] if clouds not in clouds_registered: file.write( "%s|%s\n" % (clouds, s2_scene['date'].strftime("%Y-%m-%d %H:%M:%S"))) clouds_registered.append(clouds) file.close() grass.run_command('t.register', input=strdsout, file=registerfile, quiet=True) # remove registerfile grass.try_remove(registerfile) if options['output_shadows']: grass.message(_("Create space time raster data set of shadows ...")) grass.run_command('t.create', output=options['output_shadows'], title="Sentinel-2 shadow mask", desc="Sentinel-2 shadow mask", quiet=True) # create register file registerfile = grass.tempfile() file = open(registerfile, 'w') shadows_registered = [] for s2_scene_name in s2_scenes: s2_scene = s2_scenes[s2_scene_name] shadows = s2_scene['shadows'] if shadows not in shadows_registered: file.write( "%s|%s\n" % (shadows, s2_scene['date'].strftime("%Y-%m-%d %H:%M:%S"))) shadows_registered.append(shadows) file.close() grass.run_command('t.register', input=options['output_shadows'], file=registerfile, quiet=True) # remove registerfile grass.try_remove(registerfile)
def main(): global TMPLOC, SRCGISRC, TGTGISRC, GISDBASE global tile, tmpdir, in_temp, currdir, tmpregionname in_temp = False url = options["url"] username = options["username"] password = options["password"] local = options["local"] output = options["output"] memory = options["memory"] fillnulls = flags["n"] srtmv3 = flags["2"] == 0 one = flags["1"] dozerotile = flags["z"] reproj_res = options["resolution"] overwrite = grass.overwrite() res = "00:00:03" if srtmv3: fillnulls = 0 if one: res = "00:00:01" else: one = None if len(local) == 0: if len(url) == 0: if srtmv3: if one: url = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/" else: url = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL3.003/2000.02.11/" else: url = "http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/" if len(local) == 0: local = None # are we in LatLong location? s = grass.read_command("g.proj", flags="j") kv = grass.parse_key_val(s) if fillnulls == 1 and memory <= 0: grass.warning( _( "Amount of memory to use for interpolation must be positive, setting to 300 MB" ) ) memory = "300" # make a temporary directory tmpdir = grass.tempfile() grass.try_remove(tmpdir) os.mkdir(tmpdir) currdir = os.getcwd() pid = os.getpid() # change to temporary directory os.chdir(tmpdir) in_temp = True if local is None: local = tmpdir # save region tmpregionname = "r_in_srtm_tmp_region" grass.run_command("g.region", save=tmpregionname, overwrite=overwrite) # get extents if kv["+proj"] == "longlat": reg = grass.region() else: if not options["resolution"]: grass.fatal( _("The <resolution> must be set if the projection is not 'longlat'.") ) reg2 = grass.parse_command("g.region", flags="uplg") north = [float(reg2["ne_lat"]), float(reg2["nw_lat"])] south = [float(reg2["se_lat"]), float(reg2["sw_lat"])] east = [float(reg2["ne_long"]), float(reg2["se_long"])] west = [float(reg2["nw_long"]), float(reg2["sw_long"])] reg = {} if np.mean(north) > np.mean(south): reg["n"] = max(north) reg["s"] = min(south) else: reg["n"] = min(north) reg["s"] = max(south) if np.mean(west) > np.mean(east): reg["w"] = max(west) reg["e"] = min(east) else: reg["w"] = min(west) reg["e"] = max(east) # get actual location, mapset, ... grassenv = grass.gisenv() tgtloc = grassenv["LOCATION_NAME"] tgtmapset = grassenv["MAPSET"] GISDBASE = grassenv["GISDBASE"] TGTGISRC = os.environ["GISRC"] if kv["+proj"] != "longlat": SRCGISRC, TMPLOC = createTMPlocation() if options["region"] is None or options["region"] == "": north = reg["n"] south = reg["s"] east = reg["e"] west = reg["w"] else: west, south, east, north = options["region"].split(",") west = float(west) south = float(south) east = float(east) north = float(north) # adjust extents to cover SRTM tiles: 1 degree bounds tmpint = int(north) if tmpint < north: north = tmpint + 1 else: north = tmpint tmpint = int(south) if tmpint > south: south = tmpint - 1 else: south = tmpint tmpint = int(east) if tmpint < east: east = tmpint + 1 else: east = tmpint tmpint = int(west) if tmpint > west: west = tmpint - 1 else: west = tmpint if north == south: north += 1 if east == west: east += 1 rows = abs(north - south) cols = abs(east - west) ntiles = rows * cols grass.message(_("Importing %d SRTM tiles...") % ntiles, flag="i") counter = 1 srtmtiles = "" valid_tiles = 0 for ndeg in range(south, north): for edeg in range(west, east): grass.percent(counter, ntiles, 1) counter += 1 if ndeg < 0: tile = "S" else: tile = "N" tile = tile + "%02d" % abs(ndeg) if edeg < 0: tile = tile + "W" else: tile = tile + "E" tile = tile + "%03d" % abs(edeg) grass.debug("Tile: %s" % tile, debug=1) if local != tmpdir: gotit = import_local_tile(tile, local, pid, srtmv3, one) else: gotit = download_tile(tile, url, pid, srtmv3, one, username, password) if gotit == 1: gotit = import_local_tile(tile, tmpdir, pid, srtmv3, one) if gotit == 1: grass.verbose(_("Tile %s successfully imported") % tile) valid_tiles += 1 elif dozerotile: # create tile with zeros if one: # north if ndeg < -1: tmpn = "%02d:59:59.5S" % (abs(ndeg) - 2) else: tmpn = "%02d:00:00.5N" % (ndeg + 1) # south if ndeg < 1: tmps = "%02d:00:00.5S" % abs(ndeg) else: tmps = "%02d:59:59.5N" % (ndeg - 1) # east if edeg < -1: tmpe = "%03d:59:59.5W" % (abs(edeg) - 2) else: tmpe = "%03d:00:00.5E" % (edeg + 1) # west if edeg < 1: tmpw = "%03d:00:00.5W" % abs(edeg) else: tmpw = "%03d:59:59.5E" % (edeg - 1) else: # north if ndeg < -1: tmpn = "%02d:59:58.5S" % (abs(ndeg) - 2) else: tmpn = "%02d:00:01.5N" % (ndeg + 1) # south if ndeg < 1: tmps = "%02d:00:01.5S" % abs(ndeg) else: tmps = "%02d:59:58.5N" % (ndeg - 1) # east if edeg < -1: tmpe = "%03d:59:58.5W" % (abs(edeg) - 2) else: tmpe = "%03d:00:01.5E" % (edeg + 1) # west if edeg < 1: tmpw = "%03d:00:01.5W" % abs(edeg) else: tmpw = "%03d:59:58.5E" % (edeg - 1) grass.run_command("g.region", n=tmpn, s=tmps, e=tmpe, w=tmpw, res=res) grass.run_command( "r.mapcalc", expression="%s = 0" % (tile + ".r.in.srtm.tmp." + str(pid)), quiet=True, ) grass.run_command("g.region", region=tmpregionname) # g.list with sep = comma does not work ??? pattern = "*.r.in.srtm.tmp.%d" % pid srtmtiles = grass.read_command( "g.list", type="raster", pattern=pattern, sep="newline", quiet=True ) srtmtiles = srtmtiles.splitlines() srtmtiles = ",".join(srtmtiles) grass.debug("'List of Tiles: %s" % srtmtiles, debug=1) if valid_tiles == 0: grass.run_command( "g.remove", type="raster", name=str(srtmtiles), flags="f", quiet=True ) grass.warning(_("No tiles imported")) if local != tmpdir: grass.fatal(_("Please check if local folder <%s> is correct.") % local) else: grass.fatal( _( "Please check internet connection, credentials, and if url <%s> is correct." ) % url ) grass.run_command("g.region", raster=str(srtmtiles)) grass.message(_("Patching tiles...")) if fillnulls == 0: if valid_tiles > 1: if kv["+proj"] != "longlat": grass.run_command("r.buildvrt", input=srtmtiles, output=output) else: grass.run_command("r.patch", input=srtmtiles, output=output) else: grass.run_command( "g.rename", raster="%s,%s" % (srtmtiles, output), quiet=True ) else: ncells = grass.region()["cells"] if long(ncells) > 1000000000: grass.message( _("%s cells to interpolate, this will take some time") % str(ncells), flag="i", ) if kv["+proj"] != "longlat": grass.run_command("r.buildvrt", input=srtmtiles, output=output + ".holes") else: grass.run_command("r.patch", input=srtmtiles, output=output + ".holes") mapstats = grass.parse_command( "r.univar", map=output + ".holes", flags="g", quiet=True ) if mapstats["null_cells"] == "0": grass.run_command( "g.rename", raster="%s,%s" % (output + ".holes", output), quiet=True ) else: grass.run_command( "r.resamp.bspline", input=output + ".holes", output=output + ".interp", se="0.0025", sn="0.0025", method="linear", memory=memory, flags="n", ) grass.run_command( "r.patch", input="%s,%s" % (output + ".holes", output + ".interp"), output=output + ".float", flags="z", ) grass.run_command( "r.mapcalc", expression="%s = round(%s)" % (output, output + ".float") ) grass.run_command( "g.remove", type="raster", name="%s,%s,%s" % (output + ".holes", output + ".interp", output + ".float"), flags="f", quiet=True, ) # switch to target location if kv["+proj"] != "longlat": os.environ["GISRC"] = str(TGTGISRC) # r.proj grass.message(_("Reprojecting <%s>...") % output) kwargs = { "location": TMPLOC, "mapset": "PERMANENT", "input": output, "memory": memory, "resolution": reproj_res, } if options["method"]: kwargs["method"] = options["method"] try: grass.run_command("r.proj", **kwargs) except CalledModuleError: grass.fatal(_("Unable to to reproject raster <%s>") % output) else: if fillnulls != 0: grass.run_command( "g.remove", type="raster", pattern=pattern, flags="f", quiet=True ) # nice color table grass.run_command("r.colors", map=output, color="srtm", quiet=True) # write metadata: tmphist = grass.tempfile() f = open(tmphist, "w+") f.write(os.environ["CMDLINE"]) f.close() if srtmv3: source1 = "SRTM V3" else: source1 = "SRTM V2.1" grass.run_command( "r.support", map=output, loadhistory=tmphist, description="generated by r.in.srtm.region", source1=source1, source2=(local if local != tmpdir else url), ) grass.try_remove(tmphist) grass.message(_("Done: generated map <%s>") % output)
def main(): gs.set_raise_on_error(False) options, flags = gs.parser() # import wx only after running parser # to avoid issues with complex imports when only interface is needed import wx from grass.script.setup import set_gui_path set_gui_path() from core.render import Map from mapdisp.frame import MapFrame from mapdisp.main import DMonGrassInterface from core.settings import UserSettings from grass.exceptions import CalledModuleError # define classes which needs imports as local # for longer definitions, a separate file would be a better option class RDigitMapFrame(MapFrame): def __init__( self, new_map=None, base_map=None, edit_map=None, map_type=None, ): MapFrame.__init__( self, parent=None, Map=Map(), giface=DMonGrassInterface(None), title=_("Raster Digitizer - GRASS GIS"), size=(850, 600), ) # this giface issue not solved yet, we must set mapframe afterwards self._giface._mapframe = self self._giface.mapCreated.connect(self.OnMapCreated) self._mapObj = self.GetMap() # load raster map self._addLayer(name=new_map if new_map else edit_map) # switch toolbar self.AddToolbar('rdigit', fixed=True) rdigit = self.toolbars['rdigit'] if new_map: rdigit._mapSelectionCombo.Unbind(wx.EVT_COMBOBOX) self.rdigit.SelectNewMap( standalone=True, mapName=new_map, bgMap=base_map, mapType=map_type, ) rdigit._mapSelectionCombo.Bind( wx.EVT_COMBOBOX, rdigit.OnMapSelection, ) else: rdigit._mapSelectionCombo.SetSelection(n=1) rdigit.OnMapSelection() def _addLayer(self, name, ltype='raster'): """Add layer into map :param str name: map name :param str ltype: layer type """ mapLayer = self._mapObj.AddLayer( ltype=ltype, name=name, command=['d.rast', "map={}".format(name)], active=True, hidden=False, opacity=1.0, render=True, ) def OnMapCreated(self, name, ltype): """Add new created raster layer into map :param str name: map name :param str ltype: layer type """ self._mapObj.Clean() self._addLayer(name=name, ltype=ltype) self.GetMapWindow().UpdateMap() kwargs = { 'new_map': options['create'], 'base_map': options['base'], 'edit_map': options['edit'], 'map_type': options['type'], } mapset = gs.gisenv()['MAPSET'] if kwargs['edit_map']: edit_map = gs.find_file( name=kwargs['edit_map'], element='raster', mapset=mapset, )['fullname'] if not edit_map: gs.fatal( _( "Raster map <{}> not found in current mapset.".format( options['edit'], ), ), ) else: kwargs['edit_map'] = edit_map else: if kwargs['base_map']: base_map = gs.find_file( name=kwargs['base_map'], element='raster', mapset=mapset, )['fullname'] if not base_map: gs.fatal( _( "Base raster map <{}> not found in " "current mapset.".format(options['base'], ), ), ) kwargs['base_map'] = base_map # allow immediate rendering driver = UserSettings.Get( group='display', key='driver', subkey='type', ) if driver == 'png': os.environ['GRASS_RENDER_IMMEDIATE'] = 'png' else: os.environ['GRASS_RENDER_IMMEDIATE'] = 'cairo' app = wx.App() frame = RDigitMapFrame(**kwargs) frame.Show() app.MainLoop()
def OnCmdDone(self, event): """Command done (or aborted) Sends signal mapCreated if map is recognized in output parameters or for specific modules (as r.colors). """ # Process results here try: ctime = time.time() - event.time if ctime < 60: stime = _("%d sec") % int(ctime) else: mtime = int(ctime / 60) stime = _("%(min)d min %(sec)d sec") % { "min": mtime, "sec": int(ctime - (mtime * 60)), } except KeyError: # stopped deamon stime = _("unknown") if event.aborted: # Thread aborted (using our convention of None return) self.WriteWarning( _("Please note that the data are left in" " inconsistent state and may be corrupted")) msg = _("Command aborted") else: msg = _("Command finished") self.WriteCmdLog( "(%s) %s (%s)" % (str(time.ctime()), msg, stime), notification=event.notification, ) if event.onDone: event.onDone(event) self.cmdOutputTimer.Stop() if event.cmd[0] == "g.gisenv": Debug.SetLevel() self.Redirect() # do nothing when no map added if event.returncode != 0 or event.aborted: event.Skip() return if event.cmd[0] not in globalvar.grassCmd: return # find which maps were created try: task = GUI(show=None).ParseCommand(event.cmd) except GException as e: print(e, file=sys.stderr) task = None return name = task.get_name() for p in task.get_options()["params"]: prompt = p.get("prompt", "") if prompt in ("raster", "vector", "raster_3d") and p.get( "value", None): if p.get("age", "old") == "new" or name in ( "r.colors", "r3.colors", "v.colors", "v.proj", "r.proj", ): # if multiple maps (e.g. r.series.interp), we need add each if p.get("multiple", False): lnames = p.get("value").split(",") # in case multiple input (old) maps in r.colors # we don't want to rerender it multiple times! just # once if p.get("age", "old") == "old": lnames = lnames[0:1] else: lnames = [p.get("value")] for lname in lnames: if "@" not in lname: lname += "@" + grass.gisenv()["MAPSET"] if grass.find_file( lname, element=p.get("element"))["fullname"]: self.mapCreated.emit(name=lname, ltype=prompt, add=event.addLayer) gisenv = grass.gisenv() self._giface.grassdbChanged.emit( grassdb=gisenv["GISDBASE"], location=gisenv["LOCATION_NAME"], mapset=gisenv["MAPSET"], action="new", map=lname.split("@")[0], element=prompt, ) if name == "r.mask": self.updateMap.emit() event.Skip()
def main(): vector = options["map"] layer = options["layer"] column = options["column"] value = options["value"] qcolumn = options["query_column"] where = options["where"] sqlitefile = options["sqliteextra"] mapset = grass.gisenv()["MAPSET"] # does map exist in CURRENT mapset? if not grass.find_file(vector, element="vector", mapset=mapset)["file"]: grass.fatal(_("Vector map <%s> not found in current mapset") % vector) try: f = grass.vector_db(vector)[int(layer)] except KeyError: grass.fatal( _("There is no table connected to this map. Run v.db.connect or v.db.addtable first." )) table = f["table"] database = f["database"] driver = f["driver"] # check for SQLite backend for extra functions if sqlitefile and driver != "sqlite": grass.fatal(_("Use of libsqlitefunctions only with SQLite backend")) if driver == "sqlite" and sqlitefile: if not os.access(sqlitefile, os.R_OK): grass.fatal(_("File <%s> not found") % sqlitefile) # checking column types try: coltype = grass.vector_columns(vector, layer)[column]["type"] except KeyError: grass.fatal(_("Column <%s> not found") % column) if qcolumn: if value: grass.fatal(_("<value> and <qcolumn> are mutually exclusive")) # special case: we copy from another column value = qcolumn else: if not value: grass.fatal(_("Either <value> or <qcolumn> must be given")) # we insert a value if coltype.upper() not in ["INTEGER", "DOUBLE PRECISION"]: value = "'%s'" % value cmd = "UPDATE %s SET %s=%s" % (table, column, value) if where: cmd += " WHERE " + where # SQLite: preload extra functions from extension lib if provided by user if sqlitefile: sqliteload = "SELECT load_extension('%s');\n" % sqlitefile cmd = sqliteload + cmd grass.verbose('SQL: "%s"' % cmd) grass.write_command("db.execute", input="-", database=database, driver=driver, stdin=cmd) # write cmd history: grass.vector_history(vector) return 0
def main(): global temp_dist, temp_val input = options['input'] radius = float(options['radius']) metric = options['metric'] old = options['old'] new = options['new'] mapunits = flags['m'] tmp = str(os.getpid()) temp_dist = "r.grow.tmp.%s.dist" % tmp shrink = False if radius < 0.0: shrink = True radius = -radius if new == '' and shrink == False: temp_val = "r.grow.tmp.%s.val" % tmp new = temp_val else: temp_val = None if old == '': old = input if not mapunits: kv = grass.region() scale = math.sqrt(float(kv['nsres']) * float(kv['ewres'])) radius *= scale if metric == 'euclidean': metric = 'squared' radius = radius * radius # check if input file exists if not grass.find_file(input)['file']: grass.fatal(_("Raster map <%s> not found") % input) # Workaround for r.mapcalc bug #3475 # Mapcalc will fail if output is a fully qualified map name out_name = options['output'].split('@') if len(out_name) == 2: if out_name[1] != grass.gisenv()['MAPSET']: grass.fatal(_("Output can be written only to the current mapset")) output = out_name[0] else: output = out_name[0] if shrink is False: try: grass.run_command('r.grow.distance', input=input, metric=metric, distance=temp_dist, value=temp_val) except CalledModuleError: grass.fatal(_("Growing failed. Removing temporary maps.")) grass.mapcalc( "$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))", output=output, input=input, radius=radius, old=old, new=new, dist=temp_dist) else: # shrink try: grass.run_command('r.grow.distance', input=input, metric=metric, distance=temp_dist, value=temp_val, flags='n') except CalledModuleError: grass.fatal(_("Shrinking failed. Removing temporary maps.")) grass.mapcalc( "$output = if($dist < $radius,null(),$old)", output=output, radius=radius, old=old, dist=temp_dist) grass.run_command('r.colors', map=output, raster=input) # write cmd history: grass.raster_history(output)
def main(): global TMPLOC, SRCGISRC, TGTGISRC, 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 grass.run_command('r.in.gdal', input=GDALdatasource, flags='j', errors='status', quiet=True) == 0: 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'] TGTGISRC = os.environ['GISRC'] SRCGISRC = grass.tempfile() TMPLOC = 'temp_import_location_' + str(os.getpid()) TMP_REG_NAME = 'vreg_tmp_' + 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) # prepare to set region in temp location if 'r' in region_flag: tgtregion = TMP_REG_NAME grass.run_command('v.in.region', **dict(output=tgtregion, flags='d')) # 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>") % 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', **dict(location=tgtloc, mapset=tgtmapset, input=tgtregion, output=tgtregion)) grass.run_command('g.region', **dict(vector=tgtregion)) parameters['flags'] = parameters['flags'] + region_flag 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")) if 'r' in region_flag: grass.run_command('g.remove', type="vector", flags="f", name=tgtregion) # switch to target location os.environ['GISRC'] = str(TGTGISRC) if 'r' in region_flag: grass.run_command('g.remove', **dict(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'] 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 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) # test if v.proj created a valid area if grass.vector_info_topo(vreg)['areas'] != 1: rass.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) 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' or tgtres == 'value': grass.use_temp_region() if options['extent'] == 'input': 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' or tgtres == 'value': 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
def is_location_current(database, location): genv = gisenv() if database == genv["GISDBASE"] and location == genv["LOCATION_NAME"]: return True return False
def main(): raster = options['raster'] maskcats = options['maskcats'] vector = options['vector'] layer = options['layer'] cats = options['cats'] where = options['where'] remove = flags['r'] invert = flags['i'] if not remove and not raster and not vector: grass.fatal(_("Either parameter <raster> or parameter <vector> is required")) mapset = grass.gisenv()['MAPSET'] exists = bool(grass.find_file('MASK', element='cell', mapset=mapset)['file']) if remove: # -> remove if exists: if sys.platform == 'win32': grass.run_command('g.remove', flags='if', quiet=True, type='raster', name='MASK') else: grass.run_command('g.remove', flags='f', quiet=True, type='raster', name='MASK') grass.message(_("Raster MASK removed")) else: grass.fatal(_("No existing MASK to remove")) else: # -> create if exists: if not grass.overwrite(): grass.fatal(_("MASK already found in current mapset. Delete first or overwrite.")) else: grass.warning(_("MASK already exists and will be overwritten")) grass.run_command('g.remove', flags='f', quiet=True, type='raster', name='MASK') if raster: # check if input raster exists if not grass.find_file(raster)['file']: grass.fatal(_("Raster map <%s> not found") % raster) if maskcats != '*' and not remove: if grass.raster_info(raster)['datatype'] != "CELL": grass.fatal(_("The raster map <%s> must be integer (CELL type) " " in order to use the 'maskcats' parameter") % raster) p = grass.feed_command( 'r.reclass', input=raster, output='MASK', overwrite=True, rules='-') res = "%s = 1" % maskcats p.stdin.write(encode(res)) p.stdin.close() p.wait() elif vector: vector_name = grass.find_file(vector, 'vector')['fullname'] if not vector_name: grass.fatal(_("Vector map <%s> not found") % vector) # parser bug? if len(cats) == 0: cats = None if len(where) == 0: where = None if grass.vector_info_topo(vector_name)['areas'] < 1: grass.warning(_("No area found in vector map <%s>. " "Creating a convex hull for MASK.") % vector_name) global tmp_hull tmp_hull = "tmp_hull_%d" % os.getpid() to_rast_input = tmp_hull # force 'flat' convex hull for 3D vector maps try: grass.run_command('v.hull', flags='f', quiet=True, input=vector_name, output=tmp_hull, layer=layer, cats=cats, where=where) except CalledModuleError: grass.fatal( _("Unable to create a convex hull for vector map <%s>") % vector_name) else: to_rast_input = vector_name env = os.environ.copy() if grass.verbosity() > 1: env['GRASS_VERBOSE'] = '1' grass.run_command('v.to.rast', input=to_rast_input, layer=layer, output='MASK', use='val', val='1', type='area', cats=cats, where=where, env=env) if invert: global tmp tmp = "r_mask_%d" % os.getpid() grass.run_command('g.rename', raster=('MASK', tmp), quiet=True) grass.message(_("Creating inverted raster MASK...")) grass.mapcalc("MASK = if(isnull($tmp), 1, null())", tmp=tmp) grass.verbose(_("Inverted raster MASK created")) else: grass.verbose(_("Raster MASK created")) grass.message(_("All subsequent raster operations will be limited to " "the MASK area. Removing or renaming raster map named " "'MASK' will restore raster operations to normal."))
def main(): if not flags['a'] and not options['input']: grass.fatal('Parameter <input> required') map_input = [] map_output = [] if flags['a']: mapset = grass.gisenv()['MAPSET'] map_input = map_output = grass.mlist_grouped(type='vect', mapset=mapset)[mapset] else: map_input = [options['input']] if not flags['a']: if not options['output']: map_output = map_input else: map_output = [options['output']] i = 0 for imap in map_input: # determine feature type ret = grass.read_command('v.info', flags='t', map=imap) if not ret: grass.fatal('Unable to get information about vector map <%s>' % imap) info = {} for line in ret.splitlines(): key, value = line.split('=') info[key.strip()] = int(value.strip()) # extension needed? fnum = 0 if info['points'] > 0: fnum += 1 if info['lines'] > 0: fnum += 1 if info['areas'] > 0: fnum += 1 for ftype in ('points', 'lines', 'areas'): if info[ftype] < 1: continue omap = map_output[i] if fnum != 1: omap += '_' + ftype grass.message('Converting <%s> to <%s> (%s)...' % \ (imap, omap, ftype)) if grass.overwrite(): grass.run_command('v.out.ogr', input=imap, type=ftype.rstrip('s'), dsn="PG:dbname=%s" % options['dbname'], olayer=omap, format='PostgreSQL', lco="OVERWRITE=YES") else: grass.run_command('v.out.ogr', input=imap, type=ftype.rstrip('s'), dsn="PG:dbname=%s" % options['dbname'], olayer=omap, format='PostgreSQL') i += 1 return 0
def main(): first = options['first'] second = options['second'] output = options['output'] percent = options['percent'] mapset = grass.gisenv()['MAPSET'] if not grass.overwrite(): for ch in ['r', 'g', 'b']: map = '%s.%s' % (output, ch) if grass.find_file(map, element='cell', mapset=mapset)['file']: grass.fatal(_("Raster map <%s> already exists.") % map) percent = float(percent) perc_inv = 100.0 - percent frac1 = percent / 100.0 frac2 = perc_inv / 100.0 grass.message(_("Calculating the three component maps...")) template = string.Template( "$$output.$ch = if(isnull($$first), $ch#$$second, if(isnull($$second), $ch#$$first, $$frac1 * $ch#$$first + $$frac2 * $ch#$$second))" ) cmd = [template.substitute(ch=ch) for ch in ['r', 'g', 'b']] cmd = ';'.join(cmd) grass.mapcalc(cmd, output=output, first=first, second=second, frac1=frac1, frac2=frac2) for ch in ['r', 'g', 'b']: map = "%s.%s" % (output, ch) grass.run_command('r.colors', map=map, color='grey255') grass.run_command('r.support', map=map, history="", title="Color blend of %s and %s" % (first, second), description="generated by r.blend") grass.run_command('r.support', map=map, history="r.blend %s channel." % ch) grass.run_command('r.support', map=map, history=" %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second)) grass.run_command('r.support', map=map, history="") grass.run_command('r.support', map=map, history=os.environ['CMDLINE']) if flags['c']: grass.run_command('r.composite', r='%s.r' % output, g='%s.g' % output, b='%s.b' % output, output=output) grass.run_command('r.support', map=output, history="", title="Color blend of %s and %s" % (first, second), description="generated by r.blend") grass.run_command('r.support', map=output, history=" %d%% of %s, %d%% of %s" % (percent, first, perc_inv, second)) grass.run_command('r.support', map=output, history="") grass.run_command('r.support', map=output, history=os.environ['CMDLINE']) else: grass.message( _("Done. Use the following command to visualize the result:")) grass.message( _("d.rgb r=%s.r g=%s.g b=%s.b") % (output, output, output))
def main(): input = options['input'] db_table = options['db_table'] output = options['output'] key = options['key'] mapset = grass.gisenv()['MAPSET'] if db_table: input = db_table if not output: tmpname = input.replace('.', '_') output = grass.basename(tmpname) # check if table exists try: nuldev = file(os.devnull, 'w+') s = grass.read_command('db.tables', flags='p', quiet=True, stderr=nuldev) nuldev.close() except CalledModuleError: # check connection parameters, set if uninitialized grass.read_command('db.connect', flags='c') s = grass.read_command('db.tables', flags='p', quiet=True) for l in s.splitlines(): if l == output: if grass.overwrite(): grass.warning( _("Table <%s> already exists and will be " "overwritten") % output) grass.write_command('db.execute', input='-', stdin="DROP TABLE %s" % output) break else: grass.fatal(_("Table <%s> already exists") % output) # treat DB as real vector map... layer = db_table if db_table else None vopts = {} if options['encoding']: vopts['encoding'] = options['encoding'] try: grass.run_command('v.in.ogr', flags='o', input=input, output=output, layer=layer, quiet=True, **vopts) except CalledModuleError: if db_table: grass.fatal( _("Input table <%s> not found or not readable") % input) else: grass.fatal(_("Input DSN <%s> not found or not readable") % input) # rename ID col if requested from cat to new name if key: grass.write_command('db.execute', quiet=True, input='-', stdin="ALTER TABLE %s ADD COLUMN %s integer" % (output, key)) grass.write_command('db.execute', quiet=True, input='-', stdin="UPDATE %s SET %s=cat" % (output, key)) # ... and immediately drop the empty geometry vectfile = grass.find_file(output, element='vector', mapset=mapset)['file'] if not vectfile: grass.fatal(_("Something went wrong. Should not happen")) else: # remove the vector part grass.run_command('v.db.connect', quiet=True, map=output, layer='1', flags='d') grass.run_command('g.remove', flags='f', quiet=True, type='vector', name=output) # get rid of superfluous auto-added cat column (and cat_ if present) nuldev = file(os.devnull, 'w+') grass.run_command('db.dropcolumn', quiet=True, flags='f', table=output, column='cat', stdout=nuldev, stderr=nuldev) nuldev.close() records = grass.db_describe(output)['nrows'] grass.message(_("Imported table <%s> with %d rows") % (output, records))
def main(): force = flags['f'] map = options['map'] table = options['table'] layer = options['layer'] # We check for existence of the map in the current mapset before # doing any other operation. info = gscript.find_file(map, element='vector', mapset=".") if not info['file']: mapset = gscript.gisenv()["MAPSET"] # Message is formulated in the way that it does not mislead # in case where a map of the same name is in another mapset. gscript.fatal( _("Vector map <{name}> not found" " in the current mapset ({mapset})").format(name=map, mapset=mapset)) # do some paranoia tests as well: f = gscript.vector_layer_db(map, layer) if not table: # Removing table name connected to selected layer table = f['table'] if not table: gscript.fatal(_("No table assigned to layer <%s>") % layer) else: # Removing user specified table existingtable = f['table'] if existingtable != table: gscript.fatal( _("User selected table <%s> but the table <%s> " "is linked to layer <%s>") % (table, existingtable, layer)) # we use the DB settings selected layer database = f['database'] driver = f['driver'] gscript.message( _("Removing table <%s> linked to layer <%s> of vector" " map <%s>") % (table, layer, map)) if not force: gscript.message( _("You must use the -f (force) flag to actually " "remove the table. Exiting.")) gscript.message(_("Leaving map/table unchanged.")) sys.exit(0) gscript.message(_("Dropping table <%s>...") % table) try: gscript.write_command('db.execute', stdin="DROP TABLE %s" % table, input='-', database=database, driver=driver) except CalledModuleError: gscript.fatal(_("An error occurred while running db.execute")) gscript.run_command('v.db.connect', flags='d', map=map, layer=layer) gscript.message(_("Current attribute table link(s):")) # silently test first to avoid confusing error messages nuldev = open(os.devnull, 'w') try: gscript.run_command('v.db.connect', flags='p', map=map, quiet=True, stdout=nuldev, stderr=nuldev) except CalledModuleError: gscript.message(_("(No database links remaining)")) else: gscript.run_command('v.db.connect', flags='p', map=map) # write cmd history: gscript.vector_history(map)
def main(): global TMPLOC, SRCGISRC, TGTGISRC, GISDBASE global tile, tmpdir, in_temp, currdir, tmpregionname in_temp = False url = options['url'] username = options['username'] password = options['password'] local = options['local'] output = options['output'] memory = options['memory'] fillnulls = flags['n'] srtmv3 = (flags['2'] == 0) one = flags['1'] dozerotile = flags['z'] reproj_res = options['resolution'] overwrite = grass.overwrite() res = '00:00:03' if srtmv3: fillnulls = 0 if one: res = '00:00:01' else: one = None if len(local) == 0: if len(url) == 0: if srtmv3: if one: url = 'https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/' else: url = 'https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL3.003/2000.02.11/' else: url = 'http://dds.cr.usgs.gov/srtm/version2_1/SRTM3/' if len(local) == 0: local = None # are we in LatLong location? s = grass.read_command("g.proj", flags='j') kv = grass.parse_key_val(s) if fillnulls == 1 and memory <= 0: grass.warning( _("Amount of memory to use for interpolation must be positive, setting to 300 MB" )) memory = '300' # make a temporary directory tmpdir = grass.tempfile() grass.try_remove(tmpdir) os.mkdir(tmpdir) currdir = os.getcwd() pid = os.getpid() # change to temporary directory os.chdir(tmpdir) in_temp = True if local is None: local = tmpdir # save region tmpregionname = 'r_in_srtm_tmp_region' grass.run_command('g.region', save=tmpregionname, overwrite=overwrite) # get extents if kv['+proj'] == 'longlat': reg = grass.region() else: if not options['resolution']: grass.fatal( _("The <resolution> must be set if the projection is not 'longlat'." )) reg2 = grass.parse_command('g.region', flags='uplg') north = [float(reg2['ne_lat']), float(reg2['nw_lat'])] south = [float(reg2['se_lat']), float(reg2['sw_lat'])] east = [float(reg2['ne_long']), float(reg2['se_long'])] west = [float(reg2['nw_long']), float(reg2['sw_long'])] reg = {} if np.mean(north) > np.mean(south): reg['n'] = max(north) reg['s'] = min(south) else: reg['n'] = min(north) reg['s'] = max(south) if np.mean(west) > np.mean(east): reg['w'] = max(west) reg['e'] = min(east) else: reg['w'] = min(west) reg['e'] = max(east) # get actual location, mapset, ... grassenv = grass.gisenv() tgtloc = grassenv['LOCATION_NAME'] tgtmapset = grassenv['MAPSET'] GISDBASE = grassenv['GISDBASE'] TGTGISRC = os.environ['GISRC'] if kv['+proj'] != 'longlat': SRCGISRC, TMPLOC = createTMPlocation() if options['region'] is None or options['region'] == '': north = reg['n'] south = reg['s'] east = reg['e'] west = reg['w'] else: west, south, east, north = options['region'].split(',') west = float(west) south = float(south) east = float(east) north = float(north) # adjust extents to cover SRTM tiles: 1 degree bounds tmpint = int(north) if tmpint < north: north = tmpint + 1 else: north = tmpint tmpint = int(south) if tmpint > south: south = tmpint - 1 else: south = tmpint tmpint = int(east) if tmpint < east: east = tmpint + 1 else: east = tmpint tmpint = int(west) if tmpint > west: west = tmpint - 1 else: west = tmpint if north == south: north += 1 if east == west: east += 1 rows = abs(north - south) cols = abs(east - west) ntiles = rows * cols grass.message(_("Importing %d SRTM tiles...") % ntiles, flag='i') counter = 1 srtmtiles = '' valid_tiles = 0 for ndeg in range(south, north): for edeg in range(west, east): grass.percent(counter, ntiles, 1) counter += 1 if ndeg < 0: tile = 'S' else: tile = 'N' tile = tile + '%02d' % abs(ndeg) if edeg < 0: tile = tile + 'W' else: tile = tile + 'E' tile = tile + '%03d' % abs(edeg) grass.debug("Tile: %s" % tile, debug=1) if local != tmpdir: gotit = import_local_tile(tile, local, pid, srtmv3, one) else: gotit = download_tile(tile, url, pid, srtmv3, one, username, password) if gotit == 1: gotit = import_local_tile(tile, tmpdir, pid, srtmv3, one) if gotit == 1: grass.verbose(_("Tile %s successfully imported") % tile) valid_tiles += 1 elif dozerotile: # create tile with zeros if one: # north if ndeg < -1: tmpn = '%02d:59:59.5S' % (abs(ndeg) - 2) else: tmpn = '%02d:00:00.5N' % (ndeg + 1) # south if ndeg < 1: tmps = '%02d:00:00.5S' % abs(ndeg) else: tmps = '%02d:59:59.5N' % (ndeg - 1) # east if edeg < -1: tmpe = '%03d:59:59.5W' % (abs(edeg) - 2) else: tmpe = '%03d:00:00.5E' % (edeg + 1) # west if edeg < 1: tmpw = '%03d:00:00.5W' % abs(edeg) else: tmpw = '%03d:59:59.5E' % (edeg - 1) else: # north if ndeg < -1: tmpn = '%02d:59:58.5S' % (abs(ndeg) - 2) else: tmpn = '%02d:00:01.5N' % (ndeg + 1) # south if ndeg < 1: tmps = '%02d:00:01.5S' % abs(ndeg) else: tmps = '%02d:59:58.5N' % (ndeg - 1) # east if edeg < -1: tmpe = '%03d:59:58.5W' % (abs(edeg) - 2) else: tmpe = '%03d:00:01.5E' % (edeg + 1) # west if edeg < 1: tmpw = '%03d:00:01.5W' % abs(edeg) else: tmpw = '%03d:59:58.5E' % (edeg - 1) grass.run_command('g.region', n=tmpn, s=tmps, e=tmpe, w=tmpw, res=res) grass.run_command('r.mapcalc', expression="%s = 0" % (tile + '.r.in.srtm.tmp.' + str(pid)), quiet=True) grass.run_command('g.region', region=tmpregionname) # g.list with sep = comma does not work ??? pattern = '*.r.in.srtm.tmp.%d' % pid srtmtiles = grass.read_command('g.list', type='raster', pattern=pattern, sep='newline', quiet=True) srtmtiles = srtmtiles.splitlines() srtmtiles = ','.join(srtmtiles) grass.debug("'List of Tiles: %s" % srtmtiles, debug=1) if valid_tiles == 0: grass.run_command('g.remove', type='raster', name=str(srtmtiles), flags='f', quiet=True) grass.warning(_("No tiles imported")) if local != tmpdir: grass.fatal( _("Please check if local folder <%s> is correct.") % local) else: grass.fatal( _("Please check internet connection, credentials, and if url <%s> is correct." ) % url) grass.run_command('g.region', raster=str(srtmtiles)) grass.message(_("Patching tiles...")) if fillnulls == 0: if valid_tiles > 1: if kv['+proj'] != 'longlat': grass.run_command('r.buildvrt', input=srtmtiles, output=output) else: grass.run_command('r.patch', input=srtmtiles, output=output) else: grass.run_command('g.rename', raster='%s,%s' % (srtmtiles, output), quiet=True) else: ncells = grass.region()['cells'] if long(ncells) > 1000000000: grass.message( _("%s cells to interpolate, this will take some time") % str(ncells), flag='i') if kv['+proj'] != 'longlat': grass.run_command('r.buildvrt', input=srtmtiles, output=output + '.holes') else: grass.run_command('r.patch', input=srtmtiles, output=output + '.holes') mapstats = grass.parse_command('r.univar', map=output + '.holes', flags='g', quiet=True) if mapstats['null_cells'] == '0': grass.run_command('g.rename', raster='%s,%s' % (output + '.holes', output), quiet=True) else: grass.run_command('r.resamp.bspline', input=output + '.holes', output=output + '.interp', se='0.0025', sn='0.0025', method='linear', memory=memory, flags='n') grass.run_command('r.patch', input='%s,%s' % (output + '.holes', output + '.interp'), output=output + '.float', flags='z') grass.run_command('r.mapcalc', expression='%s = round(%s)' % (output, output + '.float')) grass.run_command( 'g.remove', type='raster', name='%s,%s,%s' % (output + '.holes', output + '.interp', output + '.float'), flags='f', quiet=True) # switch to target location if kv['+proj'] != 'longlat': os.environ['GISRC'] = str(TGTGISRC) # r.proj grass.message(_("Reprojecting <%s>...") % output) kwargs = { 'location': TMPLOC, 'mapset': 'PERMANENT', 'input': output, 'memory': memory, 'resolution': reproj_res } if options['method']: kwargs['method'] = options['method'] try: grass.run_command('r.proj', **kwargs) except CalledModuleError: grass.fatal(_("Unable to to reproject raster <%s>") % output) else: if fillnulls != 0: grass.run_command('g.remove', type='raster', pattern=pattern, flags='f', quiet=True) # nice color table grass.run_command('r.colors', map=output, color='srtm', quiet=True) # write metadata: tmphist = grass.tempfile() f = open(tmphist, 'w+') f.write(os.environ['CMDLINE']) f.close() if srtmv3: source1 = 'SRTM V3' else: source1 = 'SRTM V2.1' grass.run_command('r.support', map=output, loadhistory=tmphist, description='generated by r.in.srtm.region', source1=source1, source2=(local if local != tmpdir else url)) grass.try_remove(tmphist) grass.message(_("Done: generated map <%s>") % output)
def main(): # split input images all_images = options["input"] images = all_images.split(",") # number of images n_images = len(images) # database path dbopt = options["database"] # output suffix suffix = options["suffix"] # output mosaic map mosaic = options["output"] output_names = [] # name for average table table_ave = "t%s_average" % suffix # increment of one the maximum value for a correct use of range function max_value = int(options["max"]) + 1 # if the db path is the default one if dbopt.find("$GISDBASE/$LOCATION_NAME/$MAPSET") == 0: dbopt_split = dbopt.split("/")[-1] env = grass.gisenv() path = os.path.join(env["GISDBASE"], env["LOCATION_NAME"], env["MAPSET"]) dbpath = os.path.join(path, dbopt_split) else: if os.access(os.path.dirname(dbopt), os.W_OK): path = os.path.dirname(dbopt) dbpath = dbopt else: grass.fatal( _( "Folder to write database files does not" + " exist or is not writeable" ) ) # connect to the db db = sqlite3.connect(dbpath) curs = db.cursor() grass.message(_("Calculating Cumulative Distribution Functions ...")) # number of pixels per value, summarized for all images numPixelValue = list(range(0, max_value)) for n in range(0, max_value): numPixelValue[n] = 0 # cumulative histogram for each value and each image cumulHistoValue = list(range(0, max_value)) # set up temp region only once grass.use_temp_region() # for each image for i in images: iname = i.split("@")[0] # drop table if exist query_drop = 'DROP TABLE if exists "t%s"' % iname curs.execute(query_drop) # create table query_create = 'CREATE TABLE "t%s" (grey_value integer,pixel_frequency ' % iname query_create += "integer, cumulative_histogram integer, cdf real)" curs.execute(query_create) index_create = 'CREATE UNIQUE INDEX "t%s_grey_value" ON "t%s" (grey_value) ' % ( iname, iname, ) curs.execute(index_create) # set the region on the raster grass.run_command("g.region", raster=i) # calculate statistics stats_out = grass.pipe_command("r.stats", flags="cin", input=i, separator=":") stats = stats_out.communicate()[0].decode("utf-8").split("\n")[:-1] stats_dict = dict(s.split(":", 1) for s in stats) cdf = 0 curs.execute("BEGIN") # for each number in the range for n in range(0, max_value): # try to insert the values otherwise insert 0 try: val = int(stats_dict[str(n)]) cdf += val numPixelValue[n] += val insert = 'INSERT INTO "t%s" VALUES (%i, %i, %i, 0.000000)' % ( iname, n, val, cdf, ) curs.execute(insert) except: insert = 'INSERT INTO "t%s" VALUES (%i, 0, %i, 0.000000)' % ( iname, n, cdf, ) curs.execute(insert) # save cumulative_histogram for the second loop cumulHistoValue[n] = cdf curs.execute("COMMIT") db.commit() # number of pixel is the cdf value numPixel = cdf # for each number in the range # cdf is updated using the number of non-null pixels for the current image curs.execute("BEGIN") for n in range(0, max_value): # select value for cumulative_histogram for the range number """ select_ch = "SELECT cumulative_histogram FROM \"t%s\" WHERE " % iname select_ch += "(grey_value=%i)" % n result = curs.execute(select_ch) val = result.fetchone()[0] """ val = cumulHistoValue[n] # update cdf with new value if val != 0 and numPixel != 0: update_cdf = round(float(val) / float(numPixel), 6) update_cdf = 'UPDATE "t%s" SET cdf=%s WHERE (grey_value=%i)' % ( iname, update_cdf, n, ) curs.execute(update_cdf) curs.execute("COMMIT") db.commit() db.commit() pixelTot = 0 # get total number of pixels divided by number of images # for each number in the range for n in range(0, max_value): """ numPixel = 0 # for each image for i in images: iname = i.split('@')[0] pixel_freq = "SELECT pixel_frequency FROM \"t%s\" WHERE (grey_value=%i)" % ( iname, n) result = curs.execute(pixel_freq) val = result.fetchone()[0] numPixel += val """ # calculate number of pixel divide by number of images div = int(numPixelValue[n] / n_images) pixelTot += div # drop average table query_drop = "DROP TABLE if exists %s" % table_ave curs.execute(query_drop) # create average table query_create = "CREATE TABLE %s (grey_value integer,average " % table_ave query_create += "integer, cumulative_histogram integer, cdf real)" curs.execute(query_create) index_create = 'CREATE UNIQUE INDEX "%s_grey_value" ON "%s" (grey_value) ' % ( table_ave, table_ave, ) curs.execute(index_create) cHist = 0 # for each number in the range curs.execute("BEGIN") for n in range(0, max_value): tot = 0 """ # for each image for i in images: iname = i.split('@')[0] # select pixel frequency pixel_freq = "SELECT pixel_frequency FROM \"t%s\" WHERE (grey_value=%i)" % ( iname, n) result = curs.execute(pixel_freq) val = result.fetchone()[0] tot += val """ tot = numPixelValue[n] # calculate new value of pixel_frequency average = tot / n_images cHist = cHist + int(average) # insert new values into average table if cHist != 0 and pixelTot != 0: cdf = float(cHist) / float(pixelTot) insert = "INSERT INTO %s VALUES (%i, %i, %i, %s)" % ( table_ave, n, int(average), cHist, cdf, ) curs.execute(insert) curs.execute("COMMIT") db.commit() # for each image grass.message(_("Reclassifying bands based on average histogram...")) for i in images: iname = i.split("@")[0] grass.run_command("g.region", raster=i) # write average rules file outfile = open(grass.tempfile(), "w") new_grey = 0 for n in range(0, max_value): select_newgrey = 'SELECT b.grey_value FROM "t%s" as a, ' % iname select_newgrey += ( "%s as b WHERE a.grey_value=%i " % (table_ave, n) + "ORDER BY abs(a.cdf-b.cdf) LIMIT 1" ) # write line with old and new value try: result_new = curs.execute(select_newgrey) new_grey = result_new.fetchone()[0] out_line = "%d = %d\n" % (n, new_grey) outfile.write(out_line) except: out_line = "%d = %d\n" % (n, new_grey) outfile.write(out_line) outfile.close() outname = "%s.%s" % (iname, suffix) # check if a output map already exists result = grass.core.find_file(outname, element="cell") if result["fullname"] and grass.overwrite(): grass.run_command("g.remove", flags="f", type="raster", name=outname) grass.run_command("r.reclass", input=i, out=outname, rules=outfile.name) elif result["fullname"] and not grass.overwrite(): grass.warning( _("Raster map %s already exists and will not be overwritten" % i) ) else: grass.run_command("r.reclass", input=i, out=outname, rules=outfile.name) output_names.append(outname) # remove the rules file grass.try_remove(outfile.name) # write cmd history: grass.raster_history(outname) db.commit() db.close() if mosaic: grass.message(_("Processing mosaic <%s>..." % mosaic)) grass.run_command("g.region", raster=all_images) grass.run_command("r.patch", input=output_names, output=mosaic)
def OnSelectMap(self, event): """Select vector map layer for editing If there is a vector map layer already edited, this action is firstly terminated. The map layer is closed. After this the selected map layer activated for editing. """ if event.GetSelection() == 0: # create new vector map layer if self.mapLayer: openVectorMap = self.mapLayer.GetName( fullyQualified=False)['name'] else: openVectorMap = None dlg = CreateNewVector(self.parent, exceptMap=openVectorMap, giface=self._giface, cmd=(('v.edit', { 'tool': 'create' }, 'map')), disableAdd=True) if dlg and dlg.GetName(): # add layer to map layer tree/map display mapName = dlg.GetName() + '@' + grass.gisenv()['MAPSET'] self._giface.GetLayerList().AddLayer( ltype='vector', name=mapName, checked=True, cmd=['d.vect', 'map=%s' % mapName]) vectLayers = self.UpdateListOfLayers(updateTool=True) selection = vectLayers.index(mapName) # create table ? if dlg.IsChecked('table'): # TODO: replace this by signal # also note that starting of tools such as atm, iclass, # plots etc. should be handled in some better way # than starting randomly from mapdisp and lmgr lmgr = self.parent.GetLayerManager() if lmgr: lmgr.OnShowAttributeTable(None, selection='table') dlg.Destroy() else: self.combo.SetValue(_('Select vector map')) if dlg: dlg.Destroy() return else: selection = event.GetSelection( ) - 1 # first option is 'New vector map' # skip currently selected map if self.layers[selection] == self.mapLayer: return if self.mapLayer: # deactive map layer for editing self.StopEditing() # select the given map layer for editing self.StartEditing(self.layers[selection]) event.Skip()
sys.path.append(gpydir) ########### DATA # Set GISDBASE environment variable os.environ['GISDBASE'] = gisdb # import GRASS Python bindings (see also pygrass) import grass.script as gscript import grass.script.setup as gsetup ########### # launch session gsetup.init(gisbase, gisdb, location, mapset) gscript.message('Current GRASS GIS 7 environment:') print gscript.gisenv() # ------------- import numpy as np from matplotlib import pyplot as plt #from pyevtk.hl import gridToVTK from grass import script as gscript from grass.script import array as garray import os import tempfile from grass.pygrass.modules.shortcuts import general as g from grass.pygrass.modules.shortcuts import raster as r from grass.pygrass.modules.shortcuts import vector as v from grass.pygrass.modules import Module from grass.pygrass.vector import VectorTopo
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 _createWidgets(self): self.labels = {} self.params = {} self.labels['output'] = StaticText( parent=self, id=wx.ID_ANY, label=_("Name for output raster map:")) self.params['output'] = Select(parent=self, type='raster', mapsets=[grass.gisenv()['MAPSET']], size=globalvar.DIALOG_GSELECT_SIZE) self.regionStBoxLabel = StaticBox(parent=self, id=wx.ID_ANY, label=" %s " % _("Export region")) self.region_types_order = ['display', 'comp', 'named'] self.region_types = {} self.region_types['display'] = RadioButton(parent=self, label=_("Map display"), style=wx.RB_GROUP) self.region_types['comp'] = RadioButton( parent=self, label=_("Computational region")) self.region_types['named'] = RadioButton(parent=self, label=_("Named region")) self.region_types['display'].SetToolTip( _("Extent and resolution" " are based on Map Display geometry.")) self.region_types['comp'].SetToolTip( _("Extent and resolution" " are based on computational region.")) self.region_types['named'].SetToolTip( _("Extent and resolution" " are based on named region.")) self.region_types['display'].SetValue( True) # set default as map display self.overwrite = wx.CheckBox(parent=self, id=wx.ID_ANY, label=_("Overwrite existing raster map")) self.named_reg_panel = wx.Panel(parent=self, id=wx.ID_ANY) self.labels['region'] = StaticText(parent=self.named_reg_panel, id=wx.ID_ANY, label=_("Choose named region:")) self.params['region'] = Select(parent=self.named_reg_panel, type='region', size=globalvar.DIALOG_GSELECT_SIZE) # buttons self.btn_close = Button(parent=self, id=wx.ID_CLOSE) self.SetEscapeId(self.btn_close.GetId()) self.btn_close.SetToolTip(_("Close dialog")) self.btn_ok = Button(parent=self, label=_("&Save layer")) self.btn_ok.SetToolTip(_("Save web service layer as raster map")) # statusbar self.statusbar = wx.StatusBar(parent=self, id=wx.ID_ANY) self._layout()