输入:
point = (lat, long)
places = [(lat1, long1), (lat2, long2), ..., (latN, longN)]
count = L
输出:
neighbors
= point
附近的places
子集。 (len(neighbors)=L
)
问题: 我能否使用kd树快速查找具有纬度和经度的点的最近邻居? (例如,scipy中的实现)
需要将点的地理坐标(纬度和经度)转换为坐标x, y吗?
这是解决此问题的最佳方法吗?
输入:
point = (lat, long)
places = [(lat1, long1), (lat2, long2), ..., (latN, longN)]
count = L
输出:
neighbors
= point
附近的places
子集。 (len(neighbors)=L
)
问题: 我能否使用kd树快速查找具有纬度和经度的点的最近邻居? (例如,scipy中的实现)
需要将点的地理坐标(纬度和经度)转换为坐标x, y吗?
这是解决此问题的最佳方法吗?
我真的不知道使用kd树是否能正确工作,但我的直觉告诉我它可能不准确。
我认为你需要使用类似于大圆距离的东西来获得准确的距离。
from math import radians, cos, sin, asin, sqrt, degrees, atan2
def validate_point(p):
lat, lon = p
assert -90 <= lat <= 90, "bad latitude"
assert -180 <= lon <= 180, "bad longitude"
# original formula from http://www.movable-type.co.uk/scripts/latlong.html
def distance_haversine(p1, p2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
Haversine
formula:
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
_ ____
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);
note that angles need to be in radians to pass to trig functions!
"""
lat1, lon1 = p1
lat2, lon2 = p2
for p in [p1, p2]:
validate_point(p)
R = 6371 # km - earths's radius
# convert decimal degrees to radians
lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a)) # 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c
return d
validate_point
函数?我猜它会检查纬度和经度是否介于-90和90之间? - fmarmdistance_haversine()
计算了两个经纬度坐标之间的距离(单位为公里),但它并没有回答如何使用这个度量找到最近的邻居的问题。 - lumbric