0

I want to run the calculation based on what the user chooses from the drop-down menu. For Example, if the user chooses the second option, then the calculation will be using the second formula. I can run my code but the calculation only uses the first formula. How to make use the other formula?

self.addParameter(QgsProcessingParameterEnum(
        self.RAD_PERCENTAGE,"Age",
        options = ['More than 10 years','5 to 9 years','Below than 5 years']
    ))

for options in self.RAD_PERCENTAGE:
        
        if 'options == 0':
            field = processing.run("qgis:fieldcalculator", 
                {'INPUT':parameters[self.INPUT_VECTOR],
                'FIELD_NAME':'NewRadius',
                'FIELD_TYPE':0,
                'FIELD_LENGTH':10,
                'FIELD_PRECISION':4,
                'NEW_FIELD':True,
                'FORMULA':'Radius-(Radius * 20/100)',
                'OUTPUT': 'memory:'
                },context=context, feedback=feedback, is_child_algorithm=True)
            newradius = field['OUTPUT']
        
        elif 'options == 1':
            field = processing.run("qgis:fieldcalculator", 
                {'INPUT':parameters[self.INPUT_VECTOR],
                'FIELD_NAME':'NewRadius',
                'FIELD_TYPE':0,
                'FIELD_LENGTH':10,
                'FIELD_PRECISION':4,
                'NEW_FIELD':True,
                'FORMULA':'Radius-(Radius * 10/100)',
                'OUTPUT': 'memory:'
                },context=context, feedback=feedback, is_child_algorithm=True)
            newradius = field['OUTPUT']
        
        else:
            field = processing.run("qgis:fieldcalculator", 
                {'INPUT':parameters[self.INPUT_VECTOR],
                 'FIELD_NAME':'NewRadius',
                 'FIELD_TYPE':0,
                 'FIELD_LENGTH':10,
                 'FIELD_PRECISION':4,
                 'NEW_FIELD':True,
                 'FORMULA':'Radius-(Radius * 0/100)',
                 'OUTPUT': 'memory:'
                },context=context, feedback=feedback, is_child_algorithm=True)
            newradius = field['OUTPUT']
1
  • 2
    Remove the quotes: options == 1. Also there is no need to iterate over self.RAD_PERCENTAGE.
    – MrXsquared
    Sep 14, 2022 at 8:02

1 Answer 1

4
...
def processAlgorithm(self, parameters, context, feedback):
    option = self.parameterAsEnum(parameters, self.RAD_PERCENTAGE, context)
    if option == 0: # More than 10 years
        ...
    elif option == 1: # 5 to 9 years
        ...
1
  • 1
    To elaborate on that: if-statements in python check whether a condition is true. Strings in quotes always evaluate to true. You don't want to evaluate a string, you want to evaluate the result of the comparison of option to 0 or 1. Thats why you don't need quotes.
    – til_b
    Sep 14, 2022 at 8:23

Your Answer

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

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