QGIS Labeling – How to Label Number of Points in Concave Hull Without Duplicates

expressionlabelingqgis

From a layer of points with the "CLASS" field reported with 3 different values, I apply the following expression to draw the concave polygon for each different value:

concave_hull(
            collect(
                $geometry,
                group_by:=overlay_nearest(@layer,"CLASS"))
                ,0.2)

Next, I use an expression to label the percentage of points within each concave hull polygon:

round(
with_variable(
    'num',
    concave_hull(
            collect(
                $geometry,
                group_by:=overlay_nearest(@layer,"CLASS"))
                ,0.2),
    aggregate(
        @layer,
        'count',
        1,
        within ($geometry, @num) or intersects ($geometry, @num)
    )
)

/ count('')  * 100.0,0) || ' %'

As can be seen, the total value is 103 % because it seems that there is a point that is count by duplicated in two different concave hull polygons:

enter image description here

How could I fix the label expression so that it takes this possibility and so that the percentage is balanced?

Best Answer

As you want to number the points per hull and the hull is based on the value in the class field, you can base your expression on this and don't have to complicate yourself with the hull's geometries:

round (count (class,group_by:=class) / count (class) * 100, 0)

enter image description here


By the way, better construct your hull with a simpler expression:

concave_hull(
    collect(
    $geometry,
    group_by:="class"),
    0.2
)