If you're looking for a PostGIS function that will tell you a point that's inside your polygon then the ST_PointOnSurface function may give you what you need.
SELECT
ST_AsText(ST_PointOnSurface('POLYGON((0 0, 0 5, 5 5, 5 0, 0 0))'::geometry));
st_astext
----------------
POINT(2.5 2.5)
(1 row)
Here is a set returning function ST_CreateFishnet
that creates a 2D grid of polygon geometries:
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 nrow
and ncol
are the number of rows and columns, xsize
and ysize
are the lengths of the cell size, and optional x0
and y0
are coordinates for the bottom-left corner.
The result is row
and col
numbers, starting from 1 at the bottom-left corner, and geom
rectangular polygons for each cell. So for example:
SELECT *
FROM ST_CreateFishnet(4, 6, 10, 10) AS cells;
row | col | geom
-----+-----+--------------------------------
1 | 1 | 0103000000010000000500000000...
2 | 1 | 0103000000010000000500000000...
3 | 1 | 0103000000010000000500000000...
4 | 1 | 0103000000010000000500000000...
1 | 2 | 0103000000010000000500000000...
2 | 2 | 0103000000010000000500000000...
...
3 | 6 | 0103000000010000000500000000...
4 | 6 | 0103000000010000000500000000...
(24 rows)
Or to make a single geometry collection for the full grid:
SELECT ST_Collect(cells.geom)
FROM ST_CreateFishnet(4, 6, 10, 10) AS cells;
You can add the x0
/ y0
origin offsets (these defaulted to zero).
Best Answer
You do that with generate_series.
If you don't want to manually write where the grid is to start and stop, the easiest is to create a function.
I have not tested the below properly, but I think it should work:
To use it you can do:
where the first argument is the polygon you want the grid in, and the second argument is the distance between the points in the grid.
If you want one point per row you just use ST_Dump like: