wiki:UsersWikiGenerateHexagonalGrid

Version 1 (modified by pierre, 16 years ago) ( diff )

--

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, the_geom geometry not null);

 INSERT INTO hex_grid (the_geom)
 SELECT 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 one_hex

Note: See TracWiki for help on using the wiki.