wiki:UsersWikiGenerateHexagonalGrid

Generate Hexagonal Grid

The following SQL generates a table containing a set of polygons forming a grid of hexagonal cells.

Substitute the values in the generate_series calls with the desired min/max X/Y extents. -- update to use geom column name 12mar18

CREATE TABLE hex_grid (gid serial not null primary key);
SELECT addgeometrycolumn('hex_grid','geom', -1, 'POLYGON', 2);

INSERT INTO hex_grid (geom)
SELECT st_translate(geom, x_series, y_series)
from generate_series(0 - 128, 10000 + 128, 128) as x_series,
generate_series(0 - 128, 10000 + 128, 256) as y_series,
(
   SELECT 'POLYGON((0 0,64 64,64 128,0 192,-64 128,-64 64,0 0))'::geometry as geom
   UNION
   SELECT ST_Translate('POLYGON((0 0,64 64,64 128,0 192,-64 128,-64 64,0  0))'::geometry, 64, 128)  as geom
) as two_hex

Last modified 5 months ago Last modified on Mar 12, 2018 5:03:23 PM