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
2018-02-05 22:11:34 -04:00
from qgis . core import ( Qgis ,
QgsProject ,
2017-04-24 14:35:50 +10:00
QgsProcessingFeedback ,
QgsProcessingUtils ,
2017-06-13 12:47:11 +10:00
QgsMapLayer ,
QgsWkbTypes ,
2017-04-24 14:35:50 +10:00
QgsMessageLog )
2014-10-04 14:04:39 +03:00
2013-10-01 20:52:22 +03:00
from processing . core . ProcessingConfig import ProcessingConfig
from processing . gui . RenderingStyles import RenderingStyles
2014-10-04 14:04:39 +03:00
2015-08-22 14:29:41 +02:00
2017-04-26 14:33:53 +10:00
def handleAlgorithmResults ( alg , context , 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
2017-06-06 08:40:23 +10:00
for l , details in context . layersToLoadOnCompletion ( ) . items ( ) :
2017-06-12 13:35:31 +10:00
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 )
2017-06-06 10:34:57 +10:00
if layer is not None :
2017-08-19 00:56:18 +10:00
if not ProcessingConfig . getSetting ( ProcessingConfig . USE_FILENAME_AS_LAYER_NAME ) :
layer . setName ( details . name )
2017-06-13 12:47:11 +10:00
2017-08-19 00:47:21 +10:00
style = None
if details . outputName :
style = RenderingStyles . getStyle ( alg . id ( ) , details . outputName )
2017-06-13 12:47:11 +10:00
if style is None :
if layer . type ( ) == QgsMapLayer . RasterLayer :
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 )
2018-03-14 18:48:24 +10:00
2017-06-06 08:40:23 +10:00
details . project . addMapLayer ( context . temporaryLayerStore ( ) . takeMapLayer ( layer ) )
2018-03-14 18:48:24 +10:00
if details . postProcessor ( ) :
details . postProcessor ( ) . postProcessLayer ( layer , context , feedback )
2017-12-13 14:35:14 +01:00
else :
wrongLayers . append ( str ( l ) )
2017-05-15 19:01:15 +10:00
except Exception :
2018-02-05 22:11:34 -04:00
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 ) )
2017-06-12 13:35:31 +10:00
i + = 1
2015-09-29 17:50:53 +02:00
2017-11-10 11:33:58 +10:00
feedback . setProgress ( 100 )
2014-06-08 23:12:13 +02:00
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 )
2015-09-29 17:50:53 +02:00
2015-05-20 20:21:34 +02:00
return len ( wrongLayers ) == 0