Skip to content
Browse files

When calling, map layer results are automatically

converted to QgsMapLayer objects with the ownership transferred
to the Python caller

This should make it super-easy for PyQGIS scripts to run processing
algorithms and immediately utilise the results, even if they
are memory layers. They call, and get a dict of
results back which includes those layers ready for adding to the
current project or doing some other processing or operations
with, and if they don't transfer to ownership off then these
layers will be correctly garbaged collected by Python.
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent a2af3a9 commit 55ce31b37170a577438fe70f6158d95fce0d2fe4
Showing with 15 additions and 5 deletions.
  1. +2 −4 python/plugins/processing/algs/qgis/
  2. +13 −1 python/plugins/processing/core/
@@ -87,8 +87,7 @@ def processAlgorithm(self, parameters, context, feedback):

# Delaunay triangulation from input point layer
feedback.setProgressText('Creating Delaunay triangles...'))
delone_triangles ="qgis:delaunaytriangulation", {'INPUT': parameters[ConcaveHull.INPUT], 'OUTPUT': 'memory:'}, feedback=feedback, context=context)['OUTPUT']
delaunay_layer = context.takeResultLayer(delone_triangles)
delaunay_layer ="qgis:delaunaytriangulation", {'INPUT': parameters[ConcaveHull.INPUT], 'OUTPUT': 'memory:'}, feedback=feedback, context=context)['OUTPUT']

# Get max edge length from Delaunay triangles
feedback.setProgressText('Computing edges max length...'))
@@ -131,8 +130,7 @@ def processAlgorithm(self, parameters, context, feedback):

# Dissolve all Delaunay triangles
feedback.setProgressText('Dissolving Delaunay triangles...'))
dissolved ="native:dissolve", {'INPUT': delaunay_layer, 'OUTPUT': 'memory:'}, feedback=feedback, context=context)['OUTPUT']
dissolved_layer = context.takeResultLayer(dissolved)
dissolved_layer ="native:dissolve", {'INPUT': delaunay_layer, 'OUTPUT': 'memory:'}, feedback=feedback, context=context)['OUTPUT']

# Save result
feedback.setProgressText('Saving data...'))
@@ -38,9 +38,12 @@
from qgis.utils import iface
from qgis.core import (QgsMessageLog,

import processing
from processing.script.ScriptUtils import ScriptUtils
@@ -173,6 +176,15 @@ def runAlgorithm(algOrName, parameters, onFinish=None, feedback=None, context=No

if onFinish is not None:
onFinish(alg, context, feedback)
# auto convert layer references in results to map layers
for out in alg.outputDefinitions():
if isinstance(out, (QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer)):
result = results[]
if not isinstance(result, QgsMapLayer):
layer = context.takeResultLayer(result) # transfer layer ownership out of context
if layer:
results[] = layer # replace layer string ref with actual layer (+ownership)
msg ="There were errors executing the algorithm.")

0 comments on commit 55ce31b

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