QGIS – Detecting Largest Areas and Generating Polygons from Line Geometries

geometry-generatorlinepolygon-creationqgis

Starting from a layer with points geometry, the Geometry generator is used to create a line geometry layer that connects with the 8 nearest points. The function used is the following:

collect_geometries (
        array_foreach (
            overlay_nearest ('layer_of_points',
                $geometry,
                limit:=8,
                max_distance:=100
            ),
            make_line (
                $geometry,
                @element
    )
  )
)

The result obtained, as shown in the attached screenshot, is a set of lines. From this lines I would like to build a geometry expression that allows to detect and generates the 2 largest area polygons (features in blue color on screenshot) that arise from the triangulation of connected lines with points.

enter image description here

Best Answer

I am not aware of any way to do this with the geometry generator directly. But I can propose the following process to achieve the intended result.

  1. use "Polygonize" processing tool on your line layer.

enter image description here enter image description here If step 1. failed you may need to run "Multipart to singleparts" first, then you need to run "Check Validity", and with the result you need to run "Split lines by lines"

  1. set a rule-based symbology with this expression as a filter:
array_contains(
     array_slice(
        aggregate(@layer, 'array_agg', $area, order_by:=-$area), 
        0, 
        1
    ),
    $area 
)

enter image description here