I am using the following expression to count and label the number of geometries of a layer (POINTS1_TO_INCLUDE) that intersect with polygons of another layer (POLYGONS):

@map_layer_ids,@element not in (@layer_id,'POLYGONS_31860b0b_1cc5_4deb_9016_5fa3bb37ba18'))), $id))))

The result is as follows:

enter image description here

As can be seen in the screenshot, the number of geometries of the POINTS1_TO_INCLUDE layer that intersect with the POLYGONS layer are labeled and the geometries POINTS_TO_EXCLUDE layer are excluded from counting.

The objective is to be able to add new layers with any layer name and keep the expression working. Now the expression stops working when I add a new layer, as you can see in the screenshot:

enter image description here

Any idea how to get the expression to work as shown below:

enter image description here

  • Does this answer your question? Counting points in polygon to analyze data using QGIS Commented May 15, 2023 at 12:45
  • Does this answer your question? gis.stackexchange.com/questions/417519/… Commented May 15, 2023 at 12:48
  • 1
    The problem is when you add a 4th layer, the output of array_filter is 2 layers, instead of 1. This in turn breaks the overlay_intersects function. I'm not aware of any functions in the Expression Builder that will operate on multiple layers without specifying each layer - which is not to say there aren't any. But you might need to look at a different approach. Commented May 15, 2023 at 13:02
  • Thanks for the quick response and clarifications. Regarding the solution based on virtual_layer, I find the disadvantage that you have to include in the SQL statement the name of the each layers to be included. It is not viable in my project since I would have to add in my QGIS project about 100 independent layers with different names Regarding the solution based on 'aggregate' function, I have already worked with it but without success. For performance and for save time I think in geometry generator with the functions 'overlay_intersect' and the operator is 'not in' Commented May 15, 2023 at 13:17

1 Answer 1


Use this expression. It calculates the no. of points from all point layers that have include in their name, based on variable @layers that returns the map layers in the current project as a list (see below for further explanations):

        @layers ,
            if (
                regexp_match (layer_property(@element, 'name'), 'include'),
                overlay_contains (@element,$id),

enter image description here

  1. @layers returns a list of all layers
  2. use array_foreach to loop through all layers
  3. get the layer's name layer_property()
  4. use regexp_match() to check if the string include is part of the layer's name
  5. if this condition is fulfilled, use overlay_contains() to get an array with all features (points) inside the polygon
  6. if the condition is not fulfilled, create an empty array: array()
  7. calculate the length of the array (how many elements it contains) with array_length()
  8. finally, sum up all these lengts using array_sum()
  • 1
    Absolutely out-of-the-box and creative solution/approach. A way to consider the problem in a different and original way, instead of following my traditional or expected ideas, with an optimal result in my project Commented May 15, 2023 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.