1

I am trying to compute with multiple filters applied to the aggregate function in a field calculator, but it is not working. Is this not possible in the field calculator? Here are the formulas and errors for the Field Calculator.

I assume an expression that counts the number of layer B geometries around the point geometry of the reference layer that satisfy the three conditions.

 array_length( 
  overlay_nearest('layer_B',
   $id ,
   limit:=-1,
   filter:= "field_b1"=1 & "field_b2">0 & "field_b2"<20,
   max_distance:=30
   )
   )
syntax error, unexpected Unknown_CHARACTER, expecting COMMA or ')'
syntax error, unexpected Unknown_CHARACTER, expecting $end
syntax error, unexpected COMMA, expecting $end
syntax error, unexpected ')', expecting $end

1 Answer 1

2

You can use the eval() function to evaluate an expression string like so:

array_length( 
  overlay_nearest('layer_B',
    $id,
    limit:=-1,
    filter:=eval('"field_b1"=1 and "field_b2">0 and "field_b2"<20'),
    max_distance:=30
  )
)

Note the use of and rather than &, also.

From the help pane of the field calculator
function eval

Evaluates an expression which is passed in a string. Useful to expand dynamic parameters passed as context variables or fields.

Syntax

eval(expression)

Arguments

expression an expression string

Examples

eval(''nice'') → 'nice'
eval(@expression_var) → [whatever the result of evaluating @expression_var might be…]

3
  • Thank you very much. I got the same output with and without eval(). Jan 14, 2023 at 13:25
  • Good to know! Thanks for mentioning :)
    – Matt
    Jan 14, 2023 at 13:26
  • Thanks for mentioning it, it helped me learn about eval()! Jan 14, 2023 at 13:43

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.