Skip to content

Instantly share code, notes, and snippets.

@ka7eh
Created June 12, 2017 23:22
Show Gist options
  • Save ka7eh/794e86df25b7d424efd6a4958a37ffe7 to your computer and use it in GitHub Desktop.
Save ka7eh/794e86df25b7d424efd6a4958a37ffe7 to your computer and use it in GitHub Desktop.
Creates a grid of `xsize*ysize` cells in a given extent
CREATE OR REPLACE FUNCTION ST_CreateFishNet(
IN xsize double precision,
IN ysize double precision,
IN north double precision DEFAULT 0,
IN east double precision DEFAULT 0,
IN south double precision DEFAULT 1,
IN west double precision DEFAULT 1)
RETURNS SETOF geometry AS
$BODY$
SELECT ST_Translate(cell, $6 + (i * $1), $5 + (j * $2)) AS geom
FROM generate_series(0, (($4 - $6) / $1)::integer) AS i,
generate_series(0, (($3 - $5) / $2)::integer) AS j,
(
SELECT ('POLYGON((0 0, 0 '||$2||', '||$1||' '||$2||', '||$1||' 0,0 0))')::geometry AS cell
) AS foo;
$BODY$
LANGUAGE sql IMMUTABLE STRICT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment