TransWikia.com

Python script from graphical modeller of QGIS 3.4 gives KeyError?

Geographic Information Systems Asked by Kala on November 27, 2020

For several tasks in QGIS 3.4 I need a python script. As I am not used to use python I created the script with the graphical modeler (I modeled the tasks in the graphical modeler and exported the python script).
The first part of the script is basically

  1. an object-filter to choose all points with a common attribute of a point layer
  2. create a heatmap with all the points choosen with the object-filter

When I start the model from the graphical modeler it works as it should. But after exporting the python script and start this from the processing toolbox, it gaves a “KeyError”, that the output of the object-filter is not specified.

The first two algorithms in the python script are as follow:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
import processing


class Automatisierung_20190527(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('punkte', 'punkte', types=[QgsProcessing.TypeVectorPoint], defaultValue=None))

    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(8, model_feedback)
        results = {}
        outputs = {}

        # Objektfilter
        alg_params = {
            'INPUT': parameters['punkte'],
            'OUTPUT_gefiltert_1990': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['Objektfilter'] = processing.run('native:filter', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        # Heatmap (Kerndichtenschätzung)
        alg_params = {
            'DECAY': 0,
            'INPUT': outputs['Objektfilter']['OUTPUT_gefiltert_1990'],
            'KERNEL': 0,
            'OUTPUT_VALUE': 0,
            'PIXEL_SIZE': 100,
            'RADIUS': 15000,
            'RADIUS_FIELD': None,
            'WEIGHT_FIELD': None,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['HeatmapKerndichtenschtzung'] = processing.run('qgis:heatmapkerneldensityestimation', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(2)
        if feedback.isCanceled():
            return {}

The error message says the following:

Results: {}
Traceback (most recent call last):
File "<string>", line 34, in processAlgorithm
KeyError: 'OUTPUT_gefiltert_1990'

Can somebody help me with fixing that error?

One Answer

QGIS 3.4 does not allow for exporting models as scripts, this was reintroduced in 3.6 (see Exporting processing modeler as Python script in QGIS 3? )

So I am assuming you have used a higher version of QGIS (3.6+) to export your script, and are then trying to run the script in 3.4. Like this question: QGIS 3.8 model to Python script, not working in 3.4

And the answer is the same: QgsProcessing.TEMPORARY_OUTPUT is not available in QGIS 3.4.

So this explains why your script fails when it attempts to use['OUTPUT_gefiltert_1990'] which has the value of QgsProcessing.TEMPORARY_OUTPUT

Answered by TimD on November 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP