|
28 | 28 | __revision__ = '$Format:%H$'
|
29 | 29 |
|
30 | 30 | import sys
|
31 |
| - |
| 31 | +from copy import deepcopy |
32 | 32 | from qgis.PyQt.QtCore import QCoreApplication
|
33 | 33 | from qgis.core import (QgsFeature,
|
34 | 34 | QgsVectorFileWriter,
|
35 | 35 | QgsProcessingFeedback,
|
36 | 36 | QgsSettings,
|
37 | 37 | QgsProcessingUtils,
|
38 |
| - QgsMessageLog) |
| 38 | + QgsMessageLog, |
| 39 | + QgsProperty, |
| 40 | + QgsProcessingParameters, |
| 41 | + QgsProcessingOutputLayerDefinition) |
39 | 42 | from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
40 | 43 | from processing.gui.Postprocessing import handleAlgorithmResults
|
41 | 44 | from processing.tools import dataobjects
|
@@ -69,41 +72,43 @@ def executeIterating(alg, parameters, paramToIter, context, feedback):
|
69 | 72 | # Generate all single-feature layers
|
70 | 73 | settings = QgsSettings()
|
71 | 74 | systemEncoding = settings.value('/UI/encoding', 'System')
|
72 |
| - layerfile = parameters[paramToIter] |
73 |
| - layer = QgsProcessingUtils.mapLayerFromString(layerfile, context, False) |
74 |
| - feat = QgsFeature() |
75 |
| - filelist = [] |
76 |
| - outputs = {} |
77 |
| - features = QgsProcessingUtils.getFeatures(layer, context) |
78 |
| - for feat in features: |
79 |
| - output = getTempFilename('shp') |
80 |
| - filelist.append(output) |
81 |
| - writer = QgsVectorFileWriter(output, systemEncoding, |
82 |
| - layer.fields(), layer.wkbType(), layer.crs()) |
83 |
| - writer.addFeature(feat) |
84 |
| - del writer |
| 75 | + |
| 76 | + parameter_definition = alg.parameterDefinition(paramToIter) |
| 77 | + if not parameter_definition: |
| 78 | + return False |
| 79 | + |
| 80 | + iter_source = QgsProcessingParameters.parameterAsSource(parameter_definition, parameters, context) |
| 81 | + sink_list = [] |
| 82 | + for feat in iter_source.getFeatures(): |
| 83 | + if feedback.isCanceled(): |
| 84 | + return False |
| 85 | + |
| 86 | + sink, sink_id = QgsProcessingUtils.createFeatureSink('memory:', context, iter_source.fields(), iter_source.wkbType(), iter_source.sourceCrs()) |
| 87 | + sink_list.append(sink_id) |
| 88 | + sink.addFeature(feat) |
| 89 | + del sink |
85 | 90 |
|
86 | 91 | # store output values to use them later as basenames for all outputs
|
87 |
| - for out in alg.outputs: |
88 |
| - outputs[out.name] = out.value |
| 92 | + outputs = {} |
| 93 | + for out in alg.destinationParameterDefinitions(): |
| 94 | + outputs[out.name()] = parameters[out.name()] |
89 | 95 |
|
90 | 96 | # now run all the algorithms
|
91 |
| - for i, f in enumerate(filelist): |
| 97 | + for i, f in enumerate(sink_list): |
| 98 | + if feedback.isCanceled(): |
| 99 | + return False |
| 100 | + |
92 | 101 | parameters[paramToIter] = f
|
93 |
| - for out in alg.outputs: |
94 |
| - filename = outputs[out.name] |
95 |
| - if filename: |
96 |
| - filename = filename[:filename.rfind('.')] + '_' + str(i) \ |
97 |
| - + filename[filename.rfind('.'):] |
98 |
| - out.value = filename |
99 |
| - feedback.setProgressText(tr('Executing iteration {0}/{1}...').format(i, len(filelist))) |
100 |
| - feedback.setProgress(i * 100 / len(filelist)) |
101 |
| - ret, results = execute(alg, parameters, None, feedback) |
102 |
| - if ret: |
103 |
| - handleAlgorithmResults(alg, context, None, False) |
104 |
| - else: |
| 102 | + for out in alg.destinationParameterDefinitions(): |
| 103 | + o = outputs[out.name()] |
| 104 | + parameters[out.name()] = QgsProcessingUtils.generateIteratingDestination(o, i, context) |
| 105 | + feedback.setProgressText(tr('Executing iteration {0}/{1}...').format(i, len(sink_list))) |
| 106 | + feedback.setProgress(i * 100 / len(sink_list)) |
| 107 | + ret, results = execute(alg, parameters, context, feedback) |
| 108 | + if not ret: |
105 | 109 | return False
|
106 | 110 |
|
| 111 | + handleAlgorithmResults(alg, context, feedback, False) |
107 | 112 | return True
|
108 | 113 |
|
109 | 114 |
|
|
0 commit comments