-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmExecutor.py
More file actions
124 lines (100 loc) · 4.47 KB
/
Copy pathAlgorithmExecutor.py
File metadata and controls
124 lines (100 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
"""
***************************************************************************
AlgorithmExecutor.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from builtins import str
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__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,
QgsProperty,
QgsProcessingException,
QgsProcessingParameters,
QgsProcessingOutputLayerDefinition)
from processing.gui.Postprocessing import handleAlgorithmResults
from processing.tools import dataobjects
from processing.tools.system import getTempFilename
def execute(alg, parameters, context=None, feedback=None):
"""Executes a given algorithm, showing its progress in the
progress object passed along.
Return true if everything went OK, false if the algorithm
could not be completed.
"""
if feedback is None:
feedback = QgsProcessingFeedback()
if context is None:
context = dataobjects.createContext(feedback)
try:
results, ok = alg.run(parameters, context, feedback)
return ok, results
except QgsProcessingException as e:
QgsMessageLog.logMessage(str(sys.exc_info()[0]), 'Processing', QgsMessageLog.CRITICAL)
if feedback is not None:
feedback.reportError(e.msg)
return False, {}
def executeIterating(alg, parameters, paramToIter, context, feedback):
# Generate all single-feature layers
settings = QgsSettings()
systemEncoding = settings.value('/UI/encoding', 'System')
parameter_definition = alg.parameterDefinition(paramToIter)
if not parameter_definition:
return False
iter_source = QgsProcessingParameters.parameterAsSource(parameter_definition, parameters, context)
sink_list = []
if iter_source.featureCount() == 0:
return False
total = 100.0 / iter_source.featureCount()
for current, feat in enumerate(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, QgsFeatureSink.FastInsert)
del sink
feedback.setProgress(int(current * total))
# store output values to use them later as basenames for all outputs
outputs = {}
for out in alg.destinationParameterDefinitions():
outputs[out.name()] = parameters[out.name()]
# now run all the algorithms
for i, f in enumerate(sink_list):
if feedback.isCanceled():
return False
parameters[paramToIter] = f
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
def tr(string, context=''):
if context == '':
context = 'AlgorithmExecutor'
return QCoreApplication.translate(context, string)