7

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?

2

2 Answers 2

10

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).

1
  • your version gives you the layer as a variable, too: proc = processing.runAndLoadResults("qgis:refactorfields",<<<something>>>) outputlayer = proc['OUTPUT']
    – Sickboy
    Jul 31 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.