I wrote a simple QGIS Processing algorithm:
from qgis.core import QgsProcessing
from qgis.processing import alg
import processing
@alg(name="test", label=alg.tr("test"), group="test", group_label=alg.tr("test"))
@alg.input(type=alg.SOURCE, types=[QgsProcessing.TypeVectorPoint], name="POINTS_INPUT", label="Point layer")
@alg.input(type=alg.RASTER_LAYER_DEST, name="OUTPUT", label="Raster output layer")
def test(instance, parameters, context, feedback, inputs):
"""Description goes here. (Don't delete this! Removing this comment will cause errors.)"""
buffer = processing.run(
"native:buffer",
{
"INPUT": parameters["POINTS_INPUT"],
"DISTANCE": 100,
"OUTPUT": "memory:",
},
is_child_algorithm=True,
context=context,
feedback=feedback,
)
rasterize = processing.run(
"gdal:rasterize",
{
'INPUT': buffer["OUTPUT"],
'BURN': 123,
'UNITS': 1,
'WIDTH': 10,
'HEIGHT': 10,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT,
},
is_child_algorithm=True,
context=context,
feedback=feedback,
)
return {"OUTPUT": rasterize["OUTPUT"]}
When I run it from the GUI and have the appropriate checkbox to load its result after success, QGIS does not load it. There is a valid TIF file created in my temporary Processing directory. What am I missing?
1 Answer 1
You're specifying a RASTER_LAYER_DEST as the OUTPUT parameter but not passing it to processing.run("gdal:rasterize")
.
The return value of the algorithm is used for passing results to other algorithms, e. g. in the modeller.
This version works and adds the output to the map.
from qgis.core import QgsProcessing
from qgis.processing import alg
import processing
@alg(name="test", label=alg.tr("test"), group="test", group_label=alg.tr("test"))
@alg.input(type=alg.SOURCE, types=[QgsProcessing.TypeVectorPoint], name="POINTS_INPUT", label="Point layer")
@alg.input(type=alg.RASTER_LAYER_DEST, name="OUTPUT", label="Raster output layer")
def test(instance, parameters, context, feedback, inputs):
"""Description goes here. (Don't delete this! Removing this comment will cause errors.)"""
buffer = processing.run(
"native:buffer",
{
"INPUT": parameters["POINTS_INPUT"],
"DISTANCE": 100,
"OUTPUT": "memory:",
},
is_child_algorithm=True,
context=context,
feedback=feedback,
)
rasterize = processing.run(
"gdal:rasterize",
{
'INPUT': buffer["OUTPUT"],
'BURN': 123,
'UNITS': 1,
'WIDTH': 10,
'HEIGHT': 10,
'OUTPUT': parameters["OUTPUT"], # pass RASTER_LAYER_DEST instead of TEMPORARY_OUTPUT
},
is_child_algorithm=True,
context=context,
feedback=feedback,
)
return {"OUTPUT": rasterize["OUTPUT"]}