1

I have a processing script that I created for QGIS 3.4.5 but I keep getting the following error:

Traceback (most recent call last): File "", line 66, in processAlgorithm File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/plugins\processing\tools\general.py", line 113, in run return Processing.runAlgorithm(algOrName, parameters, onFinish=post_process, feedback=feedback, context=context) File "C:/PROGRA~1/QGIS3~1.4/apps/qgis-ltr/./python/plugins\processing\core\Processing.py", line 138, in runAlgorithm feedback.reportError(msg) TypeError: reportError(self, error: str, fatalError: bool): not enough arguments

Here's my script:

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterExtent
from qgis.core import QgsProcessingParameterDistance
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsExpression
from qgis.core import QgsCoordinateReferenceSystem
import processing


class CreateGrid(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterDistance('distanceinmeters', 'Distance in Meters', parentParameterName='', defaultValue=0))
        self.addParameter(QgsProcessingParameterExtent('extent', 'Extent', defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Gridwgs84', 'GridWGS84', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, 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(4, model_feedback)
        results = {}
        outputs = {}

        # Create grid
        alg_params = {
            'CRS': 'ProjectCrs',
            'EXTENT': parameters['extent'],
            'HOVERLAY': 0,
            'HSPACING': parameters['distanceinmeters'],
            'TYPE': 2,
            'VOVERLAY': 0,
            'VSPACING': parameters['distanceinmeters'],
            'OUTPUT': 'memory:'
        }
        outputs['CreateGrid'] = processing.run('qgis:creategrid', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

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

        # Reproject layer
        alg_params = {
            'INPUT': outputs['CreateGrid']['OUTPUT'],
            'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
            'OUTPUT': 'memory:'
        }
        outputs['ReprojectLayer'] = processing.run('native:reprojectlayer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

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

        # Get Latitude
        alg_params = {
            'FIELD_LENGTH': 10,
            'FIELD_NAME': 'Lat',
            'FIELD_PRECISION': 5,
            'FIELD_TYPE': 0,
            'FORMULA': QgsExpression('y(centroid($geometry))').evaluate(),
            'INPUT': outputs['ReprojectLayer']['OUTPUT'],
            'NEW_FIELD': True,
            'OUTPUT': 'memory:a'
        }
        outputs['GetLatitude'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

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

        # Get Longitude
        alg_params = {
            'FIELD_LENGTH': 10,
            'FIELD_NAME': 'Long',
            'FIELD_PRECISION': 5,
            'FIELD_TYPE': 0,
            'FORMULA': QgsExpression('x(centroid($geometry))').evaluate(),
            'INPUT': outputs['GetLatitude']['OUTPUT'],
            'NEW_FIELD': True,
            'OUTPUT': parameters['Gridwgs84']
        }
        outputs['GetLongitude'] = processing.run('qgis:fieldcalculator', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
        results['Gridwgs84'] = outputs['GetLongitude']['OUTPUT']

        return results

    def name(self):
        return 'create_grid'

    def displayName(self):
        return '1. Create Grid'

    def group(self):
        return 'scripts'

    def groupId(self):
        return 'scripts'

    def createInstance(self):
        return CreateGrid()

I'm not sure why I'm getting this error for the field calculator bit.

  • Not sure but I thought the is_child_algorithm parameter was added to 3.6? – Joseph Mar 8 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.