3

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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 12, 2023 at 16:02
0

1 Answer 1

8

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"]}
bugmenot123
12.3k4 gold badges42 silver badges84 bronze badges
answered Mar 12, 2023 at 21:16
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.