# Counting points in adjacent polygons using QGIS

I would like to sum up the amount of points in a polygon and its adjacent polygons. If possible, I would like to add a certain `"weight"` to the points of the adjacent polygons. Here is a visual example:

Step 1. Count all points in the yellow hexagon

Step 2. Sum all points in the yellow hexagons and add them to the count of the single hexagon, but multiply this count by a certain factor X (for example 0.5) to make the `"weight"` of the single hexagon heavier.

Using calculate field you could use the following expression for points inside the polygon:

``````array_length(overlay_contains( 'points',\$geometry))
``````

You can see in the preview the results for the selected hexagon which really does have 3 points inside it.

To calculate the number of points within the neighboring hexagons you will need the following expression:

``````num_geometries(
intersection(
collect_geometries(
aggregate('points',
'array_agg',
\$geometry)
),
collect_geometries(overlay_intersects('Grid',\$geometry))
))
``````

You can see the result again for the same hexagon, which has 6 neighbors that have 10 points between them. You have to make sure the geometry you want to count is the first one in the `intersection` function.

Once you have both numbers you can use any factor you want on them.

• Thank you very much for your detailed answer!! Would you also know how to count the points in all hexagons that are 1 hexagon away from the central one? So counting all points in the polygons that are around the adjacent hexagons. In the pictures you shared this would mean the sum of 33, 43, 32, 63, 74, 75, 66 ... Dec 1, 2022 at 13:42
• You just need to get that row of polygons too. You would need to use aggregate, exclude the center geometry and filter by intersection with the the original neighbors. Dec 1, 2022 at 17:47
• Hey, sorry for coming back to this, but somehow I can't get it to work.. Here are the steps that I'm following: 1. use "Count points in polygon" tool. 2. Select the new "Count" layer and hit the Calculate Field. 3. Paste this: array_length(overlay_contains( 'points',\$geometry)) into the expression field, but replace 'points' with 'Count'. The value in the preview is 0 for each polygon, which is incorrect.. Thank you very very much for your help, I truly appreciate it! Dec 4, 2022 at 10:52
• Also, sorry if this is a very basic question, but how do you get the 'name' of each polygon printed on each of them to be able to identify them? Dec 4, 2022 at 10:52
• My method is for using the field calculator, you don't need any additional tool for it. and the 'name' is just a simple label with the "id" field. Dec 5, 2022 at 11:11