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-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-07-14 14:19:09 +02:00
|
|
|
|
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
|
2017-01-06 20:04:00 +10:00
|
|
|
from qgis.core import (QgsFeature,
|
|
|
|
QgsVectorFileWriter,
|
2017-03-03 20:39:17 +01:00
|
|
|
QgsProcessingFeedback,
|
2017-04-25 17:59:35 +10:00
|
|
|
QgsSettings,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsMessageLog)
|
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
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
|
|
|
from processing.tools import dataobjects
|
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
|
|
|
from processing.tools.system import getTempFilename
|
2014-07-14 14:19:09 +02:00
|
|
|
|
2016-08-04 09:10:08 +02:00
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def execute(alg, 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-04-25 20:03:39 +10:00
|
|
|
context = dataobjects.createContext()
|
2017-01-07 07:31:31 +10:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
try:
|
2017-04-25 14:55:38 +10:00
|
|
|
alg.execute(context, feedback)
|
2014-07-14 14:19:09 +02:00
|
|
|
return True
|
2015-07-09 14:41:00 +02:00
|
|
|
except GeoAlgorithmExecutionException as e:
|
2017-05-01 16:59:03 +10:00
|
|
|
QgsMessageLog.logMessage(str(sys.exc_info()[0]), 'Processing', QgsMessageLog.CRITICAL)
|
2017-01-06 20:04:00 +10:00
|
|
|
if feedback is not None:
|
|
|
|
feedback.reportError(e.msg)
|
2014-07-14 14:19:09 +02:00
|
|
|
return False
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-04-25 15:57:24 +10:00
|
|
|
def executeIterating(alg, paramToIter, context, feedback):
|
2014-07-14 14:19:09 +02:00
|
|
|
# Generate all single-feature layers
|
2017-03-03 20:39:17 +01:00
|
|
|
settings = QgsSettings()
|
2014-07-14 14:19:09 +02:00
|
|
|
systemEncoding = settings.value('/UI/encoding', 'System')
|
|
|
|
layerfile = alg.getParameterValue(paramToIter)
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(layerfile, context, False)
|
2014-07-14 14:19:09 +02:00
|
|
|
feat = QgsFeature()
|
|
|
|
filelist = []
|
|
|
|
outputs = {}
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2014-07-14 14:19:09 +02:00
|
|
|
for feat in features:
|
|
|
|
output = getTempFilename('shp')
|
|
|
|
filelist.append(output)
|
|
|
|
writer = QgsVectorFileWriter(output, systemEncoding,
|
2016-08-05 15:05:36 +03:00
|
|
|
layer.fields(), layer.wkbType(), layer.crs())
|
2014-07-14 14:19:09 +02:00
|
|
|
writer.addFeature(feat)
|
|
|
|
del writer
|
|
|
|
|
|
|
|
# store output values to use them later as basenames for all outputs
|
|
|
|
for out in alg.outputs:
|
|
|
|
outputs[out.name] = out.value
|
|
|
|
|
|
|
|
# now run all the algorithms
|
2015-08-22 14:29:41 +02:00
|
|
|
for i, f in enumerate(filelist):
|
2014-07-14 14:19:09 +02:00
|
|
|
alg.setParameterValue(paramToIter, f)
|
|
|
|
for out in alg.outputs:
|
|
|
|
filename = outputs[out.name]
|
|
|
|
if filename:
|
2016-09-21 18:24:26 +02:00
|
|
|
filename = filename[:filename.rfind('.')] + '_' + str(i) \
|
2014-07-14 14:19:09 +02:00
|
|
|
+ filename[filename.rfind('.'):]
|
|
|
|
out.value = filename
|
2017-03-04 16:23:36 +01:00
|
|
|
feedback.setProgressText(tr('Executing iteration {0}/{1}...').format(i, len(filelist)))
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(i * 100 / len(filelist))
|
2017-04-25 14:55:38 +10:00
|
|
|
if execute(alg, None, feedback):
|
2017-04-26 14:33:53 +10:00
|
|
|
handleAlgorithmResults(alg, context, None, False)
|
2014-07-14 14:19:09 +02:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
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)
|