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.
It's true that you cannot evenly distribute hexagons across the 4326 globe, but you can get close using a discrete global grid. The introduction of 12 pentagons allows you to tile the rest of the globe quite nicely with hexagons. In particular, my R package dggridR allows you to easily construct such a grid:
library(dggridR)
#Generate a global grid whose cells are ~100,000 km^2
dggs <- dgconstruct(area=100000, metric=FALSE, resround='nearest')
#Save the cells to a KML file for use in other software
gridfilename <- dgearthgrid(dggs,savegrid=TRUE)
The resulting KML file will have each cell labeled with a unique id.
We can also plot the grid to see that it is behaving as expected:
#Retrieve the same grid in a format useful for plotting
grid <- dgearthgrid(dggs,frame=TRUE,wrapcells=TRUE,savegrid=FALSE)
#Get country outlines
countries <- map_data("world")
#Plot on a spherical map
ggplot() +
geom_polygon(data=countries, aes(x=long, y=lat, group=group), fill=NA, color="black") +
geom_polygon(data=grid, aes(x=long, y=lat, group=group), fill="blue", alpha=0.4) +
geom_path (data=grid, aes(x=long, y=lat, group=group), alpha=0.4, color="white") +
coord_map("ortho", orientation = c(-38.49831, -179.9223, 0))+
xlab('')+ylab('')+
theme(axis.ticks.x=element_blank())+
theme(axis.ticks.y=element_blank())+
theme(axis.text.x=element_blank())+
theme(axis.text.y=element_blank())
![A discrete global grid generated by dggridR](https://i.stack.imgur.com/1nXsW.png)
Note that one of the 12 pentagons is visible. These are, by default, orientated to be in out-of-the-way places. Future version of the package will allow you to orientate them.
The package is built on top of Kevin Sahr's DGGRID software. I haven't yet looked into how the cells are addressed for identifying neighbours (a future version of the package should address this), but it may be that the following publication would shed light on it:
Sahr, Kevin. “Location Coding on Icosahedral Aperture 3 Hexagon Discrete Global Grids.” Computers, Environment and Urban Systems 32.3 (2008): 174–187. CrossRef. Web.
Good luck!
Best Answer
Step 1. Create a grid with the "Create grid" geoalgorithm
Step 2. Use the "Geometry by expression" from the Processing Toolbox (Ctrl+Alt+T).
With the following expression:
and get the output:
In case if more hexagons are required apply the following expression:
and get a corresponding answer:
Keep in mind that each output feature is a MultiPolygon, so the application of the "Multipart to singleparts" tool can be useful.
References: