0

There is the famous ST_ClosestPoint() return the closest point inside polygon to some outside point.

I'm looking the inverse problem: I have a location inside polygon, and I want to get the closest location outside the polygon.

I started thinking that first step should be to find the closest point on the boundary of the polygon. Although I don't know how to achieve this, I'm not sure how to continue even from this stage (considering I have the closest boundary point to my inside point).

EDIT: What about comparing results from ST_Line_Locate_Point() running on all lines of the polygon and choosing the minimum: This gives me the closest point on the border line

WITH border_closest_points AS (
        SELECT ST_Line_Locate_Point(border_lines.geom, ST_GeomFromText('POINT(lon lat)', 4326)) AS fraction,  
        ST_Line_Interpolate_Point(border_lines.geom, ST_Line_Locate_Point(border_lines.geom, ST_GeomFromText('POINT(lon lat)', 4326))) AS closest_point_on_line
        FROM border_lines   
)
SELECT border_closest_points.closest_point_on_line AS geom, ST_Distance(ST_GeomFromText('POINT(lon lat)', 4326), border_closest_points.closest_point_on_line) AS distance
FROM border_closest_points
ORDER BY distance ASC LIMIT 1;
  • Does "point" here mean a vertex of the surrounding polygon? I think a small image could clarify what you want to do. – user30184 Aug 22 '15 at 15:04
  • It can be vertex but not necessary... what is the closest... meanwhile my suggestion with ST_Line_Locate_Point() is going well, so I do have point on the border. But how can i now to "move it little bit outside"? – michael Aug 22 '15 at 15:08
3

You can dump the border of polygon with ST_ExteriorRing, then it will find the closest point on the resulting linestring, if you want a point that lies just a little bit outside use ST_Buffer first to add a little bit to the original polygon.

 WITH polygons AS(
    SELECT ST_Buffer(ST_GeomFromText('POINT(0 0)', 4326),2) as geom-- this circle will pretend to be a border polygon
 )
 SELECT 
 ST_AsTEXT(ST_ClosestPoint(ST_ExteriorRing(ST_Buffer(polygons.geom,0.0000000001))
        ,ST_GeomFromText('POINT(1 0)', 4326)))
FROM polygons;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.