How to create, inside a polygon, a regular grid of point spaced x,y in postgis? Like the example:
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:
CREATE OR REPLACE FUNCTION makegrid(geometry, integer)
RETURNS geometry AS
'SELECT ST_Collect(ST_POINT(x,y)) FROM
generate_series(floor(st_xmin($1))::int, ceiling(st_xmax($1)-st_xmin($1))::int, $2) as x
,generate_series(floor(st_ymin($1))::int, ceiling(st_ymax($1)-st_ymin($1))::int,$2) as y
where st_intersects($1,ST_POINT(x,y))'
LANGUAGE sql
To use it you can do:
SELECT makegrid(the_geom, 1000) from mytable;
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:
SELECT (ST_Dump(makegrid(the_geom, 1000))).geom as the_geom from mytable;
HTH
Nicklas
No comments:
Post a Comment