def processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = getTempFilename(None).replace('.',''); basename = os.path.basename(temp) validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" safeBasename = ''.join(c for c in basename if c in validChars) temp = os.path.join(os.path.dirname(temp), safeBasename) r = self.getOutputValue(SplitRGBBands.R) g = self.getOutputValue(SplitRGBBands.G) b = self.getOutputValue(SplitRGBBands.B) commands = [] if isWindows(): commands.append("io_gdal 0 -GRIDS \"" + temp + "\" -FILES \"" + input+"\"") commands.append("io_gdal 1 -GRIDS \"" + temp + "_0001.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + r + "\""); commands.append("io_gdal 1 -GRIDS \"" + temp + "_0002.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + g + "\""); commands.append("io_gdal 1 -GRIDS \"" + temp + "_0003.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + b + "\""); else: commands.append("libio_gdal 0 -GRIDS \"" + temp + "\" -FILES \"" + input + "\"") commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0001.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + r + "\""); commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0002.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + g + "\""); commands.append("libio_gdal 1 -GRIDS \"" + temp + "_0003.sgrd\" -FORMAT 1 -TYPE 0 -FILE \"" + b + "\""); SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) SagaUtils.executeSaga(progress);
def checkBeforeOpeningParametersDialog(self): msg = SagaUtils.checkSagaIsInstalled() if msg is not None: html = ("<p>This algorithm requires SAGA to be run." "Unfortunately, it seems that SAGA 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/2.0/html/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure SAGA to be used with QGIS</p>' return html
def createAlgsList(self): self.preloadedAlgs = [] saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) folder = SagaUtils.sagaDescriptionPath() for descriptionFile in os.listdir(folder): if descriptionFile.endswith("txt"): if not saga208: if descriptionFile.startswith("2.0.8"): continue else: if descriptionFile.startswith("2.1"): continue try: alg = SagaAlgorithm(os.path.join(folder, descriptionFile)) if alg.name.strip() != "": self.preloadedAlgs.append(alg) else: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, "Could not open SAGA algorithm: " + descriptionFile) except Exception, e: ProcessingLog.addToLog( ProcessingLog.LOG_ERROR, "Could not open SAGA algorithm: " + descriptionFile + "\n" + str(e))
def checkBeforeOpeningParametersDialog(self): msg = SagaUtils.checkSagaIsInstalled() if msg is not None: html = ( "<p>This algorithm requires SAGA to be run." "Unfortunately, it seems that SAGA 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/2.0/html/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure SAGA to be used with QGIS</p>' return html
def processAlgorithm(self, progress): # TODO: check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = getTempFilename(None).replace('.', '') basename = os.path.basename(temp) validChars = \ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' safeBasename = ''.join(c for c in basename if c in validChars) temp = os.path.join(os.path.dirname(temp), safeBasename) r = self.getOutputValue(SplitRGBBands.R) g = self.getOutputValue(SplitRGBBands.G) b = self.getOutputValue(SplitRGBBands.B) commands = [] if isWindows(): commands.append('io_gdal 0 -GRIDS "' + temp + '" -FILES "' + input + '"') commands.append('io_gdal 1 -GRIDS "' + temp + '_0001.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + r + '"' ) commands.append('io_gdal 1 -GRIDS "' + temp + '_0002.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + g + '"' ) commands.append('io_gdal 1 -GRIDS "' + temp + '_0003.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + b + '"' ) else: commands.append('libio_gdal 0 -GRIDS "' + temp + '" -FILES "' + input + '"') commands.append('libio_gdal 1 -GRIDS "' + temp + '_0001.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + r + '"' ) commands.append('libio_gdal 1 -GRIDS "' + temp + '_0002.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + g + '"' ) commands.append('libio_gdal 1 -GRIDS "' + temp + '_0003.sgrd" -FORMAT 1 -TYPE 0 -FILE "' + b + '"' ) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) SagaUtils.executeSaga(progress)
def getPostProcessingErrorMessage(self, wrongLayers): html = GeoAlgorithm.getPostProcessingErrorMessage(self, wrongLayers) msg = SagaUtils.checkSagaIsInstalled(True) html += ("<p>This algorithm requires SAGA to be run. A test to check if SAGA is correctly installed " "and configured in your system has been performed, with the following result:</p><ul><i>") if msg is None: html += "SAGA 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/processing/3rdParty.html">Click here</a> to know more about how to install and configure SAGA to be used with QGIS</p>' return html
def getPostProcessingErrorMessage(self, wrongLayers): html = GeoAlgorithm.getPostProcessingErrorMessage(self, wrongLayers) msg = SagaUtils.checkSagaIsInstalled(True) html += ( "<p>This algorithm requires SAGA to be run. A test to check if SAGA is correctly installed " "and configured in your system has been performed, with the following result:</p><ul><i>" ) if msg is None: html += "SAGA 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/processing/3rdParty.html">Click here</a> to know more about how to install and configure SAGA to be used with QGIS</p>' return html
def _loadAlgorithms(self): self.algs = [] saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) folder = SagaUtils.sagaDescriptionPath() for descriptionFile in os.listdir(folder): if descriptionFile.endswith("txt"): if not saga208: if descriptionFile.startswith("2.0.8"): continue else: if descriptionFile.startswith("2.1"): continue try: alg = SagaAlgorithm(os.path.join(folder, descriptionFile)) if alg.name.strip() != "": self.algs.append(alg) else: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Could not open SAGA algorithm: " + descriptionFile) except Exception,e: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, "Could not open SAGA algorithm: " + descriptionFile +"\n" + str(e))
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if ProcessingUtils.isWindows(): ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath())) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_208, "Enable SAGA 2.0.8 compatibility", False)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_LOG_COMMANDS, "Log execution commands", True)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_LOG_CONSOLE, "Log console output", True)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000)) ProcessingConfig.addSetting( Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if isWindows(): ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, 'SAGA folder', SagaUtils.sagaPath())) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_208, 'Enable SAGA 2.0.8 compatibility', True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION, 'Enable SAGA Import/Export optimizations', False)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, 'Use min covering grid system for resampling', True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_COMMANDS, 'Log execution commands', True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_CONSOLE, 'Log console output', True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMIN, 'Resampling region min x', 0)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMIN, 'Resampling region min y', 0)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMAX, 'Resampling region max x', 1000)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMAX, 'Resampling region max y', 1000)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, 'Resampling region cellsize', 1))
def _loadAlgorithms(self): self.algs = [] saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) folder = SagaUtils.sagaDescriptionPath() for descriptionFile in os.listdir(folder): if descriptionFile.endswith('txt'): if not saga208: if descriptionFile.startswith('2.0.8'): continue else: if descriptionFile.startswith('2.1'): continue try: alg = SagaAlgorithm(os.path.join(folder, descriptionFile)) if alg.name.strip() != '': self.algs.append(alg) else: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, 'Could not open SAGA algorithm: ' + descriptionFile) except Exception, e: ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, 'Could not open SAGA algorithm: ' + descriptionFile + '\n' + str(e))
def processAlgorithm(self, progress): if isWindows(): path = SagaUtils.sagaPath() if path == "": raise GeoAlgorithmExecutionException("SAGA folder is not configured.\nPlease configure it before running SAGA algorithms.") commands = list() self.exportedLayers = {} self.preProcessInputs() #1: Export rasters to sgrd and vectors to shp # Tables must be in dbf format. We check that. if self.resample: self.calculateResamplingExtent() for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value if not value.endswith("sgrd"): commands.append(self.exportRasterLayer(value)) if self.resample: commands.append(self.resampleRasterLayer(value)); if isinstance(param, ParameterVector): if param.value == None: continue layer = dataobjects.getObjectFromUri(param.value, False) if layer: filename = dataobjects.exportVectorLayer(layer) self.exportedLayers[param.value]=filename elif not param.value.endswith("shp"): raise GeoAlgorithmExecutionException("Unsupported file format") if isinstance(param, ParameterTable): if param.value == None: continue table = dataobjects.getObjectFromUri(param.value, False) if table: filename = dataobjects.exportTable(table) self.exportedLayers[param.value]=filename elif not param.value.endswith("shp"): raise GeoAlgorithmExecutionException("Unsupported file format") 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 layerfile in layers: if not layerfile.endswith("sgrd"): commands.append(self.exportRasterLayer(layerfile)) if self.resample: commands.append(self.resampleRasterLayer(layerfile)); elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layerfile in layers: layer = dataobjects.getObjectFromUri(layerfile, False) if layer: filename = dataobjects.exportVectorLayer(layer) self.exportedLayers[layerfile]=filename elif (not layerfile.endswith("shp")): raise GeoAlgorithmExecutionException("Unsupported file format") #2: set parameters and outputs saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if isWindows() or isMac() or not saga208: command = self.undecoratedGroup + " \"" + self.cmdname + "\"" else: command = "lib" + self.undecoratedGroup + " \"" + self.cmdname + "\"" if self.hardcodedStrings: for s in self.hardcodedStrings: command += " " + s for param in self.parameters: if param.value is None: continue if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)): 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]) command += (" -" + param.name + " \"" + s + "\""); elif isinstance(param, ParameterBoolean): if param.value: command+=(" -" + param.name); elif isinstance(param, ParameterFixedTable): tempTableFile = getTempFilename("txt") f = open(tempTableFile, "w") f.write('\t'.join([col for col in param.cols]) + "\n") values = param.value.split(",") for i in range(0, len(values), 3): s = values[i] + "\t" + values[i+1] + "\t" + values[i+2] + "\n" f.write(s) f.close() command+=( " -" + param.name + " \"" + tempTableFile + "\"") elif isinstance(param, ParameterExtent): #'we have to substract/add half cell size, since saga is center based, not corner based halfcell = self.getOutputCellsize() / 2 offset = [halfcell, -halfcell, halfcell, -halfcell] values = param.value.split(",") for i in range(4): command+=(" -" + self.extentParamNames[i] + " " + str(float(values[i]) + offset[i])); elif isinstance(param, (ParameterNumber, ParameterSelection)): command+=(" -" + param.name + " " + str(param.value)); else: command+=(" -" + param.name + " \"" + str(param.value) + "\""); for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename += ".sgrd" command+=(" -" + out.name + " \"" + filename + "\""); if isinstance(out, OutputVector): filename = out.getCompatibleFileName(self) command+=(" -" + out.name + " \"" + filename + "\""); if isinstance(out, OutputTable): filename = out.getCompatibleFileName(self) command+=(" -" + out.name + " \"" + filename + "\""); commands.append(command) #3:Export resulting raster layers for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = filename + ".sgrd" formatIndex = 1 if saga208 else 4 if isWindows() or isMac() or not saga208: commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT " + str(formatIndex) +" -TYPE 0 -FILE \"" + filename + "\""); else: commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\""); #4 Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append("SAGA execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress);
def initializeSettings(self): AlgorithmProvider.initializeSettings(self) if ProcessingUtils.isWindows(): ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_FOLDER, "SAGA folder", SagaUtils.sagaPath())) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_208, "Enable SAGA 2.0.8 compatibility", False)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_AUTO_RESAMPLING, "Use min covering grid system for resampling", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_COMMANDS, "Log execution commands", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_LOG_CONSOLE, "Log console output", True)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMIN, "Resampling region min x", 0)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMIN, "Resampling region min y", 0)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000)) ProcessingConfig.addSetting(Setting(self.getDescription(), SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
def processAlgorithm(self, progress): if ProcessingUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": raise GeoAlgorithmExecutionException( "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms." ) commands = list() self.exportedLayers = {} self.preProcessInputs() #1: Export rasters to sgrd and vectors to shp # Tables must be in dbf format. We check that. if self.resample: self.calculateResamplingExtent() for param in self.parameters: if isinstance(param, ParameterRaster): if param.value == None: continue value = param.value if not value.endswith("sgrd"): commands.append(self.exportRasterLayer(value)) if self.resample: commands.append(self.resampleRasterLayer(value)) if isinstance(param, ParameterVector): if param.value == None: continue layer = QGisLayers.getObjectFromUri(param.value, False) if layer: filename = LayerExporter.exportVectorLayer(layer) self.exportedLayers[param.value] = filename elif not param.value.endswith("shp"): raise GeoAlgorithmExecutionException( "Unsupported file format") if isinstance(param, ParameterTable): if param.value == None: continue table = QGisLayers.getObjectFromUri(param.value, False) if table: filename = LayerExporter.exportTable(table) self.exportedLayers[param.value] = filename elif not param.value.endswith("shp"): raise GeoAlgorithmExecutionException( "Unsupported file format") 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 layerfile in layers: if not layerfile.endswith("sgrd"): commands.append(self.exportRasterLayer(layerfile)) if self.resample: commands.append( self.resampleRasterLayer(layerfile)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layerfile in layers: layer = QGisLayers.getObjectFromUri(layerfile, False) if layer: filename = LayerExporter.exportVectorLayer(layer) self.exportedLayers[layerfile] = filename elif (not layerfile.endswith("shp")): raise GeoAlgorithmExecutionException( "Unsupported file format") #2: set parameters and outputs saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: command = self.undecoratedGroup + " \"" + self.cmdname + "\"" else: command = "lib" + self.undecoratedGroup + " \"" + self.cmdname + "\"" if self.hardcodedStrings: for s in self.hardcodedStrings: command += " " + s for param in self.parameters: if param.value is None: continue if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)): 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]) command += (" -" + param.name + " \"" + s + "\"") elif isinstance(param, ParameterBoolean): if param.value: command += (" -" + param.name) elif isinstance(param, ParameterFixedTable): tempTableFile = ProcessingUtils.getTempFilename("txt") f = open(tempTableFile, "w") f.write('\t'.join([col for col in param.cols]) + "\n") values = param.value.split(",") for i in range(0, len(values), 3): s = values[i] + "\t" + values[i + 1] + "\t" + values[i + 2] + "\n" f.write(s) f.close() command += (" -" + param.name + " \"" + tempTableFile + "\"") elif isinstance(param, ParameterExtent): #'we have to substract/add half cell size, since saga is center based, not corner based halfcell = self.getOutputCellsize() / 2 offset = [halfcell, -halfcell, halfcell, -halfcell] values = param.value.split(",") for i in range(4): command += (" -" + self.extentParamNames[i] + " " + str(float(values[i]) + offset[i])) elif isinstance(param, (ParameterNumber, ParameterSelection)): command += (" -" + param.name + " " + str(param.value)) else: command += (" -" + param.name + " \"" + str(param.value) + "\"") for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename = ProcessingUtils.tempFolder( ) + os.sep + os.path.basename(filename) + ".sgrd" command += (" -" + out.name + " \"" + filename + "\"") if isinstance(out, OutputVector): filename = out.getCompatibleFileName(self) command += (" -" + out.name + " \"" + filename + "\"") if isinstance(out, OutputTable): filename = out.getCompatibleFileName(self) command += (" -" + out.name + " \"" + filename + "\"") commands.append(command) #3:Export resulting raster layers for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = ProcessingUtils.tempFolder( ) + os.sep + os.path.basename(filename) + ".sgrd" formatIndex = 1 if saga208 else 4 if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT " + str(formatIndex) + " -TYPE 0 -FILE \"" + filename + "\"") else: commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"") #4 Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append("SAGA execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress)
def processAlgorithm(self, progress): if isWindows(): path = SagaUtils.sagaPath() if path == '': raise GeoAlgorithmExecutionException( 'SAGA folder is not configured.\nPlease configure \ it before running SAGA algorithms.') commands = list() self.exportedLayers = {} self.preProcessInputs() # 1: Export rasters to sgrd and vectors to shp # Tables must be in dbf format. We check that. if self.resample: self.calculateResamplingExtent() for param in self.parameters: if isinstance(param, ParameterRaster): if param.value is None: continue value = param.value if not value.endswith('sgrd'): exportCommand = self.exportRasterLayer(value) if exportCommand is not None: commands.append(exportCommand) if self.resample: commands.append(self.resampleRasterLayer(value)) if isinstance(param, ParameterVector): if param.value is None: continue layer = dataobjects.getObjectFromUri(param.value, False) if layer: filename = dataobjects.exportVectorLayer(layer) self.exportedLayers[param.value] = filename elif not param.value.endswith('shp'): raise GeoAlgorithmExecutionException( 'Unsupported file format') if isinstance(param, ParameterTable): if param.value is None: continue table = dataobjects.getObjectFromUri(param.value, False) if table: filename = dataobjects.exportTable(table) self.exportedLayers[param.value] = filename elif not param.value.endswith('shp'): raise GeoAlgorithmExecutionException( 'Unsupported file format') if isinstance(param, ParameterMultipleInput): if param.value is None: continue layers = param.value.split(';') if layers is None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layerfile in layers: if not layerfile.endswith('sgrd'): exportCommand = self.exportRasterLayer(layerfile) if exportCommand is not None: commands.append(exportCommand) if self.resample: commands.append( self.resampleRasterLayer(layerfile)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layerfile in layers: layer = dataobjects.getObjectFromUri(layerfile, False) if layer: filename = dataobjects.exportVectorLayer(layer) self.exportedLayers[layerfile] = filename elif not layerfile.endswith('shp'): raise GeoAlgorithmExecutionException( 'Unsupported file format') # 2: Set parameters and outputs saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if isWindows() or isMac() or not saga208: command = self.undecoratedGroup + ' "' + self.cmdname + '"' else: command = 'lib' + self.undecoratedGroup + ' "' + self.cmdname + '"' if self.hardcodedStrings: for s in self.hardcodedStrings: command += ' ' + s for param in self.parameters: if param.value is None: continue if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)): 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]) command += ' -' + param.name + ' "' + s + '"' elif isinstance(param, ParameterBoolean): if param.value: command += ' -' + param.name elif isinstance(param, ParameterFixedTable): tempTableFile = getTempFilename('txt') f = open(tempTableFile, 'w') f.write('\t'.join([col for col in param.cols]) + '\n') values = param.value.split(',') for i in range(0, len(values), 3): s = values[i] + '\t' + values[i + 1] + '\t' + values[i + 2] + '\n' f.write(s) f.close() command += ' -' + param.name + ' "' + tempTableFile + '"' elif isinstance(param, ParameterExtent): # 'We have to substract/add half cell size, since SAGA is # center based, not corner based halfcell = self.getOutputCellsize() / 2 offset = [halfcell, -halfcell, halfcell, -halfcell] values = param.value.split(',') for i in range(4): command += ' -' + self.extentParamNames[i] + ' ' \ + str(float(values[i]) + offset[i]) elif isinstance(param, (ParameterNumber, ParameterSelection)): command += ' -' + param.name + ' ' + str(param.value) else: command += ' -' + param.name + ' "' + str(param.value) + '"' for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename += '.sgrd' command += ' -' + out.name + ' "' + filename + '"' if isinstance(out, OutputVector): filename = out.getCompatibleFileName(self) command += ' -' + out.name + ' "' + filename + '"' if isinstance(out, OutputTable): filename = out.getCompatibleFileName(self) command += ' -' + out.name + ' "' + filename + '"' commands.append(command) # 3: Export resulting raster layers optim = ProcessingConfig.getSetting( SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION) for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = filename + '.sgrd' formatIndex = (4 if not saga208 and isWindows() else 1) sessionExportedLayers[filename] = filename2 dontExport = True # Do not export is the output is not a final output # of the model if self.model is not None and optim: for subalg in self.model.algOutputs: if out.name in subalg: if subalg[out.name] is not None: dontExport = False break if dontExport: continue transform = ('' if saga208 else '-TRANSFORM') if isWindows() or isMac() or not saga208: commands.append('io_gdal 1 -GRIDS "' + filename2 + '" -FORMAT ' + str(formatIndex) + ' -TYPE 0 -FILE "' + filename + '"' + transform) else: commands.append('libio_gdal 1 -GRIDS "' + filename2 + '" -FORMAT 1 -TYPE 0 -FILE "' + filename + '"' + transform) # 4: Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append('SAGA execution commands') for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress)
def processAlgorithm(self, progress): if isWindows(): path = SagaUtils.sagaPath() if path == '': raise GeoAlgorithmExecutionException( 'SAGA folder is not configured.\nPlease configure \ it before running SAGA algorithms.') commands = list() self.exportedLayers = {} self.preProcessInputs() # 1: Export rasters to sgrd and vectors to shp # Tables must be in dbf format. We check that. if self.resample: self.calculateResamplingExtent() for param in self.parameters: if isinstance(param, ParameterRaster): if param.value is None: continue value = param.value if not value.endswith('sgrd'): exportCommand = self.exportRasterLayer(value) if exportCommand is not None: commands.append(exportCommand) if self.resample: commands.append(self.resampleRasterLayer(value)) if isinstance(param, ParameterVector): if param.value is None: continue layer = dataobjects.getObjectFromUri(param.value, False) if layer: filename = dataobjects.exportVectorLayer(layer) self.exportedLayers[param.value] = filename elif not param.value.endswith('shp'): raise GeoAlgorithmExecutionException( 'Unsupported file format') if isinstance(param, ParameterTable): if param.value is None: continue table = dataobjects.getObjectFromUri(param.value, False) if table: filename = dataobjects.exportTable(table) self.exportedLayers[param.value] = filename elif not param.value.endswith('shp'): raise GeoAlgorithmExecutionException( 'Unsupported file format') if isinstance(param, ParameterMultipleInput): if param.value is None: continue layers = param.value.split(';') if layers is None or len(layers) == 0: continue if param.datatype == ParameterMultipleInput.TYPE_RASTER: for layerfile in layers: if not layerfile.endswith('sgrd'): exportCommand = self.exportRasterLayer(layerfile) if exportCommand is not None: commands.append(exportCommand) if self.resample: commands.append( self.resampleRasterLayer(layerfile)) elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY: for layerfile in layers: layer = dataobjects.getObjectFromUri(layerfile, False) if layer: filename = dataobjects.exportVectorLayer(layer) self.exportedLayers[layerfile] = filename elif not layerfile.endswith('shp'): raise GeoAlgorithmExecutionException( 'Unsupported file format') # 2: Set parameters and outputs saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if isWindows() or isMac() or not saga208: command = self.undecoratedGroup + ' "' + self.cmdname + '"' else: command = 'lib' + self.undecoratedGroup + ' "' + self.cmdname + '"' if self.hardcodedStrings: for s in self.hardcodedStrings: command += ' ' + s for param in self.parameters: if param.value is None: continue if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)): 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]) command += ' -' + param.name + ' "' + s + '"' elif isinstance(param, ParameterBoolean): if param.value: command += ' -' + param.name elif isinstance(param, ParameterFixedTable): tempTableFile = getTempFilename('txt') f = open(tempTableFile, 'w') f.write('\t'.join([col for col in param.cols]) + '\n') values = param.value.split(',') for i in range(0, len(values), 3): s = values[i] + '\t' + values[i + 1] + '\t' + values[i + 2] + '\n' f.write(s) f.close() command += ' -' + param.name + ' "' + tempTableFile + '"' elif isinstance(param, ParameterExtent): # 'We have to substract/add half cell size, since SAGA is # center based, not corner based halfcell = self.getOutputCellsize() / 2 offset = [halfcell, -halfcell, halfcell, -halfcell] values = param.value.split(',') for i in range(4): command += ' -' + self.extentParamNames[i] + ' ' \ + str(float(values[i]) + offset[i]) elif isinstance(param, (ParameterNumber, ParameterSelection)): command += ' -' + param.name + ' ' + str(param.value) else: command += ' -' + param.name + ' "' + str(param.value) + '"' for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename += '.sgrd' command += ' -' + out.name + ' "' + filename + '"' if isinstance(out, OutputVector): filename = out.getCompatibleFileName(self) command += ' -' + out.name + ' "' + filename + '"' if isinstance(out, OutputTable): filename = out.getCompatibleFileName(self) command += ' -' + out.name + ' "' + filename + '"' commands.append(command) # 3: Export resulting raster layers optim = ProcessingConfig.getSetting( SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION) for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = filename + '.sgrd' formatIndex = (4 if not saga208 and isWindows() else 1) sessionExportedLayers[filename] = filename2 dontExport = True # Do not export is the output is not a final output # of the model if self.model is not None and optim: for subalg in self.model.algOutputs: if out.name in subalg: if subalg[out.name] is not None: dontExport = False break if dontExport: continue if self.cmdname == 'RGB Composite': if isWindows() or isMac() or not saga208: commands.append('io_grid_image 0 -IS_RGB -GRID:"' + filename2 + '" -FILE:"' + filename + '"') else: commands.append('libio_grid_image 0 -IS_RGB -GRID:"' + filename2 + '" -FILE:"' + filename + '"') else: if isWindows() or isMac() or not saga208: commands.append('io_gdal 1 -GRIDS "' + filename2 + '" -FORMAT ' + str(formatIndex) + ' -TYPE 0 -FILE "' + filename + '"') else: commands.append('libio_gdal 1 -GRIDS "' + filename2 + '" -FORMAT 1 -TYPE 0 -FILE "' + filename + '"') # 4: Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append('SAGA execution commands') for line in commands: progress.setCommand(line) loglines.append(line) if ProcessingConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress)