Skip to content
Permalink
Browse files

Fix running algorithms in iterating mode

  • Loading branch information
nyalldawson committed Jun 12, 2017
1 parent 60ea095 commit b9e0b46f9f9b70abcf0f9f528776c6e38e08a6b9
@@ -228,9 +228,14 @@ def accept(self):
start_time = time.time()

if self.iterateParam:
self.buttonCancel.setEnabled(self.alg.flags() & QgsProcessingAlgorithm.FlagCanCancel)
if executeIterating(self.alg, parameters, self.iterateParam, context, feedback):
feedback.pushInfo(
self.tr('Execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
self.buttonCancel.setEnabled(False)
self.finish(parameters, context, feedback)
else:
self.buttonCancel.setEnabled(False)
QApplication.restoreOverrideCursor()
self.resetGUI()
else:
@@ -28,14 +28,17 @@
__revision__ = '$Format:%H$'

import sys

from copy import deepcopy
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsFeature,
QgsVectorFileWriter,
QgsProcessingFeedback,
QgsSettings,
QgsProcessingUtils,
QgsMessageLog)
QgsMessageLog,
QgsProperty,
QgsProcessingParameters,
QgsProcessingOutputLayerDefinition)
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.tools import dataobjects
@@ -69,41 +72,43 @@ def executeIterating(alg, parameters, paramToIter, context, feedback):
# Generate all single-feature layers
settings = QgsSettings()
systemEncoding = settings.value('/UI/encoding', 'System')
layerfile = parameters[paramToIter]
layer = QgsProcessingUtils.mapLayerFromString(layerfile, context, False)
feat = QgsFeature()
filelist = []
outputs = {}
features = QgsProcessingUtils.getFeatures(layer, context)
for feat in features:
output = getTempFilename('shp')
filelist.append(output)
writer = QgsVectorFileWriter(output, systemEncoding,
layer.fields(), layer.wkbType(), layer.crs())
writer.addFeature(feat)
del writer

parameter_definition = alg.parameterDefinition(paramToIter)
if not parameter_definition:
return False

iter_source = QgsProcessingParameters.parameterAsSource(parameter_definition, parameters, context)
sink_list = []
for feat in iter_source.getFeatures():
if feedback.isCanceled():
return False

sink, sink_id = QgsProcessingUtils.createFeatureSink('memory:', context, iter_source.fields(), iter_source.wkbType(), iter_source.sourceCrs())
sink_list.append(sink_id)
sink.addFeature(feat)
del sink

# store output values to use them later as basenames for all outputs
for out in alg.outputs:
outputs[out.name] = out.value
outputs = {}
for out in alg.destinationParameterDefinitions():
outputs[out.name()] = parameters[out.name()]

# now run all the algorithms
for i, f in enumerate(filelist):
for i, f in enumerate(sink_list):
if feedback.isCanceled():
return False

parameters[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
feedback.setProgressText(tr('Executing iteration {0}/{1}...').format(i, len(filelist)))
feedback.setProgress(i * 100 / len(filelist))
ret, results = execute(alg, parameters, None, feedback)
if ret:
handleAlgorithmResults(alg, context, None, False)
else:
for out in alg.destinationParameterDefinitions():
o = outputs[out.name()]
parameters[out.name()] = QgsProcessingUtils.generateIteratingDestination(o, i, context)
feedback.setProgressText(tr('Executing iteration {0}/{1}...').format(i, len(sink_list)))
feedback.setProgress(i * 100 / len(sink_list))
ret, results = execute(alg, parameters, context, feedback)
if not ret:
return False

handleAlgorithmResults(alg, context, feedback, False)
return True


@@ -56,6 +56,13 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True):
feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers'))
i = 0
for l, details in context.layersToLoadOnCompletion().items():
if feedback.isCanceled():
return False

if len(context.layersToLoadOnCompletion()) > 2:
# only show progress feedback if we're loading a bunch of layers
feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion())))

try:
layer = QgsProcessingUtils.mapLayerFromString(l, context)
if layer is not None:
@@ -98,7 +105,7 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True):
# wrongLayers.append(out.description)
# elif isinstance(out, OutputHTML):
# resultsList.addResult(alg.icon(), out.description, out.value)
# i += 1
i += 1

QApplication.restoreOverrideCursor()
if wrongLayers:

0 comments on commit b9e0b46

Please sign in to comment.
You can’t perform that action at this time.