QGIS/python/plugins/processing/gui/Postprocessing.py

142 lines
6.3 KiB
Python
Raw Normal View History

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. *
* *
***************************************************************************
"""
2012-10-05 23:28:47 +02:00
__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
import os
import traceback
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtWidgets import QApplication
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (Qgis,
QgsProject,
QgsProcessingFeedback,
QgsProcessingUtils,
2019-03-27 07:20:43 +10:00
QgsMapLayerType,
2017-06-13 12:47:11 +10:00
QgsWkbTypes,
QgsMessageLog,
QgsProviderRegistry,
QgsExpressionContext,
QgsExpressionContextScope)
from processing.core.ProcessingConfig import ProcessingConfig
from processing.gui.RenderingStyles import RenderingStyles
def set_layer_name(layer, context_layer_details):
"""
Sets the name for the given layer, either using the layer's file name
(or database layer name), or the name specified by the parameter definition.
"""
use_filename_as_layer_name = ProcessingConfig.getSetting(ProcessingConfig.USE_FILENAME_AS_LAYER_NAME)
if use_filename_as_layer_name or not context_layer_details.name:
source_parts = QgsProviderRegistry.instance().decodeUri(layer.dataProvider().name(), layer.source())
layer_name = source_parts.get('layerName', '')
# if source layer name exists, use that -- else use
if layer_name:
layer.setName(layer_name)
else:
path = source_parts.get('path', '')
if path:
layer.setName(os.path.splitext(os.path.basename(path))[0])
elif context_layer_details.name:
# fallback to parameter's name -- shouldn't happen!
layer.setName(context_layer_details.name)
else:
layer.setName(context_layer_details.name)
def handleAlgorithmResults(alg, context, feedback=None, showResults=True, parameters={}):
wrongLayers = []
if feedback is None:
feedback = QgsProcessingFeedback()
feedback.setProgressText(QCoreApplication.translate('Postprocessing', 'Loading resulting layers'))
i = 0
for l, details in context.layersToLoadOnCompletion().items():
if feedback.isCanceled():
return False
if len(context.layersToLoadOnCompletion()) > 2:
# only show progress feedback if we're loading a bunch of layers
feedback.setProgress(100 * i / float(len(context.layersToLoadOnCompletion())))
2017-05-15 19:01:15 +10:00
try:
layer = QgsProcessingUtils.mapLayerFromString(l, context, typeHint=details.layerTypeHint)
if layer is not None:
set_layer_name(layer, details)
2017-06-13 12:47:11 +10:00
'''If running a model, the execution will arrive here when an algorithm that is part of
that model is executed. We check if its output is a final otuput of the model, and
adapt the output name accordingly'''
outputName = details.outputName
expcontext = QgsExpressionContext()
scope = QgsExpressionContextScope()
expcontext.appendScope(scope)
for out in alg.outputDefinitions():
if out.name() not in parameters:
continue
outValue = parameters[out.name()]
if hasattr(outValue, "sink"):
outValue = outValue.sink.valueAsString(expcontext)[0]
else:
outValue = str(outValue)
if outValue == l:
outputName = out.name()
break
2019-02-13 05:52:07 +10:00
style = None
if outputName:
style = RenderingStyles.getStyle(alg.id(), outputName)
2017-06-13 12:47:11 +10:00
if style is None:
if layer.type() == QgsMapLayerType.RasterLayer:
2017-06-13 12:47:11 +10:00
style = ProcessingConfig.getSetting(ProcessingConfig.RASTER_STYLE)
else:
if layer.geometryType() == QgsWkbTypes.PointGeometry:
style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POINT_STYLE)
elif layer.geometryType() == QgsWkbTypes.LineGeometry:
style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_LINE_STYLE)
else:
style = ProcessingConfig.getSetting(ProcessingConfig.VECTOR_POLYGON_STYLE)
if style:
layer.loadNamedStyle(style)
details.project.addMapLayer(context.temporaryLayerStore().takeMapLayer(layer))
if details.postProcessor():
details.postProcessor().postProcessLayer(layer, context, feedback)
else:
wrongLayers.append(str(l))
2017-05-15 19:01:15 +10:00
except Exception:
QgsMessageLog.logMessage(QCoreApplication.translate('Postprocessing', "Error loading result layer:") + "\n" + traceback.format_exc(), 'Processing', Qgis.Critical)
2017-06-13 12:47:11 +10:00
wrongLayers.append(str(l))
i += 1
feedback.setProgress(100)
if wrongLayers:
2017-11-09 23:47:31 +01:00
msg = QCoreApplication.translate('Postprocessing', "The following layers were not correctly generated.")
msg += "<ul>" + "".join(["<li>%s</li>" % lay for lay in wrongLayers]) + "</ul>"
msg += QCoreApplication.translate('Postprocessing', "You can check the 'Log Messages Panel' in QGIS main window to find more information about the execution of the algorithm.")
2017-01-12 12:54:13 +02:00
feedback.reportError(msg)
return len(wrongLayers) == 0