如果您需要高精度和健壮的测地线测量,请使用GeographicLib,它是用几种编程语言(包括C ++,Java,MATLAB,Python等)原生编写的。
有关文学参考,请参见CFF Karney(2013)“大地测量学算法”。请注意,这些算法比Vincenty的算法更健壮和准确,例如在对映体附近。
要计算两点之间的距离(以米为单位),请s12
从反大地测量解中获取距离属性。例如,使用适用于Python 的geolib库软件包
from geographiclib.geodesic import Geodesic
g = Geodesic.WGS84.Inverse(-41.32, 174.81, 40.96, -5.50)
print(g) # shows:
{'a12': 179.6197069334283,
'azi1': 161.06766998615873,
'azi2': 18.825195123248484,
'lat1': -41.32,
'lat2': 40.96,
'lon1': 174.81,
'lon2': -5.5,
's12': 19959679.26735382}
或设置便捷功能,该功能也可以将米转换为公里:
dist_km = lambda a, b: Geodesic.WGS84.Inverse(a[0], a[1], b[0], b[1])['s12'] / 1000.0
a = (-41.32, 174.81)
b = (40.96, -5.50)
print(dist_km(a, b)) # 19959.6792674 km
现在要查找列表A
和之间的最接近点B
,每个点都有100个点:
from random import uniform
from itertools import product
A = [(uniform(-90, 90), uniform(-180, 180)) for x in range(100)]
B = [(uniform(-90, 90), uniform(-180, 180)) for x in range(100)]
a_min = b_min = min_dist = None
for a, b in product(A, B):
d = dist_km(a, b)
if min_dist is None or d < min_dist:
min_dist = d
a_min = a
b_min = b
print('%.3f km between %s and %s' % (min_dist, a_min, b_min))
(84.57916462672875,158.67545706102192)和(84.70326937581333,156.9784597422855)之间22.481公里