2

I built a python processing tool for QGIS where my goal is to use field calculator and multiply an existing field in a layer by a parameter (number) decided by the user. I defined the parameter for the user inside the processing algorithm:

def initAlgorithm(self, config=None):
    self.addParameter(
        QgsProcessingParameterNumber(
            name = self.Lane_Weight,
            description = self.tr('LaneWeight'),
            type = ProcessingParameterNumber.Double,
            defaultValue = 2.5,
            optional = True
        )
    )

My problem is that I can't understand how to call the Lane_Weight parameter in the field calculator FORMULA. This is what I have Tried (without any success):

    from qgis.processing import run as QgisRun
    QgisRun(
        "native:fieldcalculator", {
            'INPUT': my_layer,
            'FIELD_NAME': 'my_new_field',
            'FIELD_TYPE': 0, #float type
            'FIELD_LENGTH': 4,
            'FIELD_PRECISION': 4,
            'FORMULA': ' "MyFieldName" * parameters[self.Lane_Weight] ',  
            'OUTPUT': 'memory:'
        },
        context = context,
        feedback = feedback,
        is_child_algorithm = True
    )['OUTPUT']

Working with QGIS 3.22.3

1
  • Did you try to use the algorithm using the GUI and then in the log tab, to carefully compare the structure of the constructed dictionary of parameters with the one you are using in your code? This can give you some serious hint. Sep 9, 2022 at 19:09

1 Answer 1

2

The 'FORMULA' key in the dictionary of parameters passed to QgisRun gets a string as value.

In your example that is the verbatim ' "MyFieldName" * parameters[self.Lane_Weight] '.

When executing the native:fieldcalculator algorithm, this string will end up as the field calculator's expression text and QGIS' expression evaluation engine will evaluate it.

For the evaluation engine the double quoted "MyFieldName" is special, it will know that this means "the feature's value in the MyFieldName column please". The parameters[self.Lane_Weight] however is not something it can understand as it knows no Python. So you'll need to retrieve that value in Python land and add it into the formula string before it gets passed to the expression engine.

You can get the value of your user parameter using various methods of the QgsProcessingAlgorithm class. In this case QgsProcessingAlgorithm.parameterAsDouble() probably makes most sense (I wonder if there is a simple "parameterAsDefined" method...). You can then use that value and insert it into the FORMULA string.

lane_weight = self.parameterAsDouble(parameters, self.Lane_Weight, context)
...
QgisRun(
    "native:fieldcalculator", {
        ...
        'FORMULA': f'"MyFieldName" * {lane_weight}',  # inserted via f-string
        ...
     },
     ...
)

Reference: Parsing an input parameter to gdal:rastercalculator formula in QGIS 3.6 Python script

PS: Is your self.Lane_Weight really something to be used as parameter name? It sounds like a numeric value rather.

1
  • 1
    This is the answer, thank you very much !
    – Ofir
    Sep 10, 2022 at 10:06

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.