2014-11-12 19:36:12 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
BatchAlgorithmDialog.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 range
|
2014-11-12 19:36:12 +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$'
|
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
from pprint import pformat
|
|
|
|
import time
|
|
|
|
|
2016-09-27 19:51:06 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QApplication, QMessageBox, QSizePolicy
|
|
|
|
from qgis.PyQt.QtGui import QCursor
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import Qt
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
from qgis.core import (QgsProcessingParameterDefinition,
|
2017-07-08 15:25:45 +10:00
|
|
|
QgsProcessingParameterRasterDestination,
|
2017-06-13 12:32:30 +10:00
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputLayerDefinition,
|
|
|
|
QgsProcessingOutputHtml,
|
|
|
|
QgsProcessingOutputNumber,
|
2017-06-13 12:47:11 +10:00
|
|
|
QgsProcessingOutputString,
|
2017-06-13 12:32:30 +10:00
|
|
|
QgsProject)
|
|
|
|
|
2016-09-16 14:32:00 +02:00
|
|
|
from qgis.gui import QgsMessageBar
|
2017-11-10 11:33:58 +10:00
|
|
|
from qgis.utils import OverrideCursor
|
2016-09-16 14:32:00 +02:00
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
from processing.gui.BatchPanel import BatchPanel
|
|
|
|
from processing.gui.AlgorithmDialogBase import AlgorithmDialogBase
|
2017-03-22 17:17:14 +02:00
|
|
|
from processing.gui.AlgorithmExecutor import execute
|
2014-11-12 19:36:12 +02:00
|
|
|
from processing.gui.Postprocessing import handleAlgorithmResults
|
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
from processing.core.ProcessingResults import resultsList
|
2014-11-12 19:36:12 +02:00
|
|
|
|
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
|
2017-04-26 14:33:53 +10:00
|
|
|
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
|
|
|
|
|
|
|
import codecs
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
class BatchAlgorithmDialog(AlgorithmDialogBase):
|
|
|
|
|
|
|
|
def __init__(self, alg):
|
|
|
|
AlgorithmDialogBase.__init__(self, alg)
|
|
|
|
|
|
|
|
self.alg = alg
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
self.setWindowTitle(self.tr('Batch Processing - {0}').format(self.alg.displayName()))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2016-09-14 09:51:17 +02:00
|
|
|
self.setMainWidget(BatchPanel(self, self.alg))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2016-01-08 08:27:22 +01:00
|
|
|
self.textShortHelp.setVisible(False)
|
|
|
|
|
2016-09-16 14:32:00 +02:00
|
|
|
self.bar = QgsMessageBar()
|
|
|
|
self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
|
|
|
|
self.layout().insertWidget(0, self.bar)
|
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
def accept(self):
|
2017-06-13 12:32:30 +10:00
|
|
|
alg_parameters = []
|
|
|
|
load = []
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
feedback = self.createFeedback()
|
2017-05-16 15:21:41 +10:00
|
|
|
|
2014-11-12 19:36:12 +02:00
|
|
|
for row in range(self.mainWidget.tblParameters.rowCount()):
|
|
|
|
col = 0
|
2017-05-15 16:19:46 +10:00
|
|
|
parameters = {}
|
|
|
|
for param in self.alg.parameterDefinitions():
|
|
|
|
if param.flags() & QgsProcessingParameterDefinition.FlagHidden or param.isDestination():
|
2014-11-12 19:36:12 +02:00
|
|
|
continue
|
2016-09-16 14:32:00 +02:00
|
|
|
wrapper = self.mainWidget.wrappers[row][col]
|
2017-05-15 16:19:46 +10:00
|
|
|
parameters[param.name()] = wrapper.value()
|
2017-11-10 14:34:34 +10:00
|
|
|
if not param.checkValueIsAcceptable(wrapper.value()):
|
2017-03-04 16:23:36 +01:00
|
|
|
self.bar.pushMessage("", self.tr('Wrong or missing parameter value: {0} (row {1})').format(
|
2017-05-16 15:21:41 +10:00
|
|
|
param.description(), row + 1),
|
2016-09-19 11:52:31 +03:00
|
|
|
level=QgsMessageBar.WARNING, duration=5)
|
2014-11-12 19:36:12 +02:00
|
|
|
return
|
|
|
|
col += 1
|
2017-06-13 12:32:30 +10:00
|
|
|
count_visible_outputs = 0
|
|
|
|
for out in self.alg.destinationParameterDefinitions():
|
2017-05-16 16:36:00 +10:00
|
|
|
if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
|
2014-11-12 19:36:12 +02:00
|
|
|
continue
|
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
count_visible_outputs += 1
|
2014-11-12 19:36:12 +02:00
|
|
|
widget = self.mainWidget.tblParameters.cellWidget(row, col)
|
|
|
|
text = widget.getValue()
|
2017-11-10 14:34:34 +10:00
|
|
|
if out.checkValueIsAcceptable(text):
|
2017-07-08 15:25:45 +10:00
|
|
|
if isinstance(out, (QgsProcessingParameterRasterDestination,
|
2017-06-13 12:32:30 +10:00
|
|
|
QgsProcessingParameterFeatureSink)):
|
|
|
|
# load rasters and sinks on completion
|
2017-11-10 14:34:34 +10:00
|
|
|
parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, QgsProject.instance())
|
2017-06-13 12:32:30 +10:00
|
|
|
else:
|
|
|
|
parameters[out.name()] = text
|
2014-11-12 19:36:12 +02:00
|
|
|
col += 1
|
|
|
|
else:
|
2017-03-04 16:23:36 +01:00
|
|
|
self.bar.pushMessage("", self.tr('Wrong or missing output value: {0} (row {1})').format(
|
2017-05-16 16:36:00 +10:00
|
|
|
out.description(), row + 1),
|
2016-09-16 14:32:00 +02:00
|
|
|
level=QgsMessageBar.WARNING, duration=5)
|
2014-11-12 19:36:12 +02:00
|
|
|
return
|
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
alg_parameters.append(parameters)
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-11-10 11:33:58 +10:00
|
|
|
with OverrideCursor(Qt.WaitCursor):
|
|
|
|
|
|
|
|
self.mainWidget.setEnabled(False)
|
|
|
|
self.buttonCancel.setEnabled(True)
|
|
|
|
|
|
|
|
# Make sure the Log tab is visible before executing the algorithm
|
|
|
|
try:
|
|
|
|
self.tabWidget.setCurrentIndex(1)
|
|
|
|
self.repaint()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
|
|
|
algorithm_results = []
|
|
|
|
for count, parameters in enumerate(alg_parameters):
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
self.setText(self.tr('\nProcessing algorithm {0}/{1}...').format(count + 1, len(alg_parameters)))
|
|
|
|
self.setInfo(self.tr('<b>Algorithm {0} starting...</b>').format(self.alg.displayName()), escape_html=False)
|
|
|
|
|
|
|
|
feedback.pushInfo(self.tr('Input parameters:'))
|
|
|
|
feedback.pushCommandInfo(pformat(parameters))
|
2017-06-13 12:32:30 +10:00
|
|
|
feedback.pushInfo('')
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-11-10 14:34:34 +10:00
|
|
|
# important - we create a new context for each iteration
|
|
|
|
# this avoids holding onto resources and layers from earlier iterations,
|
|
|
|
# and allows batch processing of many more items then is possible
|
|
|
|
# if we hold on to these layers
|
|
|
|
context = dataobjects.createContext(feedback)
|
|
|
|
|
2017-11-10 11:33:58 +10:00
|
|
|
alg_start_time = time.time()
|
|
|
|
ret, results = execute(self.alg, parameters, context, feedback)
|
|
|
|
if ret:
|
|
|
|
self.setInfo(self.tr('Algorithm {0} correctly executed...').format(self.alg.displayName()), escape_html=False)
|
|
|
|
feedback.setProgress(100)
|
|
|
|
feedback.pushInfo(
|
|
|
|
self.tr('Execution completed in {0:0.2f} seconds'.format(time.time() - alg_start_time)))
|
|
|
|
feedback.pushInfo(self.tr('Results:'))
|
|
|
|
feedback.pushCommandInfo(pformat(results))
|
|
|
|
feedback.pushInfo('')
|
|
|
|
algorithm_results.append(results)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2017-11-10 14:34:34 +10:00
|
|
|
handleAlgorithmResults(self.alg, context, feedback, False)
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-11-10 14:34:34 +10:00
|
|
|
feedback.pushInfo(self.tr('Batch execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
self.finish(algorithm_results)
|
|
|
|
self.buttonCancel.setEnabled(False)
|
|
|
|
|
|
|
|
def finish(self, algorithm_results):
|
|
|
|
for count, results in enumerate(algorithm_results):
|
|
|
|
self.loadHTMLResults(results, count)
|
|
|
|
|
|
|
|
self.createSummaryTable(algorithm_results)
|
2014-11-12 19:36:12 +02:00
|
|
|
self.mainWidget.setEnabled(True)
|
|
|
|
QMessageBox.information(self, self.tr('Batch processing'),
|
2015-09-29 17:51:30 +02:00
|
|
|
self.tr('Batch processing completed'))
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
def loadHTMLResults(self, results, num):
|
|
|
|
for out in self.alg.outputDefinitions():
|
|
|
|
if isinstance(out, QgsProcessingOutputHtml) and out.name() in results and results[out.name()]:
|
|
|
|
resultsList.addResult(icon=self.alg.icon(), name='{} [{}]'.format(out.description(), num),
|
|
|
|
result=results[out.name()])
|
2014-11-12 19:36:12 +02:00
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
def createSummaryTable(self, algorithm_results):
|
2014-11-12 19:36:12 +02:00
|
|
|
createTable = False
|
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
for out in self.alg.outputDefinitions():
|
|
|
|
if isinstance(out, (QgsProcessingOutputNumber, QgsProcessingOutputString)):
|
2014-11-12 19:36:12 +02:00
|
|
|
createTable = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not createTable:
|
|
|
|
return
|
|
|
|
|
|
|
|
outputFile = getTempFilename('html')
|
|
|
|
with codecs.open(outputFile, 'w', encoding='utf-8') as f:
|
2017-06-13 12:32:30 +10:00
|
|
|
for res in algorithm_results:
|
2014-11-12 19:36:12 +02:00
|
|
|
f.write('<hr>\n')
|
2017-06-13 12:32:30 +10:00
|
|
|
for out in self.alg.outputDefinitions():
|
|
|
|
if isinstance(out, (QgsProcessingOutputNumber, QgsProcessingOutputString)) and out.name() in res:
|
|
|
|
f.write('<p>{}: {}</p>\n'.format(out.description(), res[out.name()]))
|
2014-11-12 19:36:12 +02:00
|
|
|
f.write('<hr>\n')
|
|
|
|
|
2017-06-13 12:32:30 +10:00
|
|
|
resultsList.addResult(self.alg.icon(),
|
|
|
|
'{} [summary]'.format(self.alg.name()), outputFile)
|