QGIS – Drawing Line to Centroid of Concave Hull Composed by Group of Points in Same Polygon

concave hullexpressiongeometry-generatorqgisvector

I am using the following Geometry Generator expression with which, for each point geometry, I draw a line to the centroid of the polygon the point is within:

make_line (
        $geometry,
        centroid (
            array_first(
                overlay_nearest(
                    'POLY',
                    $geometry
                )
            )
        )
    )

With the following result:

enter image description here

Instead of drawing the lines to the centroid of the polygon, I would like to draw the lines to the centroid of the concave hull around the points within the same polygon.

I've tried to manually recreate this goal I'm trying to do in the following image:

enter image description here

Best Answer

The trick is to first collect the points within each polygon with the functions collect() and overlay_within(). Use this expression:

centroid (
    concave_hull( 
        collect(
            $geometry, 
            group_by:=overlay_within('POLY',$id)
        ),
        0.4
))

Red point created with the expression from above; red line: concave hull, created with the same expression, without the centroid() part: enter image description here


To draw a line from the initial points to the centroid of the concave hull, simple add make_line ($geometry, at the beginning of the expression and add a closing bracket ) at the end of the expression, see:

enter image description here