I would like to run a processing algorithms in QGIS3 with a memory vector layer as result.
When I define the memory layer as shown in the following, I get the error
Incorrect parameter value for output
while in the Python Error window output
is indicated as QgsVectorLayer
and nodesLayer.isValid()
returns True
:
crs = str(inputLayer.crs().authid())
outputLayer = QgsVectorLayer('Point?crs=' + crs , "points", "memory")
processing.run('grass7:v.net',
{"input":inputLayer,
...
"output": outputLayer})
If I use a path to the output layer, everything works fine:
outputLayer = r"path_to_file\output.shp"
processing.run('grass7:v.net',
{"input":inputLayer,
...
"output": outputLayer})
Any idea on how to correctly create a memory layer in QGIS 3?
1 Answer 1
As Germán noted, you should use "memory:" as the output string. But you'll also need to store the algorithm results, or the memory layer will be garbage collected by python!
results = processing.run('grass7:v.net',
{"input":inputLayer,
...
"output": 'memory:'})
result_layer = results['output']
But more generally - it's a bit inefficient to use a memory layer here. Because you're calling a grass algorithm (not a native QGIS one), all the outputs and inputs are being converted to disk based formats for use by grass, and all outputs from grass are also disk-based (behind the scenes these are shapefiles in a temporary directory). So when you ask for a memory layer output, QGIS has to read over the shapefile created by grass and convert that to a QGIS memory layer. So in this case, it's actually more efficient to just give the algorithm a path to save the shapefile and then read that directly.
-
Thanks for your explanation, it sounds very reasonable. However, when I create the shapefile for use in an interactive QGIS plugin, I thought it would be more common practice to store the intermediate processing files in memory. If I would store the shapefile on the disk, is there a general qgis directory for temporary files craeted within the plugin? If yes, whats the path valid for all plugin users?Sophie Crommelinck– Sophie Crommelinck2018年05月25日 07:49:12 +00:00Commented May 25, 2018 at 7:49
-
2You could use QgsProcessingUtils.tempFolder() or QgsProcessingUtils.generateTempFilenamendawson– ndawson2018年05月25日 08:58:52 +00:00Commented May 25, 2018 at 8:58
-
1Or
QgsProcessing.TEMPORARY_OUTPUT
as value for theOUTPUT
parameter to have QGIS place it in your temp directory automatically.bugmenot123– bugmenot1232023年01月29日 18:23:14 +00:00Commented Jan 29, 2023 at 18:23
Explore related questions
See similar questions with these tags.
"output": 'memory:'