def exportVectorLayer(layer): '''Takes a QgsVectorLayer and returns the filename to refer to it, 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 is a remote one or db-based (non-file based) one, or if there is a selection and it should be used, exporting just the selected features. Currently, the output is restricted to shapefiles, so anything that is not in a shapefile will get exported. It also export to a new file if the original one contains non-ascii characters''' settings = QSettings() systemEncoding = settings.value( "/UI/encoding", "System" ).toString() output = SextanteUtils.getTempFilename("shp") provider = layer.dataProvider() useSelection = SextanteConfig.getSetting(SextanteConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: writer = QgsVectorFileWriter(output, systemEncoding, layer.pendingFields(), provider.geometryType(), layer.crs()) selection = layer.selectedFeatures() for feat in selection: writer.addFeature(feat) del writer return output else: isASCII=True try: unicode(layer.source()).decode("ascii") except UnicodeEncodeError: isASCII=False if (not unicode(layer.source()).endswith("shp") or not isASCII): writer = QgsVectorFileWriter( output, systemEncoding, layer.pendingFields(), provider.geometryType(), layer.crs() ) for feat in layer.getFeatures(): writer.addFeature(feat) del writer return output else: return unicode(layer.source())
def test_SagaRasterAlgorithmWithUnsupportedOutputFormat(self): outputs=sextante.runalg("saga:convergenceindex",raster(),0,0,SextanteUtils.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 exportRasterLayer(self, layer): destFilename = SextanteUtils.getTempFilename("sgrd") self.exportedLayers[layer]= destFilename if SextanteUtils.isWindows(): return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer+"\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer + "\""
def test_SagaVectorAlgorithWithUnsupportedInputAndOutputFormat(self): '''this tests both the exporting to shp and then the format change in the output layer''' layer = sextante.getobject(polygonsGeoJson()); feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) outputs=sextante.runalg("saga:polygoncentroids",polygonsGeoJson(),True, SextanteUtils.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=sextante.getfeatures(layer) self.assertEqual(1, len(features)) feature=features.next() attrs=feature.attributes() expectedvalues=["0","1.1","string a"] values=[str(attr.toString()) for attr in attrs] self.assertEqual(expectedvalues, values) wkt='POINT(270787.49991451 4458955.46775295)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
def processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = SextanteUtils.getTempFilename() r = self.getOutputValue(SplitRGBBands.R) g = self.getOutputValue(SplitRGBBands.G) b = self.getOutputValue(SplitRGBBands.B) commands = [] if SextanteUtils.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 exportVectorLayer(layer): '''Takes a QgsVectorLayer and returns the filename to refer to it, 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 is a remote one or db-based (non-file based) one, or if there is a selection and it should be used, exporting just the selected features. Currently, the output is restricted to shapefiles, so anything that is not in a shapefile will get exported''' settings = QSettings() systemEncoding = settings.value( "/UI/encoding", "System" ).toString() output = SextanteUtils.getTempFilename("shp") provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select( allAttrs ) useSelection = SextanteConfig.getSetting(SextanteConfig.USE_SELECTED) if useSelection and layer.selectedFeatureCount() != 0: writer = QgsVectorFileWriter( output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() ) selection = layer.selectedFeatures() for feat in selection: writer.addFeature(feat) del writer return output else: if (not unicode(layer.source()).endswith("shp")): writer = QgsVectorFileWriter( output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() ) feat = QgsFeature() while provider.nextFeature(feat): writer.addFeature(feat) del writer return output else: return unicode(layer.source())
def exportRasterLayer(self, layer): destFilename = SextanteUtils.getTempFilename("sgrd") self.exportedLayers[layer] = destFilename if SextanteUtils.isWindows(): return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer + "\"" else: return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + layer + "\""
def test_gdalogrsieveWithUnsupportedOutputFormat(self): outputs=sextante.runalg("gdalogr:sieve",raster(),2,0, SextanteUtils.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 = SextanteUtils.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 SextanteUtils.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 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 = SextanteUtils.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 = sextante.getobject(polygonsGeoJson()) feature = layer.getFeatures().next() selected = [feature.id()] layer.setSelectedFeatures(selected) outputs = sextante.runalg("saga:polygoncentroids", polygonsGeoJson(), True, SextanteUtils.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 = sextante.getfeatures(layer) self.assertEqual(1, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["0", "1.1", "string a"] values = [str(attr.toString()) for attr in attrs] self.assertEqual(expectedvalues, values) wkt = 'POINT(270787.49991451 4458955.46775295)' self.assertEqual(wkt, str(feature.geometry().exportToWkt()))
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 = SextanteUtils.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_gdalogrogr2ogrWrongExtension(self): outputs = sextante.runalg("gdalogr:ogr2ogr", union(), 3, "", SextanteUtils.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 = sextante.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 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.''' if self.value.endswith(self.getDefaultFileExtension(alg)): return self.value else: if self.compatible is None: self.compatible = SextanteUtils.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 = SextanteUtils.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").toString() layerfile = alg.getParameterValue(self.parameterToIterate) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select(allAttrs) feat = QgsFeature() self.filelist = [] while provider.nextFeature(feat): output = SextanteUtils.getTempFilename("shp") self.filelist.append(output) writer = QgsVectorFileWriter(output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs()) writer.addFeature(feat) del writer else: self.run = self.runalg self.internalError.connect(self.raiseInternalError)
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.getSupportedOutputTableExtensions(): return self.value else: if self.compatible is None: self.compatible = SextanteUtils.getTempFilename( self.getDefaultFileExtension(alg)) return self.compatible
def processAlgorithm(self, progress): '''Here is where the processing itself takes place''' ili = self.getParameterValue(self.ILI) imd = SextanteUtils.getTempFilename('imd') gml = SextanteUtils.getTempFilename('gml') db = self.getParameterFromName(self.DB) #output = self.getOutputValue(self.OUTPUT) IliUtils.runIli2c(["-oIMD", "--out", imd, ili], progress) gmlstr = extract_enums_asgml(imd) f = open(gml, "w") f.write(gmlstr) f.close() #IliUtils.runShellCmd(["ogr2ogr", "-f", "PostgreSQL", db.getOgrConnection(), gml], progress) ogr2ogr(pszFormat=db.getOgrDriverName(), pszDataSource=gml, pszDestDataSource=db.getOgrConnection(), errfunc=IliUtils.errfunc)
def runalgIterating(alg, paramToIter, progress): #generate all single-feature layers settings = QSettings() systemEncoding = settings.value("/UI/encoding", "System").toString() layerfile = alg.getParameterValue(paramToIter) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select(allAttrs) feat = QgsFeature() filelist = [] outputs = {} while provider.nextFeature(feat): output = SextanteUtils.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()): SextantePostprocessing.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 = SextanteUtils.getTempFilename("sgrd") self.exportedLayers[layer]= destFilename if SextanteUtils.isWindows(): 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 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 = SextanteUtils.getTempFilename("sgrd") self.exportedLayers[layer] = destFilename if SextanteUtils.isWindows() or SextanteUtils.isMac(): 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 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 = SextanteUtils.getTempFilename("sgrd") self.exportedLayers[layer] = destFilename if SextanteUtils.isWindows() or SextanteUtils.isMac(): 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 checkSagaIsInstalled(cls): if SextanteUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms." cmdpath = os.path.join(path, "saga_cmd.exe") if not os.path.exists(cmdpath): return ("The specified SAGA folder does not contain a valid SAGA executable.\n" + "Please, go to the SEXTANTE settings dialog, and check that the SAGA\n" + "folder is correctly configured") SAGA_INSTALLED = "/SextanteQGIS/SagaInstalled" settings = QSettings() if settings.contains(SAGA_INSTALLED): return try: qgis = QGisLayers.iface crs = qgis.mapCanvas().mapRenderer().destinationCrs() fields = [] fields.append(QgsField("NUM_FIELD", QVariant.Int)) filename = SextanteUtils.getTempFilename("shp") writer = SextanteVectorWriter(filename, None, fields, QGis.WKBPoint, crs) for x in range(5): for y in range(5): attrs = [] attrs.append(QVariant(x)) outFeat = QgsFeature() pt = QgsPoint(x, y) outFeat.setGeometry(QgsGeometry.fromPoint(pt)) outFeat.setAttributes(attrs) writer.addFeature(outFeat) del writer.writer del writer from sextante.core.Sextante import runalg result = runalg("saga:thiessenpolygons", filename, None) if not os.path.exists(result['POLYGONS']): return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms." except: s = traceback.format_exc() return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s; settings.setValue("/SextanteQGIS/SagaInstalled", True)
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 = SextanteUtils.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() SextanteResults.addResult(self.algs[0].name + "[summary]", outputFile)
def runalgIterating(alg, paramToIter, progress): # generate all single-feature layers settings = QSettings() systemEncoding = settings.value("/UI/encoding", "System").toString() layerfile = alg.getParameterValue(paramToIter) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select(allAttrs) feat = QgsFeature() filelist = [] outputs = {} while provider.nextFeature(feat): output = SextanteUtils.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()): SextantePostprocessing.handleAlgorithmResults(alg, progress, False) i += 1 else: return False return True
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" ).toString() layerfile = alg.getParameterValue(self.parameterToIterate) layer = QGisLayers.getObjectFromUri(layerfile, False) provider = layer.dataProvider() allAttrs = provider.attributeIndexes() provider.select( allAttrs ) feat = QgsFeature() self.filelist = [] while provider.nextFeature(feat): output = SextanteUtils.getTempFilename("shp") self.filelist.append(output) writer = QgsVectorFileWriter(output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() ) writer.addFeature(feat) del writer else: self.run = self.runalg self.internalError.connect(self.raiseInternalError)
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 sextante temp dir, so it is deleted at the end. f = SextanteUtils.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=sextante.runalg("gdalogr:ogr2ogr",union(),3,"",SextanteUtils.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=sextante.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.toString()) 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 processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = SextanteUtils.getTempFilename(); r = self.getOutputValue(SplitRGBBands.R) g = self.getOutputValue(SplitRGBBands.G) b = self.getOutputValue(SplitRGBBands.B) commands = [] if SextanteUtils.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 testWrongformat(self): outputs = sextante.runalg("qgis:countpointsinpolygon", polygons(), points(), "NUMPOINTS", SextanteUtils.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 = sextante.getfeatures(layer) self.assertEqual(2, len(features)) feature = features.next() attrs = feature.attributes() expectedvalues = ["1", "1.1", "string a", "6"] values = [str(attr.toString()) for attr in attrs] self.assertEqual(expectedvalues, values)
def processAlgorithm(self, progress): #TODO:check correct num of bands input = self.getParameterValue(SplitRGBBands.INPUT) temp = SextanteUtils.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 SextanteUtils.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 processAlgorithm(self, progress): if SextanteUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": raise GeoAlgorithmExecutionException( "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms." ) commands = list() self.exportedLayers = {} #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 if SextanteUtils.isWindows() or SextanteUtils.isMac(): 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 = SextanteUtils.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 = SextanteUtils.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 = SextanteUtils.tempFolder( ) + os.sep + os.path.basename(filename) + ".sgrd" if SextanteUtils.isWindows() or SextanteUtils.isMac(): commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"") else: commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"") #4 Run SAGA SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append("SAGA execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress)
def getOutputFile(self): if self.useTempFiles: return None else: return SextanteUtils.getTempFilename('shp')
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 = SextanteUtils.getTempFilename('shp') commands.append(roiFile) self.roiVectors[param.value] = roiFile else: commands.append(param.value) if isinstance(param, ParameterRaster): commands.append(param.name) if self.hasROI: roiFile = SextanteUtils.getTempFilename('tif') commands.append(roiFile) self.roiRasters[param.value] = roiFile else: commands.append(param.value) elif isinstance(param, ParameterMultipleInput): commands.append(param.name) commands.append(str(param.value.replace(";", " "))) 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) ] SextanteLog.addToLog(SextanteLog.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() ] SextanteLog.addToLog(SextanteLog.LOG_INFO, helperCommands) progress.setCommand(helperCommands) OTBUtils.executeOtb(helperCommands, progress) loglines = [] loglines.append("OTB execution command") for line in commands: loglines.append(line) SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) progress.setCommand(loglines) OTBUtils.executeOtb(commands, progress)
def testWrongformat(self): outputs=sextante.runalg("qgis:countpointsinpolygon",polygons(),points(),"NUMPOINTS",SextanteUtils.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=sextante.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 = SextanteUtils.getTempFilename('shp') commands.append(roiFile) self.roiVectors[param.value] = roiFile else: commands.append(param.value) if isinstance(param, ParameterRaster): commands.append(param.name) if self.hasROI: roiFile = SextanteUtils.getTempFilename('tif') commands.append(roiFile) self.roiRasters[param.value] = roiFile else: commands.append(param.value) elif isinstance(param, ParameterMultipleInput): commands.append(param.name) commands.append(str(param.value.replace(";"," "))) 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)] SextanteLog.addToLog(SextanteLog.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()] SextanteLog.addToLog(SextanteLog.LOG_INFO, helperCommands) progress.setCommand(helperCommands) OTBUtils.executeOtb(helperCommands, progress) loglines = [] loglines.append("OTB execution command") for line in commands: loglines.append(line) SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) progress.setCommand(loglines) OTBUtils.executeOtb(commands, 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 = SextanteUtils.getTempFilename('shp') commands.append(roiFile) self.roiVectors[param.value] = roiFile else: commands.append(param.value) if isinstance(param, ParameterRaster): commands.append(param.name) if self.hasROI: roiFile = SextanteUtils.getTempFilename('tif') commands.append(roiFile) self.roiRasters[param.value] = roiFile else: commands.append(param.value) elif isinstance(param, ParameterMultipleInput): commands.append(param.name) commands.append(str(param.value.replace(";"," "))) 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)] SextanteLog.addToLog(SextanteLog.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()] SextanteLog.addToLog(SextanteLog.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) SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) import sextante.otb.OTBSpecific module = sextante.otb.OTBSpecific found = False if 'adapt%s' % self.appkey in dir(module): found = True commands = getattr(module, 'adapt%s' % self.appkey)(commands) else: the_key = 'adapt%s' % self.appkey if '-' in the_key: base_key = the_key.split("-")[0] if base_key in dir(module): found = True commands = getattr(module, base_key)(commands) if not found: SextanteLog.addToLog(SextanteLog.LOG_INFO, "Adapter for %s not found" % the_key) frames = inspect.getouterframes(inspect.currentframe())[1:] for a_frame in frames: frame,filename,line_number,function_name,lines,index= a_frame SextanteLog.addToLog(SextanteLog.LOG_INFO, "%s %s %s %s %s %s" % (frame,filename,line_number,function_name,lines,index)) OTBUtils.executeOtb(commands, progress)
def processAlgorithm(self, progress): if SextanteUtils.isWindows(): path = SagaUtils.sagaPath() if path == "": raise GeoAlgorithmExecutionException("SAGA folder is not configured.\nPlease configure it before running SAGA algorithms.") commands = list() self.exportedLayers = {} #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 if SextanteUtils.isWindows(): command = self.undecoratedGroup + " \"" + self.cmdname + "\"" else: command = "lib" + self.undecoratedGroup + " \"" + self.cmdname + "\"" 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 = SextanteUtils.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 = out.value #=============================================================== # if not filename.endswith(".tif"): # filename += ".tif" # out.value = filename #=============================================================== filename = SextanteUtils.tempFolder() + os.sep + os.path.basename(filename) + ".sgrd" command+=(" -" + out.name + " \"" + filename + "\""); if isinstance(out, OutputVector): filename = out.getCompatibleFileName(self)#out.value #=============================================================== # if not filename.endswith(".shp"): # filename += ".shp" # out.value = filename #=============================================================== command+=(" -" + out.name + " \"" + filename + "\""); if isinstance(out, OutputTable): filename = out.getCompatibleFileName(self)#out.value #=============================================================== # if not filename.endswith(".dbf"): # filename += ".dbf" # out.value = filename #=============================================================== 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 = SextanteUtils.tempFolder() + os.sep + os.path.basename(filename) + ".sgrd" if SextanteUtils.isWindows(): commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\""); else: commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\""); #4 Run SAGA SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] loglines.append("SAGA execution commands") for line in commands: progress.setCommand(line) loglines.append(line) if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS): SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress);