From 32d6275fa3514f4bdea61ea4919c36c47537402e Mon Sep 17 00:00:00 2001 From: volaya Date: Mon, 22 Jun 2015 09:10:01 +0200 Subject: [PATCH] [processing]changed native raster format for SAGA algorithms from tif to sdat This eliminates the need to export to a QGIS compatible format after generating a raster output. The SDAT format is the native SAGA format, and it is supported by GDAL, so it is also supported by QGIS. This was not used before, but now we use that to reduce the nuber of steps and provide a better integration. Conversion to other formats is now done by Processing itself, not by the SAGA algorithm. fixes #10735 --- .../processing/algs/saga/SagaAlgorithm212.py | 43 +++---------- .../processing/algs/saga/SagaAlgorithm213.py | 60 ++----------------- .../algs/saga/SagaAlgorithmProvider.py | 2 +- 3 files changed, 15 insertions(+), 90 deletions(-) diff --git a/python/plugins/processing/algs/saga/SagaAlgorithm212.py b/python/plugins/processing/algs/saga/SagaAlgorithm212.py index 11b1fab8a9f..51523c99849 100644 --- a/python/plugins/processing/algs/saga/SagaAlgorithm212.py +++ b/python/plugins/processing/algs/saga/SagaAlgorithm212.py @@ -28,8 +28,8 @@ __revision__ = '$Format:%H$' import os import importlib +import subprocess from PyQt4.QtGui import QIcon -from processing.gui.Help2Html import getHtmlFromRstFile from processing.core.GeoAlgorithm import GeoAlgorithm from processing.core.ProcessingConfig import ProcessingConfig from processing.core.ProcessingLog import ProcessingLog @@ -209,51 +209,23 @@ class SagaAlgorithm212(GeoAlgorithm): 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 + '"' + command += ' -' + out.name + ' "' + out.getCompatibleFileName(self) + '"' commands.append(command) - # 3: Export resulting raster layers - # optim = ProcessingConfig.getSetting(SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION) + + # special treatment for RGB algorithm + #TODO: improve this and put this code somewhere else for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = filename + '.sgrd' - formatIndex = (4 if isWindows() else 1) - sessionExportedLayers[filename] = filename2 - - # Do not export is the output is not a final output - # of the model - # dontExport = True - # 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': commands.append('io_grid_image 0 -IS_RGB -GRID:"' + filename2 + '" -FILE:"' + filename + '"') - else: - commands.append('io_gdal 1 -GRIDS "' + filename2 - + '" -FORMAT ' + str(formatIndex) - + ' -TYPE 0 -FILE "' + filename + '"') - - # 4: Run SAGA + # 3: Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] @@ -265,6 +237,7 @@ class SagaAlgorithm212(GeoAlgorithm): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress) + def preProcessInputs(self): name = self.commandLineName().replace('.', '_')[len('saga:'):] try: @@ -352,4 +325,4 @@ class SagaAlgorithm212(GeoAlgorithm): if extent != extent2: return self.tr("Input layers do not have the same grid extent.") - + diff --git a/python/plugins/processing/algs/saga/SagaAlgorithm213.py b/python/plugins/processing/algs/saga/SagaAlgorithm213.py index 6b06365d4e0..83b23c95ef6 100644 --- a/python/plugins/processing/algs/saga/SagaAlgorithm213.py +++ b/python/plugins/processing/algs/saga/SagaAlgorithm213.py @@ -35,7 +35,7 @@ from processing.core.parameters import ParameterRaster, ParameterVector, Paramet from processing.core.outputs import OutputRaster, OutputVector, OutputTable import SagaUtils from processing.tools import dataobjects -from processing.tools.system import getTempFilenameInTempFolder, getTempFilename, isWindows +from processing.tools.system import getTempFilename sessionExportedLayers = {} @@ -161,50 +161,23 @@ class SagaAlgorithm213(SagaAlgorithm212): 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 + '"' + command += ' -' + out.name + ' "' + out.getCompatibleFileName(self) + '"' commands.append(command) - # 3: Export resulting raster layers - # optim = ProcessingConfig.getSetting( SagaUtils.SAGA_IMPORT_EXPORT_OPTIMIZATION) + # special treatment for RGB algorithm + #TODO: improve this and put this code somewhere else for out in self.outputs: if isinstance(out, OutputRaster): filename = out.getCompatibleFileName(self) filename2 = filename + '.sgrd' - formatIndex = (4 if isWindows() else 1) - sessionExportedLayers[filename] = filename2 - # Do not export is the output is not a final output - # of the model - # dontExport = True - #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': commands.append('io_grid_image 0 -IS_RGB -GRID:"' + filename2 + '" -FILE:"' + filename + '"') - else: - commands.append('io_gdal 1 -GRIDS "' + filename2 - + '" -FORMAT ' + str(formatIndex) - + ' -TYPE 0 -FILE "' + filename + '"') - # 4: Run SAGA + # 3: Run SAGA commands = self.editCommands(commands) SagaUtils.createSagaBatchJobFileFromSagaCommands(commands) loglines = [] @@ -216,25 +189,4 @@ class SagaAlgorithm213(SagaAlgorithm212): ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines) SagaUtils.executeSaga(progress) - def exportRasterLayer(self, source): - global sessionExportedLayers - if source in sessionExportedLayers: - exportedLayer = sessionExportedLayers[source] - if os.path.exists(exportedLayer): - self.exportedLayers[source] = exportedLayer - return None - else: - del sessionExportedLayers[source] - layer = dataobjects.getObjectFromUri(source, False) - if layer: - filename = layer.name() - else: - filename = os.path.basename(source) - validChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:' - filename = ''.join(c for c in filename if c in validChars) - if len(filename) == 0: - filename = 'layer' - destFilename = getTempFilenameInTempFolder(filename + '.sgrd') - self.exportedLayers[source] = destFilename - sessionExportedLayers[source] = destFilename - return 'io_gdal 0 -TRANSFORM -INTERPOL 0 -GRIDS "' + destFilename + '" -FILES "' + source + '"' + diff --git a/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py b/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py index 4d78cecc480..b2bc3caaa6b 100644 --- a/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py +++ b/python/plugins/processing/algs/saga/SagaAlgorithmProvider.py @@ -122,7 +122,7 @@ class SagaAlgorithmProvider(AlgorithmProvider): return ['shp'] def getSupportedOutputRasterLayerExtensions(self): - return ['tif'] + return ['sdat'] def getSupportedOutputTableLayerExtensions(self): return ['dbf']