Skip to content
Permalink
Browse files

Fix processing.runAndLoadResults

  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 55ce31b commit ac9a39f09afb865f5482a6642c12917c5360b174
Showing with 29 additions and 4 deletions.
  1. +2 −1 python/plugins/processing/gui/HistoryDialog.py
  2. +27 −3 python/plugins/processing/tools/general.py
@@ -112,7 +112,8 @@ def executeAlgorithm(self):
if isinstance(item, TreeLogEntryItem):
if item.isAlg:
script = 'import processing\n'
script += item.entry.text.replace('run(', 'runAndLoadResults(')
script += 'from qgis.core import QgsProcessingOutputLayerDefinition, QgsProcessingFeatureSourceDefinition\n'
script += item.entry.text.replace('processing.run(', 'processing.runAndLoadResults(')
exec(script)

def changeText(self):
@@ -35,7 +35,13 @@
except ImportError:
import configparser as configparser

from qgis.core import (QgsApplication)
from qgis.core import (QgsApplication,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterVectorOutput,
QgsProcessingParameterRasterOutput,
QgsProcessingOutputLayerDefinition,
QgsProject)
from processing.core.Processing import Processing
from processing.core.parameters import ParameterSelection
from processing.gui.Postprocessing import handleAlgorithmResults
@@ -76,11 +82,29 @@ def run(algOrName, parameters, onFinish=None, feedback=None, context=None):
return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)


def runAndLoadResults(name, *args, **kwargs):
def runAndLoadResults(algOrName, parameters, feedback=None, context=None):
"""Executes given algorithm and load its results into QGIS project
when possible.
"""
return Processing.runAlgorithm(name, handleAlgorithmResults, *args, **kwargs)
if isinstance(algOrName, QgsProcessingAlgorithm):
alg = algOrName
else:
alg = QgsApplication.processingRegistry().algorithmById(algOrName)

# output destination parameters to point to current project
for param in alg.parameterDefinitions():
if not param.name() in parameters:
continue

if isinstance(param, (QgsProcessingParameterFeatureSink, QgsProcessingParameterVectorOutput, QgsProcessingParameterRasterOutput)):
p = parameters[param.name()]
if not isinstance(p, QgsProcessingOutputLayerDefinition):
parameters[param.name()] = QgsProcessingOutputLayerDefinition(p, QgsProject.instance())
else:
p.destinationProject = QgsProject.instance()
parameters[param.name()] = p

return Processing.runAlgorithm(alg, parameters=parameters, onFinish=handleAlgorithmResults, feedback=feedback, context=context)


def version():

0 comments on commit ac9a39f

Please sign in to comment.
You can’t perform that action at this time.