2

I wrote a simple custom QGIS Processing algorithm in Python:

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,
 )
 
 feedback.pushInfo("Buffering done...")
 
 rasterize = processing.run(
 "gdal:rasterize",
 {
 'INPUT': buffer["OUTPUT"],
 'BURN': 123,
 'UNITS': 1,
 'WIDTH': 10,
 'HEIGHT': 10,
 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT,
 },
 is_child_algorithm=True,
 feedback=feedback,
 )
 return {"OUTPUT": rasterize["OUTPUT"]}

When I run it, the call to the gdal:rasterize algorithm fails to find its input though.

My QGIS is in German, so the error is German:

Kann Algorithmus nicht ausführen Konnte Quelllayer für INPUT nicht laden: output_ac05fad6_1b89_41ba_960e_a68be241e84b nicht gefunden

What is wrong?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 12, 2023 at 15:58

1 Answer 1

4

processing.run() needs the context, otherwise it does not know where to find the temporary data. You should always pass the context and feedback objects to the child algorithms.

 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, # <- this line was added
 feedback=feedback,
 )
answered Mar 12, 2023 at 15:58

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.