Sunday, 16 December 2018

qgis - Getting the output layer reference returned by Processing tool


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


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*(...)


Is there any processing.run*() method that returns layer instance/reference directly, e.g. QgsVectorLayer?



Answer



QGIS 3:





  • OPTION 1: run() method with memory output


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

    # OUTPUT

    #`result = {'OUTPUT': }`

    Result is a dictionary. result['OUTPUT'] gives an instance of a layer (QgsVectorLayer). No layer is added. Option 1 is one and only solution that returns a reference for QgsVectorLayer in QGIS 3.




It can be used in the following way:


result_layer = processing.run("alg_name", {...,  "OUTPUT":"memory:"})["OUTPUT"]

result_layer is now . Since it's a memory layer, if necessary, it has to be added using addMapLayer() method.


Other Processing Options





  • OPTION 2: 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 3: 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 4: runAndLoadResults() method with memory output


    result = processing.runAndLoadResults("native:buffer",
    {'INPUT':'D:/foo/bar.shp',
    ... # other params

    'OUTPUT':'memory:buffer' # or 'OUTPUT':'TEMPORARY_OUTPUT'
    })

    # OUTPUT
    # `result = {'OUTPUT': 'buffer_0ae....'}`

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







QGIS 2:




  • OPTION 5: 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 6: 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 7: 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 8: 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.




Neither runalg nor runandload returns a reference for output in QGIS 2.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...