Looking for an implementation in PostGIS for generating a hexagonal grid that covers the the whole planet in order to aggregate data over each hexagon.
Any pointer in the right direction would be of great help!
End product: - A table containing the center point for each hexagon in a hexagonal grid that covers the whole world. - The hexagons have a fixed area
Some time ago I adapted a
function
to generate hexagons that might be exactly what you're looking for. It takes the parameters cell width, and the coordinates for southwest and northeast corners, and generates a hexagonal grid.This function returns a table with the columns
_gid
and_geom
, containing an identifier and the geometry for each hexagon, respectively.With these parameters, the function generates a grid with 98192 hexagons covering the whole world:
Here a bit closer, so that you can see the grid:
If you're only interested in covering land, you can create a subset of these hexagons based on a geometry of your choice using
ST_Intersects
:This query will create a subset with a grid containing 35911 hexagons, which intersect with the geometries from the world map:
The world map used in this answer can be downloaded as shapefile
here
.Generating the centroids for each hexagon isn't a big deal either (see
ST_Centroid
):