2

After running Processing algorithm ending up with a layer (memory or linked to a file) by runalg or runandload in QGIS2, run or runAndLoadResults in QGIS 3, these methods return a result which is mostly a dictionary like {'OUTPUT': 'path_to_result_file'} etc. So, after result = processing.run*(...), I can get the output by result['OUTPUT']. But its value is mostly just a string (a path or 'memory:name').

How can I get the instance of output layer by just writing processing.run*(...) line without adding additional code line(s).

If it would be a method like processing.run*(...) that returns an instance of output, not path or not 'memory:name', we would create the layer reference by writing just layer = processing.run*("any_alg", ...)

EDIT 1: I don't ask how to load the output as a layer or how to get any layer in the memory.

EDIT 2: Briefly, Is there any processing.run*() method that returns layer instance directly, e.g. QgsVectorLayer?

EDIT 3: It's OK if it is in a dictionary like {'OUTPUT': <layer instance>} or a pure instance.

5

After some works, I found these results:

QGIS 2: (2.18.18)

  • OPTION 1: runandload() method with file output

    result = processing.runandload("qgis:fixeddistancebuffer",
                                   "c:/foo/bar.shp", 10, 5, False,
                                   "c:/foo/baz.shp")
    # OUTPUT
    # `result = <*****.FixedDistanceBuffer instance at 0x00...>`
    

    Result is an instance of related algorithm class. A layer is added.

  • OPTION 2: runandload() method with memory output

    result = processing.runandload("qgis:fixeddistancebuffer",
                                   "c:/foo/bar.shp", 10, 5, False,
                                   "memory:mem_layer")
    # OUTPUT
    # `result = <*****.FixedDistanceBuffer instance at 0x00...>`
    

    Result is an instance of related algorithm class. A layer is added.

  • OPTION 3: runalg() method with file output

    result = processing.runalg("qgis:fixeddistancebuffer",
                               "c:/foo/bar.shp", 10, 5, False,
                               "c:/foo/baz.shp")
    # OUTPUT
    # `result = {'OUTPUT': "c:/foo/baz.shp"}`
    

    Result is a dictionary, value is a string. No layer is added.

  • OPTION 4: runalg() method with memory output

    result = processing.runalg("qgis:fixeddistancebuffer",
                               "c:/foo/bar.shp", 10, 5, False,
                               "memory:mem_layer") 
    
    # OUTPUT
    # `result = {'OUTPUT': "memory:mem_layer"}`
    

    Result is a dictionary, value is a string. No layer is added.


QGIS 3: (3.0.1)

  • OPTION 5: runAndLoadResults() method with file output

    result = processing.runAndLoadResults("native:buffer",
                                          {'INPUT':'D:/foo/bar.shp', 
                                           ... # other params
                                           'OUTPUT':'c:/foo/baz.shp'})
    
    # OUTPUT
    # `result = {'OUTPUT': 'c:/foo/baz.shp'}`
    

    Result is a dictionary, value is a string. A layer is added.

  • OPTION 6: runAndLoadResults() method with memory output

    result = processing.runAndLoadResults("native:buffer",
                                          {'INPUT':'D:/foo/bar.shp', 
                                           ... # other params
                                           'OUTPUT':'memory:buffer'})
    
    # OUTPUT
    # `result = {'OUTPUT': 'buffer_0ae....'}`
    

    Result is a dictionary, value is a string. A layer is added.

  • OPTION 7: run() method with file output

    result = processing.run("native:buffer",
                            {'INPUT':'D:/foo/bar.shp', 
                             ... # other params
                             'OUTPUT':'c:/foo/baz.shp'})
    
    # OUTPUT
    # `result = {'OUTPUT': 'c:/foo/baz.shp'}`
    

    Result is a dictionary, value is a string. No layer is added.

  • OPTION 8: run() method with memory output

    result = processing.run("native:buffer",
                            {'INPUT':'D:/foo/bar.shp', 
                             ... # other params
                             'OUTPUT':'memory:buffer'})
    
    # OUTPUT
    #`result = {'OUTPUT': <qgis._core.QgsVectorLayer object at 0x00...>}`
    

    Again, result is a dictionary, BUT value is an instance of a layer (QgsVectorLayer). No layer is added.

    result['OUTPUT'] value, now, is an instance of a layer (QgsVectorLayer), and OPTION 8 is the answer that I'm looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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