4

Context

I use an expression with Geometry Generator in QGIS 3.22 to connect each point with the nearest point that has the value of 2 for the attribute field called value. This works fine using this expression:

make_line (
    $geometry,
    overlay_nearest (
        @layer, 
        $geometry, 
        filter:= value = 2
    )[0]
)

The problem

However, I want to replace the value of 2 with a variable. However, this does not work - the following expression returns just NULL. When I replace @number (line 9) with 2, everything is correct again:

with_variable(
    'number',
    2,
    make_line (
        $geometry,
        overlay_nearest (
            @layer, 
            $geometry, 
            filter:= value = @number
        )[0]
    )
)

Using a value of 2, lines are correctly drawn. If I replace 2 by @number, output is just NULL: enter image description here

The question

Obviously, the variable is not correctly recognized. Why is that / how can this be solved? I'm specifically interested in solving this using a variable.

There is a workaround (see next), still, I'm interested why that does not work as expected.

A workaround

A workaround based on this solution is to use a string with eval() function.

with_variable(
    'number',
    2,
    make_line (
        $geometry,
        eval (
            'overlay_nearest (
                @layer, 
                $geometry, 
                filter:= value ='  || @number || ')[0]')
    )
)

enter image description here

Babel
  • 71,072
  • 14
  • 78
  • 208

0 Answers0