Wednesday 12 June 2019

How can I find a point inside a polygon in PostGIS?


How can I find a point that is guaranteed to be within a given polygon in PostGIS?


I am aware of the ST_Centroid function. However, the centroid is not always within a polygon, see below:


a centroid lying outside a polygon


Furthermore, I would like to avoid using a point that is on the polygon boundary, but rather want one that is inside the boundary (and not inside a hole in donut shaped polygons).



Answer




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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...