File: Processing.py

package info (click to toggle)
qgis 3.40.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,183,672 kB
  • sloc: cpp: 1,595,771; python: 372,544; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,257; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 161
file content (279 lines) | stat: -rw-r--r-- 10,726 bytes parent folder | download | duplicates (6)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
***************************************************************************
    Processing.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.                                   *
*                                                                         *
***************************************************************************
"""

__author__ = "Victor Olaya"
__date__ = "August 2012"
__copyright__ = "(C) 2012, Victor Olaya"

import os
import traceback

from qgis.PyQt.QtCore import Qt, QCoreApplication
from qgis.PyQt.QtWidgets import QApplication
from qgis.PyQt.QtGui import QCursor

from qgis.utils import iface
from qgis.core import (
    QgsMessageLog,
    QgsApplication,
    QgsMapLayer,
    QgsProcessingProvider,
    QgsProcessingAlgorithm,
    QgsProcessingException,
    QgsProcessingParameterDefinition,
    QgsProcessingOutputVectorLayer,
    QgsProcessingOutputRasterLayer,
    QgsProcessingOutputPointCloudLayer,
    QgsProcessingOutputMapLayer,
    QgsProcessingOutputMultipleLayers,
    QgsProcessingFeedback,
    QgsRuntimeProfiler,
)
from qgis.analysis import QgsNativeAlgorithms

import processing
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.MessageBarProgress import MessageBarProgress
from processing.gui.RenderingStyles import RenderingStyles
from processing.gui.AlgorithmExecutor import execute
from processing.script import ScriptUtils
from processing.tools import dataobjects

with QgsRuntimeProfiler.profile("Import QGIS Provider"):
    from processing.algs.qgis.QgisAlgorithmProvider import QgisAlgorithmProvider  # NOQA

with QgsRuntimeProfiler.profile("Import GDAL Provider"):
    from processing.algs.gdal.GdalAlgorithmProvider import GdalAlgorithmProvider  # NOQA

with QgsRuntimeProfiler.profile("Import Script Provider"):
    from processing.script.ScriptAlgorithmProvider import (
        ScriptAlgorithmProvider,
    )  # NOQA

# should be loaded last - ensures that all dependent algorithms are available when loading models
from processing.modeler.ModelerAlgorithmProvider import ModelerAlgorithmProvider  # NOQA
from processing.modeler.ProjectProvider import ProjectProvider  # NOQA


class Processing:
    BASIC_PROVIDERS = []

    @staticmethod
    def activateProvider(providerOrName, activate=True):
        provider_id = (
            providerOrName.id()
            if isinstance(providerOrName, QgsProcessingProvider)
            else providerOrName
        )
        provider = QgsApplication.processingRegistry().providerById(provider_id)
        try:
            provider.setActive(True)
            provider.refreshAlgorithms()
        except:
            # provider could not be activated
            QgsMessageLog.logMessage(
                Processing.tr("Error: Provider {0} could not be activated\n").format(
                    provider_id
                ),
                Processing.tr("Processing"),
            )

    @staticmethod
    def initialize():
        if "script" in [
            p.id() for p in QgsApplication.processingRegistry().providers()
        ]:
            return

        with QgsRuntimeProfiler.profile("Initialize"):

            # add native provider if not already added
            if "native" not in [
                p.id() for p in QgsApplication.processingRegistry().providers()
            ]:
                QgsApplication.processingRegistry().addProvider(
                    QgsNativeAlgorithms(QgsApplication.processingRegistry())
                )

            # add 3d provider if available and not already added
            if "3d" not in [
                p.id() for p in QgsApplication.processingRegistry().providers()
            ]:
                try:
                    from qgis._3d import Qgs3DAlgorithms

                    QgsApplication.processingRegistry().addProvider(
                        Qgs3DAlgorithms(QgsApplication.processingRegistry())
                    )
                except ImportError:
                    # no 3d library available
                    pass

            # Add the basic providers
            basic_providers = [
                QgisAlgorithmProvider,
                GdalAlgorithmProvider,
                ScriptAlgorithmProvider,
            ]

            # model providers are deferred for qgis_process startup
            if QgsApplication.platform() != "qgis_process":
                basic_providers.extend([ModelerAlgorithmProvider, ProjectProvider])

            for c in basic_providers:
                p = c()
                if QgsApplication.processingRegistry().addProvider(p):
                    Processing.BASIC_PROVIDERS.append(p)

            if QgsApplication.platform() == "external":
                # for external applications we must also load the builtin providers stored in separate plugins
                try:
                    from grassprovider.grass_provider import GrassProvider

                    p = GrassProvider()
                    if QgsApplication.processingRegistry().addProvider(p):
                        Processing.BASIC_PROVIDERS.append(p)
                except ImportError:
                    pass

            # And initialize
            ProcessingConfig.initialize()
            ProcessingConfig.readSettings()
            RenderingStyles.loadStyles()

    @staticmethod
    def perform_deferred_model_initialization():
        if "model" in [p.id() for p in QgsApplication.processingRegistry().providers()]:
            return

        # Add the model providers
        # note that we don't add the Project Provider, as this cannot be called
        # from qgis_process
        model_providers = [ModelerAlgorithmProvider]

        for c in model_providers:
            p = c()
            if QgsApplication.processingRegistry().addProvider(p):
                Processing.BASIC_PROVIDERS.append(p)

    @staticmethod
    def deinitialize():
        for p in Processing.BASIC_PROVIDERS:
            QgsApplication.processingRegistry().removeProvider(p)

        Processing.BASIC_PROVIDERS = []

    @staticmethod
    def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=None):
        if isinstance(algOrName, QgsProcessingAlgorithm):
            alg = algOrName
        else:
            alg = QgsApplication.processingRegistry().createAlgorithmById(algOrName)

        if feedback is None:
            feedback = QgsProcessingFeedback()

        if alg is None:
            msg = Processing.tr("Error: Algorithm {0} not found\n").format(algOrName)
            feedback.reportError(msg)
            raise QgsProcessingException(msg)

        if context is None:
            context = dataobjects.createContext(feedback)

        if context.feedback() is None:
            context.setFeedback(feedback)

        ok, msg = alg.checkParameterValues(parameters, context)
        if not ok:
            msg = Processing.tr("Unable to execute algorithm\n{0}").format(msg)
            feedback.reportError(msg)
            raise QgsProcessingException(msg)

        if not alg.validateInputCrs(parameters, context):
            feedback.pushInfo(
                Processing.tr(
                    "Warning: Not all input layers use the same CRS.\nThis can cause unexpected results."
                )
            )

        ret, results = execute(
            alg, parameters, context, feedback, catch_exceptions=False
        )
        if ret:
            feedback.pushInfo(Processing.tr("Results: {}").format(results))

            if onFinish is not None:
                onFinish(alg, context, feedback)
            else:
                # auto convert layer references in results to map layers
                for out in alg.outputDefinitions():
                    if out.name() not in results:
                        continue

                    if isinstance(
                        out,
                        (
                            QgsProcessingOutputVectorLayer,
                            QgsProcessingOutputRasterLayer,
                            QgsProcessingOutputPointCloudLayer,
                            QgsProcessingOutputMapLayer,
                        ),
                    ):
                        result = results[out.name()]
                        if not isinstance(result, QgsMapLayer):
                            layer = context.takeResultLayer(
                                result
                            )  # transfer layer ownership out of context
                            if layer:
                                results[out.name()] = (
                                    layer  # replace layer string ref with actual layer (+ownership)
                                )
                    elif isinstance(out, QgsProcessingOutputMultipleLayers):
                        result = results[out.name()]
                        if result:
                            layers_result = []
                            for l in result:
                                if not isinstance(l, QgsMapLayer):
                                    layer = context.takeResultLayer(
                                        l
                                    )  # transfer layer ownership out of context
                                    if layer:
                                        layers_result.append(layer)
                                    else:
                                        layers_result.append(l)
                                else:
                                    layers_result.append(l)

                            results[out.name()] = (
                                layers_result  # replace layers strings ref with actual layers (+ownership)
                            )

        else:
            msg = Processing.tr("There were errors executing the algorithm.")
            feedback.reportError(msg)
            raise QgsProcessingException(msg)

        if isinstance(feedback, MessageBarProgress):
            feedback.close()
        return results

    @staticmethod
    def tr(string, context=""):
        if context == "":
            context = "Processing"
        return QCoreApplication.translate(context, string)