Name

ST_HexagonGrid — Returns a set of hexagons and cell indices that completely cover the bounds of the geometry argument.

Synopsis

setof record ST_HexagonGrid(float8 size, geometry bounds);

Description

Starts with the concept of a hexagon tiling of the plane. (Not a hexagon tiling of the globe, this is not the H3 tiling scheme.) For a given planar SRS, and a given edge size, starting at the origin of the SRS, there is one unique hexagonal tiling of the plane, Tiling(SRS, Size). This function answers the question: what hexagons in a given Tiling(SRS, Size) overlap with a given bounds.

The SRS for the output hexagons is the SRS provided by the bounds geometry.

Doubling or tripling the edge size of the hexagon generates a new parent tiling that fits with the origin tiling. Unfortunately, it is not possible to generate parent hexagon tilings that the child tiles perfectly fit inside.

Availability: 3.1

Example: Counting points in hexagons

To do a point summary against a hexagonal tiling, generate a hexagon grid using the extent of the points as the bounds, then spatially join to that grid.

WITH bounds AS (
 	SELECT ST_SetSRID(ST_EstimatedExtent('pointtable', 'geom'), 3857) AS geom
 )
 SELECT Count(*), hexes.geom
 FROM pointtable pts
 JOIN ST_HexagonGrid(1000, bounds.geom) hexes
 GROUP BY hexes.geom

Example: Generating hex coverage of polygons

If we generate a set of hexagons for each polygon boundary and filter out those that do not intersect their hexagons, we end up with a tiling for each polygon.

Tiling states results in a hexagon coverage of each state, and multiple hexagons overlapping at the borders between states.

WITH hexes AS (
	SELECT admin1.gid,
	       (ST_HexagonGrid(100000, admin1.geom)).* AS hex
	FROM admin1
	WHERE adm0_a3 = 'USA'
)
SELECT hexes.*
FROM hexes
JOIN admin1 USING (gid)
WHERE ST_Intersects(admin1.geom, hexes.geom)

See Also

ST_TileEnvelope, ST_SquareGrid