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?

Taras
  • 32,823
  • 4
  • 66
  • 137
four-eyes
  • 3,378
  • 5
  • 33
  • 58

2 Answers2

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
  • 32,823
  • 4
  • 66
  • 137
BERA
  • 72,339
  • 13
  • 72
  • 161
  • your version gives you the layer as a variable, too: proc = processing.runAndLoadResults("qgis:refactorfields",<<>>) outputlayer = proc['OUTPUT'] – Sickboy Jul 31 '22 at 09: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
  • 32,823
  • 4
  • 66
  • 137
MrXsquared
  • 34,292
  • 21
  • 67
  • 117