[英]Nearest latitude and longitude points in python
I have a list of stations in List 1 and List 2 How do I find the closest stations to List 1 from List 2 ?我有列表 1 和列表 2 中的站点列表 如何从列表 2 中找到离列表 1 最近的站点?
List 1清单 1
SS No Latitude Longtitude 977 23.141747 53.796469 946 23.398398 55.422916 742 23.615732 53.717952 980 23.633077 55.567046 660 23.6504 54.4007 SS 无纬度 经度 977 23.141747 53.796469 946 23.398398 55.422916 742 23.615732 53.717952 980 23.633077 .66507 54057 54057
List 2清单 2
SS No Latitude Longtitude 962 23.657571 53.703683 745 23.671971 52.955976 743 23.766849 53.770344 978 23.847163 52.809653 748 23.942166 52.16236 744 23.955817 52.790424 760 23.984592 55.55764 945 24.030256 55.844842 894 24.03511 53.891547 856 24.741601 55.80063 893 24.04123 53.899958 387 24.059988 51.748138 675 24.061578 53.417912 664 24.063978 51.76195 SS没有纬度经度962 23.657571 53.703683 745 23.671971 52.955976 743 23.766849 53.770344 978 23.847163 52.809653 748 23.942166 52.16236 744 23.955817 52.790424 760 23.984592 55.55764 945 24.030256 55.844842 894 24.03511 53.891547 856 24.741601 55.80063 893 24.04123 53.899958 387 24.059988 51.748138 675 24.061578 53.417912 664 24.063978 51.76195
I can do this manually by mapping them on PowerBI but I am looking for a scalable solution and Prefer Python.我可以通过将它们映射到 PowerBI 来手动执行此操作,但我正在寻找可扩展的解决方案并且更喜欢 Python。
This is quite similar to the previous question这与上一个问题非常相似
Getting distance between two points based on latitude/longitude 根据纬度/经度获取两点之间的距离
so, it could be claimed as duplicate.因此,它可以声称是重复的。
Anyway, following Kurt Peek's answer, you could do:无论如何,按照 Kurt Peek 的回答,你可以这样做:
import geopy.distance
def get_distnace(coords_1, coords_2):
return geopy.distance.vincenty(coords_1, coords_2).km
to get a function that returns the distance in 'km'.获得一个以“km”为单位返回距离的函数。 Then, given a couple of lists like yours, in the form
然后,给出几个像你这样的列表,形式为
list1 = [[stat_name_1, lat_1, lon_1], [stat_name_1, lat_1, lon_1], ... ]
list2 = [[...], ... ]
you could probably do:你可能会这样做:
min_stat = get_distance(list1[0][1:], list2[0][1:])
for stat_1 in list1:
coord_1 = stat_1[1:]
for stat_2 in list2:
coord_2 = stat_2[1:]
min_stat = min(min_stat, get_distance(coord_1, coord_2)
Take it as a draft, an idea, to be debugged and tested before any application.把它当作一个草稿、一个想法,在任何应用程序之前进行调试和测试。
geopy is your friend. geopy 是你的朋友。 It has functions that calculate the distance between coordinate pairs.
它具有计算坐标对之间距离的函数。 Here's one approach:
这是一种方法:
from geopy import distance
s = {
977: (23.141747, 53.796469),
946: (23.398398, 55.422916),
# etc etc
}
d = {
962: (23.657571, 53.703683),
745: (23.671971, 52.955976),
743: (23.766849, 53.770344),
# etc etc
}
for (ss, a) in s.items():
best = None
dist = None
for (dd, b) in d.items():
km = distance.distance(a, b).km
if dist is None or km < dist:
best = dd
dist = km
print(f'{ss} is nearest {best}: {dist} km')
If I run it with your example data I get:如果我使用您的示例数据运行它,我会得到:
977 is nearest 962: 57.909274476145846 km
946 is nearest 760: 66.3613771288571 km
742 is nearest 962: 4.857141728990575 km
980 is nearest 760: 38.94400553289674 km
660 is nearest 743: 65.56437761273963 km
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.