QGIS – Count Points in Adjacent Polygons

countfields-attributesoverlapping-featurespoint-in-polygonqgis

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

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.

enter image description here

Best Answer

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

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

enter image description here

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

enter image description here

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.

enter image description here enter image description here

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

Related Question