QGIS Geometry Generator – Creating Lines Once Per Grouped Geometries

expressiongeometry-generatorqgisqgis-3

I am using a Geometry Generator symbol layer to draw lines between grouped points. I am grouping points based on two fields letter and number, which I concatenate to make the groups (GeoPackage download).

Current expression:

make_line(
    array_agg(                       -- gather results into array
        expression:=$geometry, 
        group_by:=letter||number     -- concatenate fields to make groups
    )
)

make_line accepts an array of Point geometries, hence using array_agg.

The expression works and draws the lines, however, it draws a line per point. So, for a group of 4 geometries it overlays a line 4 times.

It is difficult to visualise but there is a discernible difference in colours between these lines using the Screen feature blending mode.

enter image description here

How can I adapt my expression to prevent this?

Best Answer

You can use just the first point of each group to create the geometry. Simply use an case .. when condition to check the $id and create the line only in case it is equal to the smallest $id of each group. If you don't add an else statement, you can even avoid creating empty geometries:

case
when
    $id = array_min (
        array_agg(
            $id,
            group_by:=letter||number 
        )
    )
then    make_line(
        array_agg( 
            expression:=$geometry, 
            group_by:=letter||number 
        )
    )
end