0

I want to find the closest distance from one co-ordinate to the nearest end of a region. Like the image below, how can I calculate the closest distance from red dot to end of the region polygon? So basically the distance of red line.

enter image description here

I used ST_DISTANCE(region_polygon_1, red_dot_coordinate_1) but it returns 0.

What would be the problem with my st_distance function?

I used ST_DISTANCE(region_polygon_1, red_dot_coordinate_1) to get the distance of the red line but it returns 0

SternK
  • 11,649
  • 22
  • 32
  • 46
jjun
  • 3
  • 1
  • Actually the red line is not the shortest distance to the region, but to one of the points that define the polygon. – Mark Mar 17 '23 at 06:04
  • Thank you for the details! Is there any way to calculate the shortest distance? – jjun Mar 17 '23 at 06:31

1 Answers1

0

Polygon includes all the internal points, so the distance from an internal point to polygon is indeed 0.

What you want is the distance to the boundary of the polygon, so what you need is

ST_DISTANCE(ST_Boundary(region_polygon_1), red_dot_coordinate_1) 

If the polygon has holes, you'll need to determine whether to ignore those or find shortest distance to those - use either ST_ExteriorRing or ST_Boundary

Michael Entin
  • 7,189
  • 3
  • 21
  • 26