PyQGIS Output Parameter – Defining OUTPUT Parameter in PyQGIS 3 Processing Algorithms

pyqgispyqgis-3qgisqgis-processing

In PyQGIS 2.X I could handle the output of processings algorithm as follows:

import processing
# None is the OUTPUT parameter here
buffer = processing.runalg('qgis:fixeddistancebuffer', network_from_nv_tmp, 0.1, 10, True, None)
clip = processing.runalg('qgis:clip', self.network_layer, buffer['OUTPUT'], None)

key approach here is to set the output parameter to None for running the algorithm and access it via result['OUTPUT'] in subsequent algorithms.

Now I try the same (or comparitivly the same) in PyQGIS 3:

import processing
buffer = processing.run('qgis:buffer', {'INPUT':network_from_nv_tmp, 'DISTANCE':0.1, 'SEGMENTS':10, 'END_CAP_STYLE':0, 'JOIN_STYLE':0, 'MITER_LIMIT':2, 'DISSOLVE':True, 'OUTPUT':None})        
clip = processing.run('qgis:clip', {'INPUT':self.network_layer, 'OVERLAY':buffer['OUTPUT'], 'OUTPUT':None})

which throws the following traceback:

[...]
_core.QgsProcessingException: Unable to execute algorithm
Could not create destination layer for OUTPUT: invalid value

So my question is: What is a valid value for parameter OUTPUT here?

Best Answer

I just stumbled upon a code snippet in https://anitagraser.com/2018/03/24/revisiting-point-polygon-joins/ that solved my issue. This indicates that the string 'memory:' is one possible valid value for OUTPUT parameter generating a temporary memory layer.

Furthermore, any string is a valid value. If a string e.g. 'buffered' is provided the result is stored in a Geopackage called buffered.gpkg. Thus, I strongly recommend not to provide the same string in subsequent algorithms, because the second algorithm will throw an exeption that the *.gpkg cannot be created since it has allready be created by the first.

Related Question