Skip to content

Commit

Permalink
[processing] Reduced number of conversion operation when running saga…
Browse files Browse the repository at this point in the history
… algorithms
  • Loading branch information
volaya committed Sep 15, 2013
1 parent 2d101c4 commit a5f0e4b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
10 changes: 8 additions & 2 deletions python/plugins/processing/core/GeoAlgorithm.py
Expand Up @@ -65,6 +65,11 @@ def __init__(self):
self.canRunInBatchMode = True
#to be set by the provider when it loads the algorithm
self.provider = None

#if the algorithm is run as part of a model, the parent model can be set in this variable,
#to allow for customized behaviour, in case some operations should be run differently when
#running as part of a model
self.model = None

self.defineCharacteristics()

Expand Down Expand Up @@ -135,13 +140,14 @@ def checkParameterValuesBeforeExecuting(self):
#=========================================================


def execute(self, progress):
def execute(self, progress, model = None):

'''The method to use to call a processing algorithm.
Although the body of the algorithm is in processAlgorithm(),
it should be called using this method, since it performs
some additional operations.
Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''

self.model = model
try:
self.setOutputCRS()
self.resolveTemporaryOutputs()
Expand Down
10 changes: 2 additions & 8 deletions python/plugins/processing/grass/GrassAlgorithm.py
Expand Up @@ -46,7 +46,7 @@
from processing.parameters.ParameterFactory import ParameterFactory
from processing.outputs.OutputFactory import OutputFactory
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools import dataobjects
from processing.tools import dataobjects, system
from processing.grass.GrassUtils import GrassUtils
from processing.parameters.ParameterSelection import ParameterSelection
from processing.core.WrongHelpFileException import WrongHelpFileException
Expand All @@ -55,12 +55,6 @@
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterString import ParameterString

NUM_EXPORTED = 1

def getNumExportedLayers():
NUM_EXPORTED += 1
return NUM_EXPORTED

class GrassAlgorithm(GeoAlgorithm):

GRASS_OUTPUT_TYPE_PARAMETER = "GRASS_OUTPUT_TYPE_PARAMETER"
Expand Down Expand Up @@ -443,7 +437,7 @@ def exportRasterLayer(self, layer):


def getTempFilename(self):
filename = "tmp" + str(time.time()).replace(".","") + str(getNumExportedLayers())
filename = "tmp" + str(time.time()).replace(".","") + str(system.getNumExportedLayers())
return filename

def commandLineName(self):
Expand Down
12 changes: 9 additions & 3 deletions python/plugins/processing/grass/GrassUtils.py
Expand Up @@ -23,7 +23,8 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
import stat
import shutil
from qgis.core import QgsApplication
from PyQt4.QtCore import *
import traceback
Expand All @@ -32,8 +33,7 @@
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools.system import *
from processing.core.ProcessingLog import ProcessingLog
import stat
import shutil


class GrassUtils:

Expand Down Expand Up @@ -67,6 +67,12 @@ def grassScriptFilename():
filename = "grass_script.bat"
filename = userFolder() + os.sep + filename
return filename

@staticmethod
def getGrassVersion():
#I do not know if this should be removed or let the user enter it
#or something like that... This is just a temporary thing
return "6.4.0"

@staticmethod
def grassPath():
Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/grass/nviz.py
Expand Up @@ -23,7 +23,6 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os
from qgis.core import *
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput
from processing.grass.GrassUtils import GrassUtils
Expand All @@ -34,7 +33,6 @@
from processing.parameters.ParameterNumber import ParameterNumber
from processing.parameters.ParameterRaster import ParameterRaster
from processing.tools import dataobjects
import time

class nviz(GeoAlgorithm):

Expand Down
7 changes: 4 additions & 3 deletions python/plugins/processing/gui/UnthreadedAlgorithmExecutor.py
Expand Up @@ -16,8 +16,7 @@
* *
***************************************************************************
"""
from processing.tools.vector import features
from processing.core.ProcessingLog import ProcessingLog


__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand All @@ -31,6 +30,8 @@
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.tools import dataobjects
from processing.tools.system import *
from processing.tools import vector
from processing.core.ProcessingLog import ProcessingLog
from processing.gui.Postprocessing import Postprocessing
from processing.core.SilentProgress import SilentProgress
import traceback
Expand Down Expand Up @@ -65,7 +66,7 @@ def runalgIterating(alg,paramToIter,progress):
filelist = []
outputs = {}
provider = layer.dataProvider()
features = features(layer)
features = vector.features(layer)
for feat in features:
output = getTempFilename("shp")
filelist.append(output)
Expand Down
14 changes: 10 additions & 4 deletions python/plugins/processing/saga/SagaAlgorithm.py
Expand Up @@ -51,6 +51,8 @@
from processing.parameters.ParameterFixedTable import ParameterFixedTable
from processing.core.ProcessingLog import ProcessingLog

sessionExportedLayers = {}

class SagaAlgorithm(GeoAlgorithm):

OUTPUT_EXTENT = "OUTPUT_EXTENT"
Expand Down Expand Up @@ -193,7 +195,9 @@ def processAlgorithm(self, progress):
continue
value = param.value
if not value.endswith("sgrd"):
commands.append(self.exportRasterLayer(value))
exportCommand = self.exportRasterLayer(value)
if exportCommand is not None:
commands.append(exportCommand)
if self.resample:
commands.append(self.resampleRasterLayer(value));
if isinstance(param, ParameterVector):
Expand Down Expand Up @@ -309,6 +313,7 @@ def processAlgorithm(self, progress):
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 + "\"");
sessionExportedLayers[filename] = filename2


#4 Run SAGA
Expand Down Expand Up @@ -377,6 +382,9 @@ def resampleRasterLayer(self,layer):


def exportRasterLayer(self, source):
if source in sessionExportedLayers:
self.exportedLayers[source] = sessionExportedLayers[source]
return None
layer = dataobjects.getObjectFromUri(source, False)
if layer:
filename = str(layer.name())
Expand All @@ -388,15 +396,13 @@ def exportRasterLayer(self, source):
filename = "layer"
destFilename = getTempFilenameInTempFolder(filename + ".sgrd")
self.exportedLayers[source]= destFilename
sessionExportedLayers[source] = destFilename
saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208)
if isWindows() or isMac() or not saga208:
return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source+"\""
else:
return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source + "\""




def checkBeforeOpeningParametersDialog(self):
msg = SagaUtils.checkSagaIsInstalled()
if msg is not None:
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/processing/tools/system.py
Expand Up @@ -77,11 +77,12 @@ def removeInvalidChars(string):
return string


NUM_EXPORTED = 1
numExported = 1

def getNumExportedLayers():
NUM_EXPORTED += 1
return NUM_EXPORTED
global numExported
numExported += 1
return numExported

def mkdir(newdir):
if os.path.isdir(newdir):
Expand Down

0 comments on commit a5f0e4b

Please sign in to comment.