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?

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,
    )

Your Answer

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

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