路线的最短距离(Python)

时间:2017-03-18 20:11:43

标签: python-3.x geospatial latitude-longitude shapely shortest

我有一个带有纬度和长信息的位置

loc = (28.469723, 77.065292)

和由五个纬度长对给出的路线(折线)

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

有一种简单的方法可以计算从locroute的最短距离吗?

1 个答案:

答案 0 :(得分:0)

然后你可以像那样解决它。

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

输出仍然是相同的,因为该点意外地是最近的,但是通过改变该点,您将看到它在最接近的线上找到一个点。