10

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
  • 1
    Set "output": 'memory:' Commented May 24, 2018 at 16:23

1 Answer 1

9

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.

3
  • 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? Commented May 25, 2018 at 7:49
  • 2
    You could use QgsProcessingUtils.tempFolder() or QgsProcessingUtils.generateTempFilename
    – ndawson
    Commented May 25, 2018 at 8:58
  • 1
    Or QgsProcessing.TEMPORARY_OUTPUT as value for the OUTPUT parameter to have QGIS place it in your temp directory automatically. Commented Jan 29, 2023 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.