2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2013-08-12 20:44:27 +02:00
|
|
|
Processing.py
|
2012-10-05 23:28:47 +02:00
|
|
|
---------------------
|
|
|
|
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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2014-08-22 11:27:57 +02:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-05-23 16:57:59 +02:00
|
|
|
import os
|
2016-04-26 08:29:15 +02:00
|
|
|
import traceback
|
2014-10-03 14:44:01 +03:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
from qgis.PyQt.QtCore import Qt, QCoreApplication
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QApplication
|
|
|
|
from qgis.PyQt.QtGui import QCursor
|
2014-10-03 14:44:01 +03:00
|
|
|
|
2014-05-20 16:30:59 +02:00
|
|
|
from qgis.utils import iface
|
2017-01-09 09:59:35 +10:00
|
|
|
from qgis.core import (QgsMessageLog,
|
2017-04-04 12:58:25 +10:00
|
|
|
QgsApplication,
|
2017-07-01 20:04:36 +10:00
|
|
|
QgsMapLayer,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsProcessingProvider,
|
2017-06-27 11:19:29 +10:00
|
|
|
QgsProcessingAlgorithm,
|
2017-07-13 09:01:28 +03:00
|
|
|
QgsProcessingException,
|
2017-07-01 20:04:36 +10:00
|
|
|
QgsProcessingParameterDefinition,
|
|
|
|
QgsProcessingOutputVectorLayer,
|
|
|
|
QgsProcessingOutputRasterLayer)
|
2014-10-03 14:44:01 +03:00
|
|
|
|
|
|
|
import processing
|
2016-05-23 16:57:59 +02:00
|
|
|
from processing.script.ScriptUtils import ScriptUtils
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.gui.MessageBarProgress import MessageBarProgress
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.gui.RenderingStyles import RenderingStyles
|
2014-06-08 23:15:33 +02:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
2017-03-22 17:17:14 +02:00
|
|
|
from processing.gui.AlgorithmExecutor import execute
|
2017-04-25 20:03:39 +10:00
|
|
|
from processing.tools import dataobjects
|
2016-05-19 12:25:04 +02:00
|
|
|
|
2017-05-22 15:46:50 +10:00
|
|
|
from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider # NOQA
|
2017-05-19 10:17:59 +10:00
|
|
|
#from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider # NOQA
|
|
|
|
#from processing.algs.gdal.GdalAlgorithmProvider import GdalAlgorithmProvider # NOQA
|
|
|
|
#from processing.algs.saga.SagaAlgorithmProvider import SagaAlgorithmProvider # NOQA
|
2017-06-27 10:50:07 +10:00
|
|
|
from processing.script.ScriptAlgorithmProvider import ScriptAlgorithmProvider # NOQA
|
2017-05-19 10:17:59 +10:00
|
|
|
#from processing.preconfigured.PreconfiguredAlgorithmProvider import PreconfiguredAlgorithmProvider # NOQA
|
2016-04-27 15:40:56 +03:00
|
|
|
|
2017-06-24 13:49:10 +10:00
|
|
|
# should be loaded last - ensures that all dependent algorithms are available when loading models
|
|
|
|
from processing.modeler.ModelerAlgorithmProvider import ModelerAlgorithmProvider # NOQA
|
|
|
|
|
2016-05-27 19:04:25 +03:00
|
|
|
|
2016-09-21 18:24:26 +02:00
|
|
|
class Processing(object):
|
2017-04-04 14:34:00 +10:00
|
|
|
BASIC_PROVIDERS = []
|
|
|
|
|
2016-06-02 09:45:20 +02:00
|
|
|
@staticmethod
|
|
|
|
def activateProvider(providerOrName, activate=True):
|
2017-04-04 12:58:25 +10:00
|
|
|
provider_id = providerOrName.id() if isinstance(providerOrName, QgsProcessingProvider) else providerOrName
|
2017-04-04 11:28:28 +10:00
|
|
|
provider = QgsApplication.processingRegistry().providerById(provider_id)
|
2017-04-04 12:22:29 +10:00
|
|
|
try:
|
|
|
|
provider.setActive(True)
|
2017-04-04 11:28:28 +10:00
|
|
|
provider.refreshAlgorithms()
|
2017-04-04 12:22:29 +10:00
|
|
|
except:
|
|
|
|
# provider could not be activated
|
|
|
|
QgsMessageLog.logMessage(Processing.tr('Error: Provider {0} could not be activated\n').format(provider_id),
|
|
|
|
Processing.tr("Processing"))
|
2016-06-02 09:45:20 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
@staticmethod
|
|
|
|
def initialize():
|
2017-04-04 11:04:36 +10:00
|
|
|
if "model" in [p.id() for p in QgsApplication.processingRegistry().providers()]:
|
2016-05-23 16:57:59 +02:00
|
|
|
return
|
2013-10-01 20:52:22 +03:00
|
|
|
# Add the basic providers
|
2017-04-04 12:58:25 +10:00
|
|
|
for c in QgsProcessingProvider.__subclasses__():
|
2017-04-04 13:28:44 +10:00
|
|
|
p = c()
|
2017-04-04 14:34:00 +10:00
|
|
|
Processing.BASIC_PROVIDERS.append(p)
|
2017-04-04 13:28:44 +10:00
|
|
|
QgsApplication.processingRegistry().addProvider(p)
|
2013-10-01 20:52:22 +03:00
|
|
|
# And initialize
|
2013-08-12 20:44:27 +02:00
|
|
|
ProcessingConfig.initialize()
|
2014-04-23 18:24:23 +02:00
|
|
|
ProcessingConfig.readSettings()
|
2013-02-28 22:08:32 +01:00
|
|
|
RenderingStyles.loadStyles()
|
2016-05-27 19:04:25 +03:00
|
|
|
|
2017-04-04 13:28:44 +10:00
|
|
|
@staticmethod
|
|
|
|
def deinitialize():
|
2017-04-04 14:34:00 +10:00
|
|
|
for p in Processing.BASIC_PROVIDERS:
|
2017-04-04 13:28:44 +10:00
|
|
|
QgsApplication.processingRegistry().removeProvider(p)
|
|
|
|
|
2017-04-04 14:34:00 +10:00
|
|
|
Processing.BASIC_PROVIDERS = []
|
|
|
|
|
2016-05-27 19:04:25 +03:00
|
|
|
@staticmethod
|
2016-05-23 16:57:59 +02:00
|
|
|
def addScripts(folder):
|
|
|
|
Processing.initialize()
|
2017-01-09 09:59:35 +10:00
|
|
|
provider = QgsApplication.processingRegistry().providerById("qgis")
|
2016-05-23 16:57:59 +02:00
|
|
|
scripts = ScriptUtils.loadFromFolder(folder)
|
2016-08-04 09:10:08 +02:00
|
|
|
# fix_print_with_import
|
|
|
|
print(scripts)
|
2016-05-23 16:57:59 +02:00
|
|
|
for script in scripts:
|
|
|
|
script.allowEdit = False
|
|
|
|
script._icon = provider._icon
|
2016-05-27 19:04:25 +03:00
|
|
|
provider.externalAlgs.extend(scripts)
|
2017-04-04 11:28:28 +10:00
|
|
|
provider.refreshAlgorithms()
|
2016-05-27 19:04:25 +03:00
|
|
|
|
2016-05-24 10:27:38 +02:00
|
|
|
@staticmethod
|
2016-05-23 16:57:59 +02:00
|
|
|
def removeScripts(folder):
|
2017-01-09 09:59:35 +10:00
|
|
|
provider = QgsApplication.processingRegistry().providerById("qgis")
|
2016-05-24 10:27:38 +02:00
|
|
|
for alg in provider.externalAlgs[::-1]:
|
|
|
|
path = os.path.dirname(alg.descriptionFile)
|
|
|
|
if path == folder:
|
2016-05-27 19:04:25 +03:00
|
|
|
provider.externalAlgs.remove(alg)
|
2017-04-04 11:28:28 +10:00
|
|
|
provider.refreshAlgorithms()
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
@staticmethod
|
2017-06-27 12:44:14 +10:00
|
|
|
def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=None):
|
2017-06-27 11:19:29 +10:00
|
|
|
if isinstance(algOrName, QgsProcessingAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
alg = algOrName
|
|
|
|
else:
|
2017-07-10 16:31:14 +10:00
|
|
|
alg = QgsApplication.processingRegistry().createAlgorithmById(algOrName)
|
2017-06-27 12:44:14 +10:00
|
|
|
|
|
|
|
if feedback is None:
|
|
|
|
feedback = MessageBarProgress(alg.displayName() if alg else Processing.tr('Processing'))
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
if alg is None:
|
2016-08-04 09:10:08 +02:00
|
|
|
# fix_print_with_import
|
|
|
|
print('Error: Algorithm not found\n')
|
2017-06-27 12:44:14 +10:00
|
|
|
msg = Processing.tr('Error: Algorithm {0} not found\n').format(algOrName)
|
|
|
|
feedback.reportError(msg)
|
2017-07-13 09:01:28 +03:00
|
|
|
raise QgsProcessingException(msg)
|
2014-07-02 07:46:03 +02:00
|
|
|
|
2017-06-27 12:44:14 +10:00
|
|
|
# check for any mandatory parameters which were not specified
|
2017-06-27 11:24:33 +10:00
|
|
|
for param in alg.parameterDefinitions():
|
|
|
|
if param.name() not in parameters:
|
|
|
|
if not param.flags() & QgsProcessingParameterDefinition.FlagOptional:
|
|
|
|
# fix_print_with_import
|
2017-06-27 12:44:14 +10:00
|
|
|
msg = Processing.tr('Error: Missing parameter value for parameter {0}.').format(param.name())
|
2017-06-27 11:24:33 +10:00
|
|
|
print('Error: Missing parameter value for parameter %s.' % param.name())
|
2017-06-27 12:44:14 +10:00
|
|
|
feedback.reportError(msg)
|
2017-07-13 09:01:28 +03:00
|
|
|
raise QgsProcessingException(msg)
|
2017-06-23 10:32:51 +10:00
|
|
|
|
2017-06-27 11:24:33 +10:00
|
|
|
if context is None:
|
2017-06-23 10:32:51 +10:00
|
|
|
context = dataobjects.createContext(feedback)
|
2017-04-29 12:14:53 +02:00
|
|
|
|
2017-05-16 15:21:41 +10:00
|
|
|
ok, msg = alg.checkParameterValues(parameters, context)
|
|
|
|
if not ok:
|
2016-08-04 09:10:08 +02:00
|
|
|
# fix_print_with_import
|
2016-09-21 18:24:26 +02:00
|
|
|
print('Unable to execute algorithm\n' + str(msg))
|
2017-06-27 12:44:14 +10:00
|
|
|
msg = Processing.tr('Unable to execute algorithm\n{0}').format(msg)
|
|
|
|
feedback.reportError(msg)
|
2017-07-13 09:01:28 +03:00
|
|
|
raise QgsProcessingException(msg)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-06-09 16:55:31 +10:00
|
|
|
if not alg.validateInputCrs(parameters, context):
|
2017-03-04 19:41:23 +01:00
|
|
|
print('Warning: Not all input layers use the same CRS.\n' +
|
|
|
|
'This can cause unexpected results.')
|
2017-06-27 12:44:14 +10:00
|
|
|
feedback.pushInfo(
|
|
|
|
Processing.tr('Warning: Not all input layers use the same CRS.\nThis can cause unexpected results.'))
|
2013-02-07 01:09:39 +01:00
|
|
|
|
2017-05-22 15:46:50 +10:00
|
|
|
ret, results = execute(alg, parameters, context, feedback)
|
2015-06-23 08:30:17 +02:00
|
|
|
if ret:
|
2017-06-27 12:44:14 +10:00
|
|
|
feedback.pushInfo(
|
|
|
|
Processing.tr('Results: {}').format(results))
|
|
|
|
|
2015-06-23 08:30:17 +02:00
|
|
|
if onFinish is not None:
|
2017-04-26 14:33:53 +10:00
|
|
|
onFinish(alg, context, feedback)
|
2017-07-01 20:04:36 +10:00
|
|
|
else:
|
|
|
|
# auto convert layer references in results to map layers
|
|
|
|
for out in alg.outputDefinitions():
|
|
|
|
if isinstance(out, (QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer)):
|
|
|
|
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)
|
2015-06-23 08:30:17 +02:00
|
|
|
else:
|
2017-06-27 12:44:14 +10:00
|
|
|
msg = Processing.tr("There were errors executing the algorithm.")
|
|
|
|
feedback.reportError(msg)
|
2017-07-13 09:01:28 +03:00
|
|
|
raise QgsProcessingException(msg)
|
2014-10-03 10:35:07 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
if isinstance(feedback, MessageBarProgress):
|
|
|
|
feedback.close()
|
2017-06-12 15:59:56 +10:00
|
|
|
return results
|
2014-10-03 14:44:01 +03:00
|
|
|
|
2014-10-09 10:51:50 +03:00
|
|
|
@staticmethod
|
|
|
|
def tr(string, context=''):
|
2014-10-03 14:44:01 +03:00
|
|
|
if context == '':
|
|
|
|
context = 'Processing'
|
2014-10-09 10:51:50 +03:00
|
|
|
return QCoreApplication.translate(context, string)
|