More

Find distance and heading from a point to a polygon in lat, lon coordinates


I have counties mapped using polygons in Shapely with latitude/longitude for coordinates. Given a complex polygon x and single point y, how would I find the shortest distance between x and y as well as the heading needed?

Using the distance function in Shapely, I can get the closest distance, but I'm unable to come up with a fast solution to get the heading. I tried determining the closest point, but this solution was unreasonably slow to run on 10 polygons.


I've no idea if this works or if it's fast enough, but I'd try this:

  • compute distance (Z) between poly A and point B
  • build a buffer geometry (C) around point B of "radius" Z
  • compute the intersection between poly C and poly A as geometry D
  • compute centroid of geometry D
  • compute heading between D and A

What makes or breaks this approach is if the buffer intersects A or not (because buffer is a circle's approximation, it might not).

If you don't get an intersection, you could increase either:

  • the buffer size or
  • the buffer's accuracy or
  • both

until you get an intersection.

Here's a diagram of a buffer slightly bigger then the distance to illustrate what I mean: