wiki:UsersWikiCreateFishnet

Version 1 (modified by Mike Taves, 12 years ago) ( diff )

add function

Create Fishnet Grid

Make a 2D grid of rectangular polygons, which is sometimes called a fishnet grid.

Use the following source:

CREATE OR REPLACE FUNCTION ST_CreateFishnet(
        nrow integer, ncol integer,
        xsize float8, ysize float8,
        x0 float8 DEFAULT 0, y0 float8 DEFAULT 0)
    RETURNS SETOF geometry AS
$$
SELECT ST_Translate(cell, j * $3 + $5, i * $4 + $6)
FROM generate_series(0, $1 - 1) AS i,
     generate_series(0, $2 - 1) AS j,
(
SELECT ('POLYGON((0 0, 0 '||$4||', '||$3||' '||$4||', '||$3||' 0,0 0))')::geometry AS cell
) AS foo;
$$ LANGUAGE sql IMMUTABLE STRICT;

Example:

SELECT ST_Collect(cells)
FROM ST_CreateFishnet(3,4,10,10) AS cells;

http://i.stack.imgur.com/vQY0Z.png

Note: See TracWiki for help on using the wiki.