QGIS – Using Geometry Generator with Expressions for Overlaying Layers

expressiongeometry-generatorqgissymbology

  • I have a point layer "Layer1", with an attribute called L1_att.
  • I have a surface layer "Layer2", with an attribute called L2_att.

In my symbology of Layer1, I have categorized with L1_att. I have a point symbol (different color for each value) and also a "geometry generator" with type as surface.

For the geometry generator part, what I'm trying to do is something like this:

collect_geometries(overlay_intersects('Layer2', 'L2_att'="L1_att",$geometry))

This means, I want to collect the polygons that overlay my point layer, but only if their attributes are the same!
And secondly, if a polygon covers 2 points, does it show this feature twice? Cause I want it to be shown only once.

Hope it is clear enough!

EDIT: I try Babel's solution (see below), but it doesn't seem to work:
enter image description here

Best Answer

Edit: new version

Use this expression:

geometry (
    get_feature_by_id (
        'Layer2',
        array_first (
            if (
                L1_att = array_first (overlay_intersects('Layer2', L2_att)),
                overlay_intersects('Layer2', $id),
                ''
))))

Screenshot: point X is inside polygon X and polygon Y, but only polygon X is created with Geoemtry generator, based on the common attribute value X: enter image description here

Old version

Use this expression:

collect_geometries (
    if (
        L1_att = array_first (overlay_intersects('Layer2', L2_att)),
        overlay_intersects('Layer2', $geometry),
        ''
))
Related Question