QGIS Graphical Modeler – How to Use Extract by Expression in QGIS Graphical Modeler for Data Extraction

extractqgisqgis-expressionqgis-modeler

QGIS version 3.16.16-Hannover
OS Version Linux Mint 19.3

So I am trying to run a simple Extract by location algorithm in the graphical modeler

I have as Input a polygon file with a field call NUMPOINTS that comes from counting points in polygons.

I want to extract the polygon with the maximum value

I use the expression

"NUMPOINTS" = maximum("NUMPOINTS")

I tested the expression using the algorithm outside the graphical modeler and everything works fine.

Then I go to the graphical model to create the model

model

alorithm parameters

And when I run it I get this error message

syntax error, unexpected $end
Error encountered while running Extract by expression
Execution failed after 0.12 seconds

So I open the model as a python script to try see what is happening. The corresponding code line is

# Extract by expression
alg_params = {
    'EXPRESSION': QgsExpression('\"NUMPOINTS\" = maximum(\"NUMPOINTS\")').evaluate(),
    'INPUT': parameters['InputPoints'],
    'OUTPUT': parameters['Test1']
}
outputs['ExtractByExpression'] = processing.run('native:extractbyexpression', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
results['Test1'] = outputs['ExtractByExpression']['OUTPUT']
return results

And when I run it from there I get the following message

Unable to execute algorithm
Incorrect parameter value for EXPRESSION
Unable to execute algorithm
Incorrect parameter value for EXPRESSION
Execution failed after 0.09 seconds

As I said before, I tested the expression outside the graphical modeler and it works fine.

I just do not understand why the algorithm is not working here or how to make it work.

What am I missing?

Best Answer

In the expression field, select the first entry: Value insted of Precalculated Value. Then the model runs fine.

If you're using the second entry: Pre calculated Value, you have to enclose the expression in single qutotes ': ' "NUMPOINTS" = maximum("NUMPOINTS") '

enter image description here

Related Question