[GIS] How to count points within adjoining hex grid cells

hexagonal gridqgisvector-grid

I am currently using QGIS, and have developed a vector grid (hexagonal). I have a separate layer with point data. For each grid cell I need to count the number of points in that cell (but also all adjoining cells).

I searched for this problem on Google and Stack Exchange, but no solutions I've been able to find nothing.

Some diagrams to explain, below.

Image 1: I want to calculate a value for this grid cell

Image 2: Value for selected cell = the number of points within this cell + all adjoining cells

I want to calculate a value for this grid cell

I want the value for the cell to = the number of points within this cell and all adjoining cells

Best Answer

The first part of your question is easy. Go to Vector->Analysis Tools->Points in Polygon. This tool will iterate over all your polygons and add a column (called 'PNTCNT' by default) with the number of points in each polygon.

This tool gives you just a simple count of the number of points in each polygon as the name suggests. You can refine this by using one of the tools from the Processing ToolBox such as: Count points in polygon(weighted) or Count unique points in polygon.

Your second requirement is a little more complicated to automate but fortunately somebody has already done this for you! First perform the basic Point-In-Polygon count as above. Then follow this tutorial, using the script provided and change the sum-field in the script to 'PNTCNT'. This script basically iterates over your polygons, selecting each in turn and then re-selecting all polygons that intersect with the current one (as per your second image). It then iterates over the new set of polygons, summing the values of your point-count field for all neighbours.

Related Question