ST_Hexagon
What is ST_Hexagon?
ST_Hexagon is a PostGIS function (added in 3.1) that returns a single hexagon polygon at the specified integer grid coordinate, matching the tiling produced by ST_HexagonGrid.
ST_Hexagon(float size, integer cell_i, integer cell_j, geometry origin = null) → geometrysize is the edge length in CRS units. (cell_i, cell_j) are the grid indices. origin (optional) anchors the grid — if omitted, the grid is aligned to the world origin in the input SRID. The output polygon inherits the SRID of origin.
When would you use ST_Hexagon?
Use ST_Hexagon when you already know the (i, j) cell coordinate — typically stored as a key from a previous ST_HexagonGrid materialization — and want to reconstruct the geometry on demand without generating the full grid:
1SELECT h_i, h_j, count,
2 ST_Hexagon(500, h_i, h_j,
3 ST_Transform(ST_MakePoint(0,0), 3857)) AS geom
4FROM hex_counts;This pattern is common in tile servers and dashboards, where storing (i, j) + a count is far cheaper than storing a polygon per row, and the polygon is reconstructed only at render time.
FAQs
Does ST_Hexagon produce the same geometry as ST_HexagonGrid for matching (i, j)?
Yes, as long as size and origin match. That round-tripability is the reason to store (i, j) rather than the full polygon.
What CRS should I use?
Any CRS where size has meaningful units — usually a projected metre-based CRS. Lat/lon (EPSG:4326) leads to hexagons whose width varies by latitude.
Can I offset the grid?
Yes — supply an origin geometry whose SRID and coordinates anchor cell (0, 0). Omit the argument to use the default world-origin alignment.
When should I prefer ST_Hexagon over ST_HexagonGrid?
ST_Hexagon is for reconstructing one cell by known key; ST_HexagonGrid is for enumerating a full tessellation. Use ST_Hexagon in dashboards and APIs, ST_HexagonGrid in bulk spatial binning jobs.