8

Let's say I run a simple algorithm like this

import processing
# https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectoranalysis.html?highlight=count%20points%20polygon#id3
processing.run("qgis:countpointsinpolygon", { 
 'POLYGONS' : 'polygons', 
 'POINTS': 'points',
 'OUTPUT': 'TEMPORARY_OUTPUT'
 })

How do I materialize/visualize/add the temporary layer in the Layers Panel?

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Sep 28, 2021 at 13:16
2

2 Answers 2

11

Another option is runAndLoadResults().

As was mentioned in the QGIS Documentation:

Unlike when an algorithm is executed from the toolbox, outputs are not added to the map canvas if you execute that same algorithm from the Python console using run(), but runAndLoadResults() will do that.

processing.runAndLoadResults("qgis:countpointsinpolygon", { 
 'POLYGONS' : 'polygons', 
 'POINTS': 'points',
 'OUTPUT': 'TEMPORARY_OUTPUT'
 })

(But I like @MrXsquared option better, since you get the layer as a variable you can then use in the next processing steps).

Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Sep 28, 2021 at 13:25
1
  • your version gives you the layer as a variable, too: proc = processing.runAndLoadResults("qgis:refactorfields",<<<something>>>) outputlayer = proc['OUTPUT'] Commented Jul 31, 2022 at 9:05
6

Can't tell if that is the "best" way, but its one straight forward:

import processing
# https://docs.qgis.org/3.16/en/docs/user_manual/processing_algs/qgis/vectoranalysis.html?highlight=count%20points%20polygon#id3
proc = processing.run("qgis:countpointsinpolygon", { 
 'POLYGONS' : 'polygons', 
 'POINTS': 'points',
 'OUTPUT': 'TEMPORARY_OUTPUT'
 })
outputlayer = proc['OUTPUT']
QgsProject.instance().addMapLayer(outputlayer)
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Sep 28, 2021 at 13:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.