wiki:UsersWikiCreateFishnet

Create Fishnet Grid

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

Use the following source for a set returning function:

CREATE OR REPLACE FUNCTION ST_CreateFishnet(
        nrow integer, ncol integer,
        xsize float8, ysize float8,
        x0 float8 DEFAULT 0, y0 float8 DEFAULT 0,
        OUT "row" integer, OUT col integer,
        OUT geom geometry)
    RETURNS SETOF record AS
$$
SELECT i + 1 AS row, j + 1 AS col, ST_Translate(cell, j * $3 + $5, i * $4 + $6) AS geom
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;

Where input parameters:

nrow integer
number of rows in y-direction
ncol integer
number of columns in x-direction
xsize float8
cell size length in x-direction
ysize float8
cell size length in x-direction
x0 float8 (optional)
origin offset in x-direction; DEFAULT is 0
y0 float8 (optional)
origin offset in y-direction; DEFAULT is 0

Returning output parameters are:

row integer
row number, starting from 1 at the bottom
col integer
column number, starting from 1 at the left
geom geometry
grid cell geometry

Example:

SELECT ST_Collect(cells.geom)
FROM ST_CreateFishnet(4, 6, 10, 10) AS cells;

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

Last modified 4 years ago Last modified on Jun 16, 2013 9:25:18 PM