2014-07-14 14:19:09 +02:00
|
|
|
# -*- 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-07-21 06:55:47 +02:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
__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$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
import sys
|
2017-03-03 20:39:17 +01:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication
|
2018-02-05 22:11:34 -04:00
|
|
|
from qgis.core import (Qgis,
|
2018-02-15 22:30:52 +01:00
|
|
|
QgsFeatureSink,
|
2017-03-03 20:39:17 +01:00
|
|
|
QgsProcessingFeedback,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsProcessingUtils,
|
2017-06-12 13:35:31 +10:00
|
|
|
QgsMessageLog,
|
2017-07-13 09:01:28 +03:00
|
|
|
QgsProcessingException,
|
2018-02-15 22:30:52 +01:00
|
|
|
QgsProcessingParameters)
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
|
|
|
from processing.tools import dataobjects
|
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def execute(alg, parameters, context=None, feedback=None):
|
2014-07-14 14:19:09 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2017-01-07 07:31:31 +10:00
|
|
|
|
|
|
|
if feedback is None:
|
|
|
|
feedback = QgsProcessingFeedback()
|
2017-04-25 14:55:38 +10:00
|
|
|
if context is None:
|
2017-06-23 10:32:51 +10:00
|
|
|
context = dataobjects.createContext(feedback)
|
2017-01-07 07:31:31 +10:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
try:
|
2017-06-23 09:27:39 +10:00
|
|
|
results, ok = alg.run(parameters, context, feedback)
|
|
|
|
return ok, results
|
2017-07-13 09:01:28 +03:00
|
|
|
except QgsProcessingException as e:
|
2018-02-05 22:11:34 -04:00
|
|
|
QgsMessageLog.logMessage(str(sys.exc_info()[0]), 'Processing', Qgis.Critical)
|
2017-01-06 20:04:00 +10:00
|
|
|
if feedback is not None:
|
|
|
|
feedback.reportError(e.msg)
|
2017-05-22 15:46:50 +10:00
|
|
|
return False, {}
|
2014-07-14 14:19:09 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def executeIterating(alg, parameters, paramToIter, context, feedback):
|
2014-07-14 14:19:09 +02:00
|
|
|
# Generate all single-feature layers
|
2017-06-12 13:35:31 +10:00
|
|
|
parameter_definition = alg.parameterDefinition(paramToIter)
|
|
|
|
if not parameter_definition:
|
|
|
|
return False
|
|
|
|
|
|
|
|
iter_source = QgsProcessingParameters.parameterAsSource(parameter_definition, parameters, context)
|
|
|
|
sink_list = []
|
2017-06-12 13:39:33 +10:00
|
|
|
if iter_source.featureCount() == 0:
|
|
|
|
return False
|
|
|
|
|
|
|
|
total = 100.0 / iter_source.featureCount()
|
|
|
|
for current, feat in enumerate(iter_source.getFeatures()):
|
2017-06-12 13:35:31 +10:00
|
|
|
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)
|
2017-06-23 14:34:38 +10:00
|
|
|
sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
2017-06-12 13:35:31 +10:00
|
|
|
del sink
|
2014-07-14 14:19:09 +02:00
|
|
|
|
2017-06-12 13:39:33 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
# store output values to use them later as basenames for all outputs
|
2017-06-12 13:35:31 +10:00
|
|
|
outputs = {}
|
|
|
|
for out in alg.destinationParameterDefinitions():
|
|
|
|
outputs[out.name()] = parameters[out.name()]
|
2014-07-14 14:19:09 +02:00
|
|
|
|
|
|
|
# now run all the algorithms
|
2017-06-12 13:35:31 +10:00
|
|
|
for i, f in enumerate(sink_list):
|
|
|
|
if feedback.isCanceled():
|
|
|
|
return False
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
parameters[paramToIter] = f
|
2017-06-12 13:35:31 +10:00
|
|
|
for out in alg.destinationParameterDefinitions():
|
|
|
|
o = outputs[out.name()]
|
|
|
|
parameters[out.name()] = QgsProcessingUtils.generateIteratingDestination(o, i, context)
|
2018-02-15 22:30:52 +01:00
|
|
|
feedback.setProgressText(QCoreApplication.translate('AlgorithmExecutor', 'Executing iteration {0}/{1}…').format(i, len(sink_list)))
|
2017-06-12 13:35:31 +10:00
|
|
|
feedback.setProgress(i * 100 / len(sink_list))
|
|
|
|
ret, results = execute(alg, parameters, context, feedback)
|
|
|
|
if not ret:
|
2014-07-14 14:19:09 +02:00
|
|
|
return False
|
|
|
|
|
2017-06-12 13:35:31 +10:00
|
|
|
handleAlgorithmResults(alg, context, feedback, False)
|
2014-07-14 14:19:09 +02:00
|
|
|
return True
|
2014-10-03 14:44:01 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-10-03 14:44:01 +03:00
|
|
|
def tr(string, context=''):
|
|
|
|
if context == '':
|
|
|
|
context = 'AlgorithmExecutor'
|
|
|
|
return QCoreApplication.translate(context, string)
|