def exportTable( table): '''Takes a QgsVectorLayer and returns the filename to refer to its attributes table, which allows external apps which support only file-based layers to use it. It performs the necessary export in case the input layer is not in a standard format suitable for most applications, it isa remote one or db-based (non-file based) one Currently, the output is restricted to dbf. It also export to a new file if the original one contains non-ascii characters''' settings = QSettings() systemEncoding = settings.value( "/UI/encoding", "System" ) output = ProcessingUtils.getTempFilename("dbf") provider = table.dataProvider() isASCII=True try: unicode(table.source()).decode("ascii") except UnicodeEncodeError: isASCII=False isDbf = unicode(table.source()).endswith("dbf") or unicode(table.source()).endswith("shp") if (not isDbf or not isASCII): writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), QGis.WKBNoGeometry, layer.crs() ) for feat in table.getFeatures(): writer.addFeature(feat) del writer return output else: filename = unicode(table.source()) if unicode(table.source()).endswith("shp"): return filename[:-3] + "dbf" else: return filename
def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self): '''this tests both the exporting to shp and then the format change in the output layer''' layer = processing.getobject(polygonsGeoJson()); feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) outputs=processing.runalg("saga:polygoncentroids",polygonsGeoJson(),True, ProcessingUtils.getTempFilename("geojson")) layer.setSelectedFeatures([]) output=outputs['CENTROIDS'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A'] expectedtypes=['Real','Real','String'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(1, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["0","1.1","string a"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POINT(270787.49991451 4458955.46775295)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self): outputs=processing.runalg("saga:convergenceindex",raster(),0,0,ProcessingUtils.getTempFilename("img")) output=outputs['RESULT'] self.assertTrue(os.path.isfile(output)) dataset=gdal.Open(output, GA_ReadOnly) strhash=hash(str(dataset.ReadAsArray(0).tolist())) self.assertEqual(strhash, 485390137)
def test_gdalogrsieveWithUnsupportedOutputFormat(self): outputs=processing.runalg("gdalogr:sieve",raster(),2,0, ProcessingUtils.getTempFilename("img")) output=outputs['dst_filename'] self.assertTrue(os.path.isfile(output)) dataset=gdal.Open(output, GA_ReadOnly) strhash=hash(str(dataset.ReadAsArray(0).tolist())) self.assertEqual(strhash,-1353696889)
def processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = ProcessingUtils.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 ProcessingUtils.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 getCompatibleFileName(self, alg): '''Returns a filename that is compatible with the algorithm that is going to generate this output. If the algorithm supports the file format of the current output value, it returns that value. If not, it returns a temporary file with a supported file format, to be used to generate the output result.''' ext = self.value[self.value.rfind(".") + 1:] if ext in alg.provider.getSupportedOutputRasterLayerExtensions(): return self.value else: if self.compatible is None: self.compatible = ProcessingUtils.getTempFilename(self.getDefaultFileExtension(alg)) return self.compatible;
def getCompatibleFileName(self, alg): '''Returns a filename that is compatible with the algorithm that is going to generate this output. If the algorithm supports the file format of the current output value, it returns that value. If not, it returns a temporary file with a supported file format, to be used to generate the output result.''' ext = self.value[self.value.rfind(".") + 1:] if ext in alg.provider.getSupportedOutputVectorLayerExtensions(): return self.value else: if self.compatible is None: self.compatible = ProcessingUtils.getTempFilename( self.getDefaultFileExtension(alg)) return self.compatible
def __init__(self, alg, iterParam=None, parent=None): QThread.__init__(self, parent) self.algorithm = alg self.parameterToIterate = iterParam class Progress: def __init__(self, algex): self.algorithmExecutor = algex def setText(self, text): self.algorithmExecutor.textChanged.emit(text) def setPercentage(self, p): self.algorithmExecutor.percentageChanged.emit(p) def setInfo(self, info): self.algorithmExecutor.infoSet.emit(info) def setCommand(self, cmd): self.algorithmExecutor.commandSet.emit(cmd) def setDebugInfo(self, info): self.algorithmExecutor.debugInfoSet.emit(info) def setConsoleInfo(self, info): self.algorithmExecutor.consoleInfoSet.emit(info) self.progress = Progress(self) if self.parameterToIterate: self.run = self.runalgIterating #generate all single-feature layers settings = QSettings() systemEncoding = settings.value("/UI/encoding", "System") layerfile = alg.getParameterValue(self.parameterToIterate) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() features = getfeatures(layer) self.filelist = [] for feat in features: output = ProcessingUtils.getTempFilename("shp") self.filelist.append(output) writer = QgsVectorFileWriter(output, systemEncoding, provider.fields(), provider.geometryType(), layer.crs()) writer.addFeature(feat) del writer else: self.run = self.runalg self.internalError.connect(self.raiseInternalError)
def createSummaryTable(self): createTable = False for out in self.algs[0].outputs: if isinstance(out, (OutputNumber,OutputString)): createTable = True break if not createTable: return outputFile = ProcessingUtils.getTempFilename("html") f = open(outputFile, "w") for alg in self.algs: f.write("<hr>\n") for out in alg.outputs: if isinstance(out, (OutputNumber,OutputString)): f.write("<p>" + out.description + ": " + str(out.value) + "</p>\n") f.write("<hr>\n") f.close() ProcessingResults.addResult(self.algs[0].name + "[summary]", outputFile)
def resampleRasterLayer(self,layer): '''this is supposed to be run after having exported all raster layers''' if layer in self.exportedLayers.keys(): inputFilename = self.exportedLayers[layer] else: inputFilename = layer destFilename = ProcessingUtils.getTempFilename("sgrd") self.exportedLayers[layer]= destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac() or not saga208: s = "grid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" else: s = "libgrid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" return s
def runalgIterating(alg, paramToIter, progress): #generate all single-feature layers settings = QSettings() systemEncoding = settings.value("/UI/encoding", "System") layerfile = alg.getParameterValue(paramToIter) layer = QGisLayers.getObjectFromUri(layerfile, False) feat = QgsFeature() filelist = [] outputs = {} provider = layer.dataProvider() features = getfeatures(layer) for feat in features: output = ProcessingUtils.getTempFilename("shp") filelist.append(output) writer = QgsVectorFileWriter(output, systemEncoding, provider.fields(), provider.geometryType(), layer.crs()) writer.addFeature(feat) del writer #store output values to use them later as basenames for all outputs for out in alg.outputs: outputs[out.name] = out.value #now run all the algorithms i = 1 for f in filelist: alg.setParameterValue(paramToIter, f) for out in alg.outputs: filename = outputs[out.name] if filename: filename = filename[:filename.rfind(".")] + "_" + str( i) + filename[filename.rfind("."):] out.value = filename progress.setText("Executing iteration " + str(i) + "/" + str(len(filelist)) + "...") progress.setPercentage((i * 100) / len(filelist)) if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()): Postprocessing.handleAlgorithmResults(alg, progress, False) i += 1 else: return False return True
def resampleRasterLayer(self, layer): '''this is supposed to be run after having exported all raster layers''' if layer in self.exportedLayers.keys(): inputFilename = self.exportedLayers[layer] else: inputFilename = layer destFilename = ProcessingUtils.getTempFilename("sgrd") self.exportedLayers[layer] = destFilename saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208) if ProcessingUtils.isWindows() or ProcessingUtils.isMac( ) or not saga208: s = "grid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" else: s = "libgrid_tools \"Resampling\" -INPUT \"" + inputFilename + "\" -TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\ str(self.xmin) + " -USER_XMAX " + str(self.xmax) + " -USER_YMIN " + str(self.ymin) + " -USER_YMAX " + str(self.ymax) +\ " -USER_SIZE " + str(self.cellsize) + " -USER_GRID \"" + destFilename + "\"" return s
def runalgIterating(alg, paramToIter, progress): # generate all single-feature layers settings = QSettings() systemEncoding = settings.value("/UI/encoding", "System") layerfile = alg.getParameterValue(paramToIter) layer = QGisLayers.getObjectFromUri(layerfile, False) feat = QgsFeature() filelist = [] outputs = {} provider = layer.dataProvider() features = getfeatures(layer) for feat in features: output = ProcessingUtils.getTempFilename("shp") filelist.append(output) writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), layer.crs() ) writer.addFeature(feat) del writer # store output values to use them later as basenames for all outputs for out in alg.outputs: outputs[out.name] = out.value # now run all the algorithms i = 1 for f in filelist: alg.setParameterValue(paramToIter, f) for out in alg.outputs: filename = outputs[out.name] if filename: filename = filename[: filename.rfind(".")] + "_" + str(i) + filename[filename.rfind(".") :] out.value = filename progress.setText("Executing iteration " + str(i) + "/" + str(len(filelist)) + "...") progress.setPercentage((i * 100) / len(filelist)) if UnthreadedAlgorithmExecutor.runalg(alg, SilentProgress()): Postprocessing.handleAlgorithmResults(alg, progress, False) i += 1 else: return False return True
def prepare_upload_bundle(name, data): """GeoServer's REST API uses ZIP archives as containers for file formats such as Shapefile and WorldImage which include several 'boxcar' files alongside the main data. In such archives, GeoServer assumes that all of the relevant files will have the same base name and appropriate extensions, and live in the root of the ZIP archive. This method produces a zip file that matches these expectations, based on a basename, and a dict of extensions to paths or file-like objects. The client code is responsible for deleting the zip archive when it's done.""" #we ut the zip file in the processing temp dir, so it is deleted at the end. f = ProcessingUtils.getTempFilename('zip') zip_file = ZipFile(f, 'w') for ext, stream in data.iteritems(): fname = "%s.%s" % (name, ext) if (isinstance(stream, basestring)): zip_file.write(stream, fname) else: zip_file.writestr(fname, stream.read()) zip_file.close() return f
def test_gdalogrogr2ogrWrongExtension(self): outputs=processing.runalg("gdalogr:ogr2ogr",union(),3,"",ProcessingUtils.getTempFilename("wrongext")) output=outputs['OUTPUT_LAYER'] layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['id','poly_num_a','poly_st_a','id_2','poly_num_b','poly_st_b'] expectedtypes=['Integer','Real','String','Integer','Real','String'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(8, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1","1.1","string a","2","1","string a"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POLYGON((270807.08580285 4458940.1594565,270798.42294527 4458914.62661676,270780.81854858 4458914.21983449,270763.52289518 4458920.715993,270760.3449542 4458926.6570575,270763.78234766 4458958.22561242,270794.30290024 4458942.16424502,270807.08580285 4458940.1594565))' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def __init__(self, alg, iterParam = None, parent = None): QThread.__init__(self, parent) self.algorithm = alg self.parameterToIterate = iterParam class Progress: def __init__(self, algex): self.algorithmExecutor = algex def setText(self, text): self.algorithmExecutor.textChanged.emit(text) def setPercentage(self, p): self.algorithmExecutor.percentageChanged.emit(p) def setInfo(self, info): self.algorithmExecutor.infoSet.emit(info) def setCommand(self, cmd): self.algorithmExecutor.commandSet.emit(cmd) def setDebugInfo(self, info): self.algorithmExecutor.debugInfoSet.emit(info) def setConsoleInfo(self, info): self.algorithmExecutor.consoleInfoSet.emit(info) self.progress = Progress(self) if self.parameterToIterate: self.run = self.runalgIterating #generate all single-feature layers settings = QSettings() systemEncoding = settings.value( "/UI/encoding", "System" ) layerfile = alg.getParameterValue(self.parameterToIterate) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() features = getfeatures(layer) self.filelist = [] for feat in features: output = ProcessingUtils.getTempFilename("shp") self.filelist.append(output) writer = QgsVectorFileWriter(output, systemEncoding,provider.fields(), provider.geometryType(), layer.crs() ) writer.addFeature(feat) del writer else: self.run = self.runalg self.internalError.connect(self.raiseInternalError)
def processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = ProcessingUtils.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 ProcessingUtils.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 getOutputFile(self): if self.useTempFiles: return None else: return ProcessingUtils.getTempFilename('shp')
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 testWrongformat(self): outputs=processing.runalg("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS",ProcessingUtils.getTempFilename("wrongext")) output=outputs['OUTPUT'] self.assertTrue(output.endswith('shp')) layer=QGisLayers.getObjectFromUri(output, True) fields=layer.pendingFields() expectednames=['ID','POLY_NUM_A','POLY_ST_A','NUMPOINTS'] expectedtypes=['Integer','Real','String','Real'] names=[str(f.name()) for f in fields] types=[str(f.typeName()) for f in fields] self.assertEqual(expectednames, names) self.assertEqual(expectedtypes, types) features=processing.getfeatures(layer) self.assertEqual(2, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["1","1.1","string a","6.0"] values=[str(attr) for attr in attrs] self.assertEqual(expectedvalues, values)
def processAlgorithm(self, progress): path = OTBUtils.otbPath() libpath = OTBUtils.otbLibPath() if path == "" or libpath == "": raise GeoAlgorithmExecutionException("OTB folder is not configured.\nPlease configure it before running OTB algorithms.") commands = [] commands.append(path + os.sep + self.cliName) self.roiVectors = {} self.roiRasters = {} for param in self.parameters: if param.value == None or param.value == "": continue if isinstance(param, ParameterVector): commands.append(param.name) if self.hasROI: roiFile = ProcessingUtils.getTempFilename('shp') commands.append(roiFile) self.roiVectors[param.value] = roiFile else: commands.append("\"" + param.value+ "\"") elif isinstance(param, ParameterRaster): commands.append(param.name) if self.hasROI: roiFile = ProcessingUtils.getTempFilename('tif') commands.append(roiFile) self.roiRasters[param.value] = roiFile else: commands.append("\"" + param.value+ "\"") elif isinstance(param, ParameterMultipleInput): commands.append(param.name) files = str(param.value).split(";") paramvalue = " ".join(["\"" + f + "\"" for f in files]) commands.append(paramvalue) elif isinstance(param, ParameterSelection): commands.append(param.name) idx = int(param.value) commands.append(str(param.options[idx])) elif isinstance(param, ParameterBoolean): if param.value: commands.append(param.name) commands.append(str(param.value).lower()) elif isinstance(param, ParameterExtent): self.roiValues = param.value.split(",") else: commands.append(param.name) commands.append(str(param.value)) for out in self.outputs: commands.append(out.name) commands.append('"' + out.value + '"') for roiInput, roiFile in self.roiRasters.items(): startX, startY = float(self.roiValues[0]), float(self.roiValues[1]) sizeX = float(self.roiValues[2]) - startX sizeY = float(self.roiValues[3]) - startY helperCommands = [ "otbcli_ExtractROI", "-in", roiInput, "-out", roiFile, "-startx", str(startX), "-starty", str(startY), "-sizex", str(sizeX), "-sizey", str(sizeY)] ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands) progress.setCommand(helperCommands) OTBUtils.executeOtb(helperCommands, progress) if self.roiRasters: supportRaster = self.roiRasters.itervalues().next() for roiInput, roiFile in self.roiVectors.items(): helperCommands = [ "otbcli_VectorDataExtractROIApplication", "-vd.in", roiInput, "-io.in", supportRaster, "-io.out", roiFile, "-elev.dem.path", OTBUtils.otbSRTMPath()] ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands) progress.setCommand(helperCommands) OTBUtils.executeOtb(helperCommands, progress) loglines = [] loglines.append("OTB execution command") for line in commands: loglines.append(line) progress.setCommand(line) ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) OTBUtils.executeOtb(commands, progress)
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): path = OTBUtils.otbPath() libpath = OTBUtils.otbLibPath() if path == "" or libpath == "": raise GeoAlgorithmExecutionException( "OTB folder is not configured.\nPlease configure it before running OTB algorithms." ) commands = [] commands.append(path + os.sep + self.cliName) self.roiVectors = {} self.roiRasters = {} for param in self.parameters: if param.value == None or param.value == "": continue if isinstance(param, ParameterVector): commands.append(param.name) if self.hasROI: roiFile = ProcessingUtils.getTempFilename('shp') commands.append(roiFile) self.roiVectors[param.value] = roiFile else: commands.append("\"" + param.value + "\"") elif isinstance(param, ParameterRaster): commands.append(param.name) if self.hasROI: roiFile = ProcessingUtils.getTempFilename('tif') commands.append(roiFile) self.roiRasters[param.value] = roiFile else: commands.append("\"" + param.value + "\"") elif isinstance(param, ParameterMultipleInput): commands.append(param.name) files = str(param.value).split(";") paramvalue = " ".join(["\"" + f + "\"" for f in files]) commands.append(paramvalue) elif isinstance(param, ParameterSelection): commands.append(param.name) idx = int(param.value) commands.append(str(param.options[idx])) elif isinstance(param, ParameterBoolean): if param.value: commands.append(param.name) commands.append(str(param.value).lower()) elif isinstance(param, ParameterExtent): self.roiValues = param.value.split(",") else: commands.append(param.name) commands.append(str(param.value)) for out in self.outputs: commands.append(out.name) commands.append('"' + out.value + '"') for roiInput, roiFile in self.roiRasters.items(): startX, startY = float(self.roiValues[0]), float(self.roiValues[1]) sizeX = float(self.roiValues[2]) - startX sizeY = float(self.roiValues[3]) - startY helperCommands = [ "otbcli_ExtractROI", "-in", roiInput, "-out", roiFile, "-startx", str(startX), "-starty", str(startY), "-sizex", str(sizeX), "-sizey", str(sizeY) ] ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands) progress.setCommand(helperCommands) OTBUtils.executeOtb(helperCommands, progress) if self.roiRasters: supportRaster = self.roiRasters.itervalues().next() for roiInput, roiFile in self.roiVectors.items(): helperCommands = [ "otbcli_VectorDataExtractROIApplication", "-vd.in", roiInput, "-io.in", supportRaster, "-io.out", roiFile, "-elev.dem.path", OTBUtils.otbSRTMPath() ] ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands) progress.setCommand(helperCommands) OTBUtils.executeOtb(helperCommands, progress) loglines = [] loglines.append("OTB execution command") for line in commands: loglines.append(line) progress.setCommand(line) ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) OTBUtils.executeOtb(commands, progress)