def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False)) #SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True)) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False)) #======================================================================= # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 100)) #======================================================================= SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) SextanteConfig.addSetting( Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
def checkBeforeOpeningParametersDialog(self): msg = GrassUtils.checkGrassIsInstalled() if msg is not None: html = ("<p>This algorithm requires GRASS to be run." "Unfortunately, it seems that GRASS is not installed in your system, or it is not correctly configured to be used from QGIS</p>") html += '<p><a href= "http://docs.qgis.org/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure GRASS to be used with SEXTANTE</p>' return html
def helpFile(self): folder = GrassUtils.grassHelpPath() helpfile = str(folder) + os.sep + self.grassName + ".html" if os.path.exists(helpfile): return helpfile else: raise WrongHelpFileException("Grass help folder is not correctly configured.\nPlease configure it")
def checkBeforeOpeningParametersDialog(self): msg = GrassUtils.checkGrassIsInstalled() if msg is not None: html = ( "<p>This algorithm requires GRASS to be run." "Unfortunately, it seems that GRASS is not installed in your system, or it is not correctly configured to be used from QGIS</p>" ) html += '<p><a href= "http://docs.qgis.org/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure GRASS to be used with SEXTANTE</p>' return html
def getPostProcessingErrorMessage(self, wrongLayers): html = AlgorithmProvider.getPostProcessingErrorMessage(self, wrongLayers) msg = GrassUtils.checkGrassIsInstalled(True) html += ("<p>This algorithm requires GRASS to be run. A test to check if GRASS is correctly installed " "and configured in your system has been performed, with the following result:</p><ul><i>") if msg is None: html += "GRASS seems to be correctly installed and configured</li></ul>" else: html += msg + "</i></li></ul>" html += '<p><a href= "http://docs.qgis.org/2.0/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure GRASS to be used with SEXTANTE</p>' return html
def processAlgorithm(self, progress): commands = [] vector = self.getParameterValue(self.VECTOR) elevation = self.getParameterValue(self.ELEVATION) region = str(self.getParameterValue( self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") command = "g.region " command += "n=" + str(regionCoords[3]) command += " s=" + str(regionCoords[2]) command += " e=" + str(regionCoords[1]) command += " w=" + str(regionCoords[0]) cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER) if cellsize: command += " res=" + str(cellsize) else: command += " res=" + str(self.getDefaultCellsize()) commands.append(command) command = "nviz" if vector: layers = vector.split(";") for layer in layers: cmd, newfilename = self.exportVectorLayer(layer) commands.append(cmd) vector = vector.replace(layer, newfilename) command += (" vector=" + vector.replace(";", ",")) if elevation: layers = elevation.split(";") for layer in layers: cmd, newfilename = self.exportRasterLayer(layer) commands.append(cmd) elevation = elevation.replace(layer, newfilename) command += (" elevation=" + elevation.replace(";", ",")) if elevation is None and vector is None: command += " -q" commands.append(command) GrassUtils.createTempMapset() GrassUtils.executeGrass(commands, progress)
def processAlgorithm(self, progress): commands = [] vector = self.getParameterValue(self.VECTOR); elevation = self.getParameterValue(self.ELEVATION); region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") command = "g.region " command += "n=" + str(regionCoords[3]) command +=" s=" + str(regionCoords[2]) command +=" e=" + str(regionCoords[1]) command +=" w=" + str(regionCoords[0]) cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER) if cellsize: command +=" res=" + str(cellsize); else: command +=" res=" + str(self.getDefaultCellsize()) commands.append(command) command = "nviz" if vector: layers = vector.split(";") for layer in layers: cmd, newfilename = self.exportVectorLayer(layer) commands.append(cmd) vector = vector.replace(layer, newfilename) command += (" vector=" + vector.replace(";", ",")) if elevation: layers = elevation.split(";") for layer in layers: cmd, newfilename = self.exportRasterLayer(layer) commands.append(cmd) elevation = elevation.replace(layer, newfilename) command += (" elevation=" + elevation.replace(";", ",")) if elevation is None and vector is None: command += " -q" commands.append(command) GrassUtils.createTempMapset(); GrassUtils.executeGrass(commands, progress)
def createAlgsList(self): self.preloadedAlgs = [] folder = GrassUtils.grassDescriptionPath() for descriptionFile in os.listdir(folder): if descriptionFile.endswith("txt"): try: alg = GrassAlgorithm(os.path.join(folder, descriptionFile)) if alg.name.strip() != "": self.preloadedAlgs.append(alg) else: SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + descriptionFile) except Exception,e: SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + descriptionFile)
def processAlgorithm(self, progress): commands = [] command = "nviz" vector = self.getParameterValue(self.VECTOR) elevation = self.getParameterValue(self.ELEVATION) if vector: layers = vector.split(";") for layer in layers: newfilename = self.exportVectorLayer(layer) vector = vector.replace(layer, newfilename) command += (" vector=" + vector.replace(";", ",")) if elevation: layers = elevation.split(";") for layer in layers: newfilename = self.exportRasterLayer(layer) elevation = elevation.replace(layer, newfilename) command += (" elevation=" + elevation.replace(";", ",")) if elevation is None and vector is None: command += " -q" commands.append(command) GrassUtils.createTempMapset() GrassUtils.executeGrass(commands, progress)
def processAlgorithm(self, progress): commands = [] command = "nviz" vector = self.getParameterValue(self.VECTOR); elevation = self.getParameterValue(self.ELEVATION); if vector: layers = vector.split(";") for layer in layers: newfilename = self.exportVectorLayer(layer) vector = vector.replace(layer, newfilename) command += (" vector=" + vector.replace(";", ",")) if elevation: layers = elevation.split(";") for layer in layers: newfilename = self.exportRasterLayer(layer) elevation = elevation.replace(layer, newfilename) command += (" elevation=" + elevation.replace(";", ",")) if elevation is None and vector is None: command += " -q" commands.append(command) GrassUtils.createTempMapset(); GrassUtils.executeGrass(commands, progress)
def processAlgorithm(self, progress): if SextanteUtils.isWindows(): path = GrassUtils.grassPath() if path == "": raise GeoAlgorithmExecutionException("GRASS folder is not configured.\nPlease configure it before running GRASS algorithms.") commands = [] self.exportedLayers = {} #self.calculateRegion() region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") GrassUtils.createTempMapset(); command = "g.region" command += " n=" + str(regionCoords[3]) command +=" s=" + str(regionCoords[2]) command +=" e=" + str(regionCoords[1]) command +=" w=" + str(regionCoords[0]) command +=" res=" + str(self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER)); commands.append(command) #1: Export layer to grass mapset for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value commands.append(self.exportRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue value = param.value commands.append(self.exportVectorLayer(value)) if isinstance(param, ParameterTable): pass if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layer in layers: commands.append(self.exportRasterLayer(layer)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layer in layers: commands.append(self.exportVectorLayer(layer)) #2: set parameters and outputs command = self.grassName for param in self.parameters: if param.value == None: continue if param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER: continue if isinstance(param, (ParameterRaster, ParameterVector)): value = param.value if value in self.exportedLayers.keys(): command+=(" " + param.name + "=" + self.exportedLayers[value]) else: command+=(" " + param.name + "=" + value) elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) s = s.replace(";",",") command+=(" " + param.name + "=" + s); elif isinstance(param, ParameterBoolean): if param.value: command += (" " + param.name) elif isinstance(param, ParameterSelection): idx = int(param.value) command+=(" " + param.name + "=" + str(param.options[idx])); else: command+=(" " + param.name + "=" + str(param.value)); for out in self.outputs: if isinstance(out, OutputFile): command+=(" " + out.name + "=\"" + out.value + "\""); else: command+=(" " + out.name + "=" + out.name); command += " --overwrite" commands.append(command) #3:Export resulting layers to a format that qgis can read for out in self.outputs: if isinstance(out, OutputRaster): filename = out.value #Raster layer output: adjust region to layer before exporting commands.append("g.region rast=" + out.name) command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\"" command += " input=" command += out.name command += " output=\"" + filename + "\"" commands.append(command) if isinstance(out, OutputVector): command = "v.out.ogr -ce input=" + out.name command += " dsn=\"" + os.path.dirname(out.value) + "\"" command += " format=ESRI_Shapefile" command += " olayer=" + os.path.basename(out.value)[:-4] command += " type=auto" commands.append(command) #4 Run GRASS loglines = [] loglines.append("GRASS execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) GrassUtils.executeGrass(commands, progress);
def processAlgorithm(self, progress): if SextanteUtils.isWindows(): path = GrassUtils.grassPath() if path == "": raise GeoAlgorithmExecutionException( "GRASS folder is not configured.\nPlease configure it before running GRASS algorithms." ) commands = [] self.exportedLayers = {} #self.calculateRegion() region = str(self.getParameterValue( self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") GrassUtils.createTempMapset() command = "g.region" command += " n=" + str(regionCoords[3]) command += " s=" + str(regionCoords[2]) command += " e=" + str(regionCoords[1]) command += " w=" + str(regionCoords[0]) command += " res=" + str( self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER)) commands.append(command) #1: Export layer to grass mapset for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value commands.append(self.exportRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue value = param.value commands.append(self.exportVectorLayer(value)) if isinstance(param, ParameterTable): pass if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layer in layers: commands.append(self.exportRasterLayer(layer)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layer in layers: commands.append(self.exportVectorLayer(layer)) #2: set parameters and outputs command = self.grassName for param in self.parameters: if param.value == None: continue if param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER: continue if isinstance(param, (ParameterRaster, ParameterVector)): value = param.value if value in self.exportedLayers.keys(): command += (" " + param.name + "=" + self.exportedLayers[value]) else: command += (" " + param.name + "=" + value) elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) s = s.replace(";", ",") command += (" " + param.name + "=" + s) elif isinstance(param, ParameterBoolean): if param.value: command += (" " + param.name) elif isinstance(param, ParameterSelection): idx = int(param.value) command += (" " + param.name + "=" + str(param.options[idx])) else: command += (" " + param.name + "=" + str(param.value)) for out in self.outputs: if isinstance(out, OutputFile): command += (" " + out.name + "=\"" + out.value + "\"") else: command += (" " + out.name + "=" + out.name) command += " --overwrite" commands.append(command) #3:Export resulting layers to a format that qgis can read for out in self.outputs: if isinstance(out, OutputRaster): filename = out.value #Raster layer output: adjust region to layer before exporting commands.append("g.region rast=" + out.name) command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\"" command += " input=" command += out.name command += " output=\"" + filename + "\"" commands.append(command) if isinstance(out, OutputVector): command = "v.out.ogr -ce input=" + out.name command += " dsn=\"" + os.path.dirname(out.value) + "\"" command += " format=ESRI_Shapefile" command += " olayer=" + os.path.basename(out.value)[:-4] command += " type=auto" commands.append(command) #4 Run GRASS loglines = [] loglines.append("GRASS execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) GrassUtils.executeGrass(commands, progress)
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
def processAlgorithm(self, progress): if SextanteUtils.isWindows(): path = GrassUtils.grassPath() if path == "": raise GeoAlgorithmExecutionException("GRASS folder is not configured.\nPlease configure it before running GRASS algorithms.") commands = [] self.exportedLayers = {} outputCommands = [] # if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS # otherwise start a new session existingSession = GrassUtils.sessionRunning if existingSession: self.exportedLayers = GrassUtils.getSessionLayers() else: GrassUtils.startGrassSession() #1: Export layer to grass mapset for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value # check if the layer hasn't already been exported in, for example, previous GRASS calls in this session if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue value = param.value if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportVectorLayer(value)) if isinstance(param, ParameterTable): pass if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportRasterLayer(layer)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportVectorLayer(layer)) self.setSessionProjectionFromProject(commands) region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") command = "g.region" command += " n=" + str(regionCoords[3]) command +=" s=" + str(regionCoords[2]) command +=" e=" + str(regionCoords[1]) command +=" w=" + str(regionCoords[0]) cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER) if cellsize: command +=" res=" + str(cellsize); else: command +=" res=" + str(self.getDefaultCellsize()) commands.append(command) #2: set parameters and outputs command = self.grassName for param in self.parameters: if param.value == None or param.value == "": continue if (param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER or param.name == self.GRASS_MIN_AREA_PARAMETER or param.name == self.GRASS_SNAP_TOLERANCE_PARAMETER): continue if isinstance(param, (ParameterRaster, ParameterVector)): value = param.value if value in self.exportedLayers.keys(): command+=(" " + param.name + "=" + self.exportedLayers[value]) else: command+=(" " + param.name + "=" + value) elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) s = s.replace(";",",") command+=(" " + param.name + "=" + s); elif isinstance(param, ParameterBoolean): if param.value: command += (" " + param.name) elif isinstance(param, ParameterSelection): idx = int(param.value) command+=(" " + param.name + "=" + str(param.options[idx])); elif isinstance(param, ParameterSelection): command+=(" " + param.name + "=" + str(param.value)); else: command+=(" " + param.name + "=" + str(param.value)); uniqueSufix = str(uuid.uuid4()).replace("-",""); for out in self.outputs: if isinstance(out, OutputFile): command+=(" " + out.name + "=\"" + out.value + "\""); elif not isinstance(out, OutputHTML): #html files are not generated by grass, only by sextante to decorate grass output #an output name to make sure it is unique if the session uses this algorithm several times uniqueOutputName = out.name + uniqueSufix command += (" " + out.name + "=" + uniqueOutputName) # add output file to exported layers, to indicate that they are present in GRASS self.exportedLayers[out.value]= uniqueOutputName command += " --overwrite" commands.append(command) #3:Export resulting layers to a format that qgis can read for out in self.outputs: if isinstance(out, OutputRaster): filename = out.value #Raster layer output: adjust region to layer before exporting commands.append("g.region rast=" + out.name + uniqueSufix) outputCommands.append("g.region rast=" + out.name + uniqueSufix) command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\"" command += " input=" command += out.name + uniqueSufix command += " output=\"" + filename + "\"" commands.append(command) outputCommands.append(command) if isinstance(out, OutputVector): filename = out.value command = "v.out.ogr -ce input=" + out.name + uniqueSufix command += " dsn=\"" + os.path.dirname(out.value) + "\"" command += " format=ESRI_Shapefile" command += " olayer=" + os.path.basename(out.value)[:-4] command += " type=auto" commands.append(command) outputCommands.append(command) #4 Run GRASS loglines = [] loglines.append("GRASS execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) self.consoleOutput = GrassUtils.executeGrass(commands, progress, outputCommands); self.postProcessResults(); # if the session has been created outside of this algorithm, add the new GRASS layers to it # otherwise finish the session if existingSession: GrassUtils.addSessionLayers(self.exportedLayers) else: GrassUtils.endGrassSession()
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if SextanteUtils.isWindows() or SextanteUtils.isMac(): SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_FOLDER, "GRASS folder", GrassUtils.grassPath())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell())) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False)) #SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True)) SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False)) #======================================================================= # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000)) # SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 100)) #======================================================================= SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))
def processAlgorithm(self, progress): if SextanteUtils.isWindows(): path = GrassUtils.grassPath() if path == "": raise GeoAlgorithmExecutionException( "GRASS folder is not configured.\nPlease configure it before running GRASS algorithms." ) commands = [] self.exportedLayers = {} outputCommands = [] # if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS # otherwise start a new session existingSession = GrassUtils.sessionRunning if existingSession: self.exportedLayers = GrassUtils.getSessionLayers() else: GrassUtils.startGrassSession() #1: Export layer to grass mapset for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value # check if the layer hasn't already been exported in, for example, previous GRASS calls in this session if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue value = param.value if value in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(value, commands) commands.append(self.exportVectorLayer(value)) if isinstance(param, ParameterTable): pass if isinstance(param, ParameterMultipleInput): if param.value == None: continue layers = param.value.split(";") if layers == None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportRasterLayer(layer)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layer in layers: if layer in self.exportedLayers.keys(): continue else: self.setSessionProjectionFromLayer(layer, commands) commands.append(self.exportVectorLayer(layer)) self.setSessionProjectionFromProject(commands) region = str(self.getParameterValue( self.GRASS_REGION_EXTENT_PARAMETER)) regionCoords = region.split(",") command = "g.region" command += " n=" + str(regionCoords[3]) command += " s=" + str(regionCoords[2]) command += " e=" + str(regionCoords[1]) command += " w=" + str(regionCoords[0]) cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER) if cellsize: command += " res=" + str(cellsize) else: command += " res=" + str(self.getDefaultCellsize()) commands.append(command) #2: set parameters and outputs command = self.grassName for param in self.parameters: if param.value == None or param.value == "": continue if (param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER or param.name == self.GRASS_MIN_AREA_PARAMETER or param.name == self.GRASS_SNAP_TOLERANCE_PARAMETER): continue if isinstance(param, (ParameterRaster, ParameterVector)): value = param.value if value in self.exportedLayers.keys(): command += (" " + param.name + "=" + self.exportedLayers[value]) else: command += (" " + param.name + "=" + value) elif isinstance(param, ParameterMultipleInput): s = param.value for layer in self.exportedLayers.keys(): s = s.replace(layer, self.exportedLayers[layer]) s = s.replace(";", ",") command += (" " + param.name + "=" + s) elif isinstance(param, ParameterBoolean): if param.value: command += (" " + param.name) elif isinstance(param, ParameterSelection): idx = int(param.value) command += (" " + param.name + "=" + str(param.options[idx])) elif isinstance(param, ParameterString): command += (" " + param.name + "=\"" + str(param.value) + "\"") else: command += (" " + param.name + "=" + str(param.value)) uniqueSufix = str(uuid.uuid4()).replace("-", "") for out in self.outputs: if isinstance(out, OutputFile): command += (" " + out.name + "=\"" + out.value + "\"") elif not isinstance( out, OutputHTML ): #html files are not generated by grass, only by sextante to decorate grass output #an output name to make sure it is unique if the session uses this algorithm several times uniqueOutputName = out.name + uniqueSufix command += (" " + out.name + "=" + uniqueOutputName) # add output file to exported layers, to indicate that they are present in GRASS self.exportedLayers[out.value] = uniqueOutputName command += " --overwrite" commands.append(command) #3:Export resulting layers to a format that qgis can read for out in self.outputs: if isinstance(out, OutputRaster): filename = out.value #Raster layer output: adjust region to layer before exporting commands.append("g.region rast=" + out.name + uniqueSufix) outputCommands.append("g.region rast=" + out.name + uniqueSufix) command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\"" command += " input=" command += out.name + uniqueSufix command += " output=\"" + filename + "\"" commands.append(command) outputCommands.append(command) if isinstance(out, OutputVector): filename = out.value command = "v.out.ogr -ce input=" + out.name + uniqueSufix command += " dsn=\"" + os.path.dirname(out.value) + "\"" command += " format=ESRI_Shapefile" command += " olayer=" + os.path.basename(out.value)[:-4] command += " type=auto" commands.append(command) outputCommands.append(command) #4 Run GRASS loglines = [] loglines.append("GRASS execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) self.consoleOutput = GrassUtils.executeGrass(commands, progress, outputCommands) self.postProcessResults() # if the session has been created outside of this algorithm, add the new GRASS layers to it # otherwise finish the session if existingSession: GrassUtils.addSessionLayers(self.exportedLayers) else: GrassUtils.endGrassSession()