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?
-
Check this tutorial for details: PyQGIS 101: Running Processing toolsTaras– Taras ♦2021年10月01日 05:51:14 +00:00Commented Oct 1, 2021 at 5:51
-
Also related: gis.stackexchange.com/questions/76594/…Taras– Taras ♦2021年10月01日 05:58:21 +00:00Commented Oct 1, 2021 at 5:58
2 Answers 2
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()
, butrunAndLoadResults()
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).
-
your version gives you the layer as a variable, too: proc = processing.runAndLoadResults("qgis:refactorfields",<<<something>>>) outputlayer = proc['OUTPUT']Sickboy– Sickboy2022年07月31日 09:05:32 +00:00Commented Jul 31, 2022 at 9:05
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)
Explore related questions
See similar questions with these tags.