Use Overlay Function as Filter in QGIS Expression

expressionfilteroverlayqgisqgis-3

I have a point layer with one field, id ranging from 0 to 9. I have a polygon that intersects some of the points, like so:

enter image description here

My aim is to use an expression to get the intersecting point with the highest value for id.

For demonstration purposes I use a Geometry Generator symbol layer on my polygon layer.

collect_geometries(
    overlay_intersects(
        'point_layer', 
        $geometry,
        filter:= id = array_max(overlay_intersects('point_layer', id))
    )
)

I expect there to be a single point at the location of the point in point_layer with the highest id (9). However, all intersecting points are returned.

enter image description here

When I test the expression I use for the filter as a standalone expression, it returns 9, as expected.

enter image description here

And when I hard-code the highest id value (9), I get the desired result:

collect_geometries(
    overlay_intersects(
        'point_layer', 
        $geometry,
        filter:= id = 9
    )
)

enter image description here

What am I missing?

Best Answer

Not a very elegant way, but I think it will work.

with_variable(
    'maxval',
    array_max(
        overlay_intersects(
            'point_layer', 
            "id"
        )
    ),
    array_filter(
        overlay_intersects(
            'point_layer', 
            array("id", $geometry)
        ),
        @element[0] = @maxval
    )[0][1]
)
Related Question