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.

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

INSERT INTO hex_grid (the_geom)
SELECT st_translate(the_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 the_geom
   UNION
   SELECT ST_Translate('POLYGON((0 0,64 64,64 128,0 192,-64 128,-64 64,0  0))'::geometry, 64, 128)  as the_geom
) as two_hex

Last modified 3 years ago Last modified on Dec 6, 2013 12:32:16 AM