Skip to content

Commit a5f0e4b

Browse files
committed
[processing] Reduced number of conversion operation when running saga algorithms
1 parent 2d101c4 commit a5f0e4b

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

python/plugins/processing/core/GeoAlgorithm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def __init__(self):
6565
self.canRunInBatchMode = True
6666
#to be set by the provider when it loads the algorithm
6767
self.provider = None
68+
69+
#if the algorithm is run as part of a model, the parent model can be set in this variable,
70+
#to allow for customized behaviour, in case some operations should be run differently when
71+
#running as part of a model
72+
self.model = None
6873

6974
self.defineCharacteristics()
7075

@@ -135,13 +140,14 @@ def checkParameterValuesBeforeExecuting(self):
135140
#=========================================================
136141

137142

138-
def execute(self, progress):
143+
def execute(self, progress, model = None):
144+
139145
'''The method to use to call a processing algorithm.
140146
Although the body of the algorithm is in processAlgorithm(),
141147
it should be called using this method, since it performs
142148
some additional operations.
143149
Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
144-
150+
self.model = model
145151
try:
146152
self.setOutputCRS()
147153
self.resolveTemporaryOutputs()

python/plugins/processing/grass/GrassAlgorithm.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from processing.parameters.ParameterFactory import ParameterFactory
4747
from processing.outputs.OutputFactory import OutputFactory
4848
from processing.core.ProcessingConfig import ProcessingConfig
49-
from processing.tools import dataobjects
49+
from processing.tools import dataobjects, system
5050
from processing.grass.GrassUtils import GrassUtils
5151
from processing.parameters.ParameterSelection import ParameterSelection
5252
from processing.core.WrongHelpFileException import WrongHelpFileException
@@ -55,12 +55,6 @@
5555
from processing.parameters.ParameterNumber import ParameterNumber
5656
from processing.parameters.ParameterString import ParameterString
5757

58-
NUM_EXPORTED = 1
59-
60-
def getNumExportedLayers():
61-
NUM_EXPORTED += 1
62-
return NUM_EXPORTED
63-
6458
class GrassAlgorithm(GeoAlgorithm):
6559

6660
GRASS_OUTPUT_TYPE_PARAMETER = "GRASS_OUTPUT_TYPE_PARAMETER"
@@ -443,7 +437,7 @@ def exportRasterLayer(self, layer):
443437

444438

445439
def getTempFilename(self):
446-
filename = "tmp" + str(time.time()).replace(".","") + str(getNumExportedLayers())
440+
filename = "tmp" + str(time.time()).replace(".","") + str(system.getNumExportedLayers())
447441
return filename
448442

449443
def commandLineName(self):

python/plugins/processing/grass/GrassUtils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26-
import os
26+
import stat
27+
import shutil
2728
from qgis.core import QgsApplication
2829
from PyQt4.QtCore import *
2930
import traceback
@@ -32,8 +33,7 @@
3233
from processing.core.ProcessingConfig import ProcessingConfig
3334
from processing.tools.system import *
3435
from processing.core.ProcessingLog import ProcessingLog
35-
import stat
36-
import shutil
36+
3737

3838
class GrassUtils:
3939

@@ -67,6 +67,12 @@ def grassScriptFilename():
6767
filename = "grass_script.bat"
6868
filename = userFolder() + os.sep + filename
6969
return filename
70+
71+
@staticmethod
72+
def getGrassVersion():
73+
#I do not know if this should be removed or let the user enter it
74+
#or something like that... This is just a temporary thing
75+
return "6.4.0"
7076

7177
@staticmethod
7278
def grassPath():

python/plugins/processing/grass/nviz.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26-
import os
2726
from qgis.core import *
2827
from processing.parameters.ParameterMultipleInput import ParameterMultipleInput
2928
from processing.grass.GrassUtils import GrassUtils
@@ -34,7 +33,6 @@
3433
from processing.parameters.ParameterNumber import ParameterNumber
3534
from processing.parameters.ParameterRaster import ParameterRaster
3635
from processing.tools import dataobjects
37-
import time
3836

3937
class nviz(GeoAlgorithm):
4038

python/plugins/processing/gui/UnthreadedAlgorithmExecutor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
from processing.tools.vector import features
20-
from processing.core.ProcessingLog import ProcessingLog
19+
2120

2221
__author__ = 'Victor Olaya'
2322
__date__ = 'August 2012'
@@ -31,6 +30,8 @@
3130
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
3231
from processing.tools import dataobjects
3332
from processing.tools.system import *
33+
from processing.tools import vector
34+
from processing.core.ProcessingLog import ProcessingLog
3435
from processing.gui.Postprocessing import Postprocessing
3536
from processing.core.SilentProgress import SilentProgress
3637
import traceback
@@ -65,7 +66,7 @@ def runalgIterating(alg,paramToIter,progress):
6566
filelist = []
6667
outputs = {}
6768
provider = layer.dataProvider()
68-
features = features(layer)
69+
features = vector.features(layer)
6970
for feat in features:
7071
output = getTempFilename("shp")
7172
filelist.append(output)

python/plugins/processing/saga/SagaAlgorithm.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
from processing.parameters.ParameterFixedTable import ParameterFixedTable
5252
from processing.core.ProcessingLog import ProcessingLog
5353

54+
sessionExportedLayers = {}
55+
5456
class SagaAlgorithm(GeoAlgorithm):
5557

5658
OUTPUT_EXTENT = "OUTPUT_EXTENT"
@@ -193,7 +195,9 @@ def processAlgorithm(self, progress):
193195
continue
194196
value = param.value
195197
if not value.endswith("sgrd"):
196-
commands.append(self.exportRasterLayer(value))
198+
exportCommand = self.exportRasterLayer(value)
199+
if exportCommand is not None:
200+
commands.append(exportCommand)
197201
if self.resample:
198202
commands.append(self.resampleRasterLayer(value));
199203
if isinstance(param, ParameterVector):
@@ -309,6 +313,7 @@ def processAlgorithm(self, progress):
309313
commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT " + str(formatIndex) +" -TYPE 0 -FILE \"" + filename + "\"");
310314
else:
311315
commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"");
316+
sessionExportedLayers[filename] = filename2
312317

313318

314319
#4 Run SAGA
@@ -377,6 +382,9 @@ def resampleRasterLayer(self,layer):
377382

378383

379384
def exportRasterLayer(self, source):
385+
if source in sessionExportedLayers:
386+
self.exportedLayers[source] = sessionExportedLayers[source]
387+
return None
380388
layer = dataobjects.getObjectFromUri(source, False)
381389
if layer:
382390
filename = str(layer.name())
@@ -388,15 +396,13 @@ def exportRasterLayer(self, source):
388396
filename = "layer"
389397
destFilename = getTempFilenameInTempFolder(filename + ".sgrd")
390398
self.exportedLayers[source]= destFilename
399+
sessionExportedLayers[source] = destFilename
391400
saga208 = ProcessingConfig.getSetting(SagaUtils.SAGA_208)
392401
if isWindows() or isMac() or not saga208:
393402
return "io_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source+"\""
394403
else:
395404
return "libio_gdal 0 -GRIDS \"" + destFilename + "\" -FILES \"" + source + "\""
396405

397-
398-
399-
400406
def checkBeforeOpeningParametersDialog(self):
401407
msg = SagaUtils.checkSagaIsInstalled()
402408
if msg is not None:

python/plugins/processing/tools/system.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ def removeInvalidChars(string):
7777
return string
7878

7979

80-
NUM_EXPORTED = 1
80+
numExported = 1
8181

8282
def getNumExportedLayers():
83-
NUM_EXPORTED += 1
84-
return NUM_EXPORTED
83+
global numExported
84+
numExported += 1
85+
return numExported
8586

8687
def mkdir(newdir):
8788
if os.path.isdir(newdir):

0 commit comments

Comments
 (0)