How to create, inside a polygon, a regular grid of point spaced x,y in postgis? Like the example:

|
How to create, inside a polygon, a regular grid of point spaced x,y in postgis? Like the example:
|
||||
|
|
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:
HTH Nicklas |
||||
|
This algorithm should be fine:
where 'polygon' is the polygon and 'resolution' is the required grid resolution. To implement it in PostGIS, the following functions may be needed:
Good luck! |
||||
|
I have picked up Nicklas Avén makegrid function code and made it a bit more generic by reading and using the srid from the polygon geometry. Otherwise using a polygon with a defined srid, would give an error. The function:
To use the function is done exactly as Nicklas Avén wrote:
or if you want one point per row:
Hope this will be usefull for someone. Alex |
||||
|
|
|
So my fixed version:
Usage:
|
|||
|
|
People using a wgs84 geometry will probably have trouble with this function since
only return integers. Except for very big geometries such as countries (that are laying on multiple lat, lng degrees), this will cause to collect only 1 point which is most of the time not even intersecting the geometry itself... => empty result ! My trouble was I can not seem to use generate_series() with a decimal distance on floating numbers such as those WSG84... This is why I tweaked the function to get it working anyway :
Basically exactly the same. Just multiplying and dividing by 1000000 to get the decimals into the game when I need it. There is surely a better solution to achieve that. ++ |
|||||
|