9 Jun 2022

QGIS 3: Symbology with Geometry Generator - Draw One Convex Hull For All Features With Same Attribute

I have a layer name "Trails" with an attribute "Trail ID", which contains uniquie, consecutive Feature/Trail IDs, and an attribute "Schwierigkeit" (trail difficulty). For all features with the value "black" for the attribute "Schwierigkeit" I want to render one convex hull. The below code will select the last element in of all the features in the layer and apply the code for drawing the polygon only once. The first
array_foreach()
in the code will create an array of all features (series generated from 1 to feature count number), over this array, the second array_foreach() will apply the geometry finction on each element which meets the condition of the
array_filter()
function. The
collect_geometries()
function finally puts all those single geometries within the resulting array into one multiline geometry, for which I then draw the hull.

if($id = maximum($id),
convex_hull(
collect_geometries(
with_variable('my_arr', 
array_foreach(
generate_series(1,  layer_property( 'trails', 'feature_count'), 1),
get_feature('trails', 'Trail ID', @element)
),
array_foreach(
array_filter(@my_arr, attribute(@element, 'Schwierigkeit')='black') , geometry(@element)))))
, NULL)