Skip to content

Commit

Permalink
[processing]changed native raster format for SAGA algorithms from tif…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
volaya committed Jun 22, 2015
1 parent 92cf321 commit 32d6275
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 90 deletions.
43 changes: 8 additions & 35 deletions python/plugins/processing/algs/saga/SagaAlgorithm212.py
Expand Up @@ -28,8 +28,8 @@

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
Expand Down Expand Up @@ -209,51 +209,23 @@ def processAlgorithm(self, progress):
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 = []
Expand All @@ -265,6 +237,7 @@ def processAlgorithm(self, progress):
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
SagaUtils.executeSaga(progress)


def preProcessInputs(self):
name = self.commandLineName().replace('.', '_')[len('saga:'):]
try:
Expand Down Expand Up @@ -352,4 +325,4 @@ def checkParameterValuesBeforeExecuting(self):
if extent != extent2:
return self.tr("Input layers do not have the same grid extent.")


60 changes: 6 additions & 54 deletions python/plugins/processing/algs/saga/SagaAlgorithm213.py
Expand Up @@ -35,7 +35,7 @@
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 = {}

Expand Down Expand Up @@ -161,50 +161,23 @@ def processAlgorithm(self, progress):
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 = []
Expand All @@ -216,25 +189,4 @@ def processAlgorithm(self, progress):
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 + '"'

Expand Up @@ -122,7 +122,7 @@ def getSupportedOutputVectorLayerExtensions(self):
return ['shp']

def getSupportedOutputRasterLayerExtensions(self):
return ['tif']
return ['sdat']

def getSupportedOutputTableLayerExtensions(self):
return ['dbf']
Expand Down

0 comments on commit 32d6275

Please sign in to comment.