wiki:UsersWikiGenerateHexagonalGrid

Version 2 (modified by veggivore, 15 years ago) ( diff )

Cleaning up my ancient code.

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 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

Note: See TracWiki for help on using the wiki.