4

I have a location with its lat and long information

loc = (28.469723, 77.065292)

and a route (polyline) given by five lat long pairs

route = [(28.478324, 77.093916), (28.471647, 77.092457), (28.465498, 77.086105), (28.461273, 77.077651)]

Is there a simple way to calculate the shortest distance in km from loc to route?

Saquib
  • 273
  • 3
  • 9

1 Answers1

0

Then you could solve it like that.

from geopy.distance import vincenty
from shapely.geometry import LineString, Point, LinearRing

loc = (28.469723, 77.065292)
routePoints = [(28.478324, 77.093916), (28.471647, 77.092457), (28.465498, 77.086105), (28.461273, 77.077651)]

point = Point(loc)
route = LineString(routePoints)

pol_ext = LinearRing(route.coords)
d = pol_ext.project(point)
p = pol_ext.interpolate(d)
closest_point_coords = list(p.coords)[0]

distance = vincenty(closest_point_coords, loc).km

print(distance)

1.5303772782641438

The output is still the same because the point is accidentally the nearest, but by changing the point you will see that it finds a point on the line that is closest.

Paul
  • 500
  • 3
  • 8
  • 2
    Thanks, but I think this will give me the distance from nearest out of these five coordinates... What I need is the shortest distance from the path given by these points. – Saquib Mar 19 '17 at 05:01