How to count points within adjoining hex grid cells?
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.
You can do this in a spatial data such as Spatialite with these queries:
To get a count of the points only within each grid cell:
SELECT g.id, Count(p.id)
FROM points AS p, grid AS g
WHERE ST_Contains(g.geometry, p.geometry)
GROUP BY g.id;
and to query all touching grid cells as well, try:
SELECT g.id, Count(p.id) he
FROM points AS p, grid AS g, grid AS g2
WHERE ST_Contains(ST_Union(g.geometry, g2.geometry), p.geometry) AND
ST_Touches(g.geometry, g2.geometry) AND
g.id <> g2.id
GROUP BY g.id;
It's important to note that the second query will take a long time if there are many cells in your grid. So it would be important to add spatial indices and to use them in the query.