2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2013-08-12 20:44:27 +02:00
|
|
|
Postprocessing.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
2015-11-08 22:16:32 +01: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$'
|
|
|
|
|
2013-03-30 18:55:46 +01:00
|
|
|
import os
|
2015-11-08 22:16:32 +01:00
|
|
|
import traceback
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QApplication
|
|
|
|
from qgis.PyQt.QtCore import QCoreApplication
|
2017-01-06 20:04:00 +10:00
|
|
|
from qgis.core import (QgsProject,
|
2017-04-24 14:35:50 +10:00
|
|
|
QgsProcessingFeedback,
|
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsMessageLog)
|
2014-10-04 14:04:39 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.ProcessingConfig import ProcessingConfig
|
2017-02-10 13:35:58 +02:00
|
|
|
from processing.core.ProcessingResults import resultsList
|
2014-10-04 14:04:39 +03:00
|
|
|
|
2017-02-10 13:35:58 +02:00
|
|
|
#from processing.gui.ResultsDock import ResultsDock
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.gui.RenderingStyles import RenderingStyles
|
2014-10-04 14:04:39 +03:00
|
|
|
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.outputs import OutputRaster
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
from processing.core.outputs import OutputTable
|
|
|
|
from processing.core.outputs import OutputHTML
|
2014-10-04 14:04:39 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects
|
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
def handleAlgorithmResults(alg, feedback=None, showResults=True):
|
2014-06-08 23:12:13 +02:00
|
|
|
wrongLayers = []
|
2017-01-06 20:04:00 +10:00
|
|
|
if feedback is None:
|
|
|
|
feedback = QgsProcessingFeedback()
|
|
|
|
feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers'))
|
2014-06-08 23:12:13 +02:00
|
|
|
i = 0
|
|
|
|
for out in alg.outputs:
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(100 * i / float(len(alg.outputs)))
|
2014-06-08 23:12:13 +02:00
|
|
|
if out.hidden or not out.open:
|
|
|
|
continue
|
|
|
|
if isinstance(out, (OutputRaster, OutputVector, OutputTable)):
|
|
|
|
try:
|
2015-11-08 17:55:25 +01:00
|
|
|
if hasattr(out, "layer") and out.layer is not None:
|
2016-07-11 08:20:46 +10:00
|
|
|
out.layer.setName(out.description)
|
2016-12-10 15:18:12 +08:00
|
|
|
QgsProject.instance().addMapLayers([out.layer])
|
2014-06-08 23:12:13 +02:00
|
|
|
else:
|
|
|
|
if ProcessingConfig.getSetting(
|
|
|
|
ProcessingConfig.USE_FILENAME_AS_LAYER_NAME):
|
|
|
|
name = os.path.basename(out.value)
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2014-06-08 23:12:13 +02:00
|
|
|
name = out.description
|
|
|
|
dataobjects.load(out.value, name, alg.crs,
|
2017-04-03 20:35:03 +10:00
|
|
|
RenderingStyles.getStyle(alg.id(),
|
2015-08-22 14:29:41 +02:00
|
|
|
out.name))
|
2015-11-08 22:16:32 +01:00
|
|
|
except Exception:
|
2017-04-26 12:52:23 +10:00
|
|
|
QgsMessageLog.logMessage("Error loading result layer:\n" + traceback.format_exc(), 'Processing', QgsMessageLog.CRITICAL)
|
2015-05-20 19:33:47 +02:00
|
|
|
wrongLayers.append(out.description)
|
2014-06-08 23:12:13 +02:00
|
|
|
elif isinstance(out, OutputHTML):
|
2017-03-29 10:42:42 +10:00
|
|
|
resultsList.addResult(alg.icon(), out.description, out.value)
|
2014-06-08 23:12:13 +02:00
|
|
|
i += 1
|
2015-09-29 17:50:53 +02:00
|
|
|
|
|
|
|
QApplication.restoreOverrideCursor()
|
2014-06-08 23:12:13 +02:00
|
|
|
if wrongLayers:
|
2015-05-20 19:33:47 +02:00
|
|
|
msg = "The following layers were not correctly generated.<ul>"
|
|
|
|
msg += "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>"
|
2015-09-29 17:50:53 +02:00
|
|
|
msg += "You can check the log messages to find more information about the execution of the algorithm"
|
2017-01-12 12:54:13 +02:00
|
|
|
feedback.reportError(msg)
|
2015-09-29 17:50:53 +02:00
|
|
|
|
2015-05-20 20:21:34 +02:00
|
|
|
return len(wrongLayers) == 0
|