[GIS] Using memory layer as input and/or output with runalg

pyqgisqgisqgis-processing

Can a memory layer be used as an input and/or output with runalg?

With runandload I get this to work:

 processing.runandload("qgis:mergevectorlayers","memory:bufferOne", "memory:bufferTwo", r"memory:merged")
 processing.runandload("qgis:dissolve", r"memory:merged", True, '', r"D:\PythonTesting\dissolved.shp")

Parameter 2 and 3 are the input layers, which are in memory already, and parameter 3 is the output layer, which is written into memory and used in the following tool (Dissolve). This works fine, but when I try to use the same logic with runalg, then my merged layer in memory does not seem to be created, as the Dissolve tool never runs. The following, for example, would not work:

 processing.runalg("qgis:mergevectorlayers","memory:bufferOne", "memory:bufferTwo", r"memory:merged")
 processing.runandload("qgis:dissolve", r"memory:merged", True, '', r"D:\PythonTesting\datenschrott\dissolved.shp")

Can I assume that runalg either does not accept layers that are in memory or that it cannot output any?

Best Answer

Not completely sure why your method doesn't work but another method is to instead use None when using runalg as this will also create an output in memory. Below I defined the first process as output_0 and called the result of this as input to the second process:

output_0 = processing.runalg("qgis:mergevectorlayers",["memory:bufferOne", "memory:bufferTwo"], None)
processing.runandload("qgis:dissolve", output_0['OUTPUT'], True, '', r"D:\PythonTesting\dissolved.shp")

Result:

Result


Tested on QGIS 2.16.0-Nødebo with Processing plugin v2.12.2.

Note that qgis:mergevectorlayers now requires a list of input layers.