# How to label a point layer with the percentage of points within another polygon layer?

I would like to find a method using Geometry generator to label a point layer with the percentage of points inside another polygons layer.

I'm working on implementing a solution based on the COUNT and OVERLAY_WITHIN functions, but for the moment I can't get the solution. An approach of my failed expression is this:

``````count(within(\$geometry,collect_geometries(overlay_within('POLYGON', \$geometry))))*100/num_geometries(\$geometry)
``````

With this result:

The idea I am looking for is as shown in the screenshot: label point layer in the centroid of each polygon with the percentage of points:

From what you say, it seems like you wanted to start from the `points` layer. As you want the number of points within the `polygon` layer, it's easier to place the logic in the `polygon` layer (also allowing you to use the native label positioning features, centroid by default).

You can use this formula in the `polygon` layer Label (or as a Virtual Field in the `polygon` Attribute Table)

``````to_string(

round(

array_length(overlay_intersects('points', \$id)) ---number of points in the polygon
/
aggregate('points','count',\$id) ---number of points in points layer

,2)*100

) ||  ' %'
``````

If you want this to automatically apply to any new Point layer, you can update the code to be the following :

``````  to_string(

round(

array_length(overlay_intersects(  array_to_string(  array_filter( @map_layer_ids ,@element!=@layer_id)), \$id)) ---number of points in the polygon
/
aggregate(  array_to_string(  array_filter( @map_layer_ids ,@element!=@layer_id)),'count',\$id) ---number of points in points layer

,2)*100

) ||  ' %'

``````

with the `point` layer name replaced by `array_to_string( array_filter( @map_layer_ids ,@element!=@layer_id))` which is the only active layer besides the polygon one. If there are other 'static' layers you want to exclude, you can add them to the filter.

Swithching to another point layer, without changing the Style in the polygon layer:

If you have static layers that you want to keep active: add them to the layer filter. Set the filter part to:

`@element not in (@layer_id, 'any other layer name you need to remove', 'any other layer to remove',...)`

`@layer_id' filters the polygon layer, then add a comma separated list of layers you want to keep active.

For instance I added an OtherLayer displayed on the map. Use the @map_layer_ids to identify its `id` and added it to the filter list

`to_string(round(array_length(overlay_intersects( array_to_string( array_filter( @map_layer_ids ,@element not in (@layer_id,'OtherLayer_a9983d8b_9323_4fac_83de_e8e24e78c8a1'))), \$id)) /aggregate( array_to_string( array_filter( @map_layer_ids ,@element not in (@layer_id,'OtherLayer_a9983d8b_9323_4fac_83de_e8e24e78c8a1'))),'count',\$id) ,1)*100) || ' %'`

• Thanks, but that's not what I'm trying to find. I clarify that the idea is to label the points layer on the centroid of polygons layer Jan 23, 2023 at 10:55
• Ok, can you explain why you specifically want the Point layer to be labeled and not the Polygon layer? If there is one label per polygon and computed per polygon, geometry based on polygon , why not label the polygon layer ? Jan 23, 2023 at 11:10
• Yes, the requirement is to visualise the label in real time while capturing the data. Data is captured in different points layers files (gpkg) and if the polygon layer is used, the expression would have to be replaced each time Jan 23, 2023 at 11:21
• You can remove static 'make-up' layers that you want to keep by adding them to the filter list (see updated answer) Jan 23, 2023 at 17:40
• Works perfectly Jan 23, 2023 at 17:52