に距離を探す:私は(実際の座標のサンプル)を以下のように緯度と経度の組とのデータフレームを持って最寄りのGPS座標(最近傍探索)
id latlon
67 79 (39.1791764701497, -96.5772313693982)
68 17 (39.1765194942359, -96.5677757455844)
69 76 (39.1751440428827, -96.5772939901891)
70 58 (39.175359525189, -96.5691986655256)
71 50 (39.1770962912298, -96.5668107589661)
私はid
との距離を見つけたいです私はPEにしたい座標の数が多い(45K +)を持っている
id latlon nearest_id nearest_dist
67 79 (39.1791764701497, -96.5772313693982) 17 37
68 17 (39.1765194942359, -96.5677757455844) 58 150
69 76 (39.1751440428827, -96.5772939901891) 50 900
70 58 (39.175359525189, -96.5691986655256) 17 12
71 50 (39.1770962912298, -96.5668107589661) 79 4
:同じデータフレーム(説明のために、私はちょうどnearest_id
で下記の番号を構成するとnearest_dist
列てる)で最寄りlatlon
この操作を実行します。ここで
はgeopy.distances
からgreat_circle
を使用して、以下の私の未遂ソリューションです:
def great_circle_dist(latlon1, latlon2):
"""Uses geopy to calculate distance between coordinates"""
return great_circle(latlon1, latlon2).meters
def find_nearest(x):
"""Finds nearest neighbor """
df['distances'] = df.latlon.apply(great_circle_dist, args=(x,))
df_sort = df.sort_values(by='distances')
return (df_sort.values[1][0], df_sort.values[1][2])
df['nearest'] = df['latlon'].apply(find_nearest)
df['nearest_id'] = df.nearest.apply(lambda x: x[0])
df['nearest_dist'] = df.nearest.apply(lambda x: x[1])
del df['nearest']
del df['distances']
効率的にこの計算をするために何ができますか?
ここで 'cKDTree'を使うと入力がデカルト座標であると仮定しますか? –
@JosephDasenbrockはい。 'pyproj'を使用してlon/latからUTM(または測定に適した他の投影)に座標を投影するか、scipy.spatial.distanceを使用してカスタム距離距離として大円またはそれ以上のhaversine式を使用する<あなたのアルゴリズムの選択>。 2番目のアプローチは[同じ質問](https://stackoverflow.com/a/45807448/6517541)の別の解決策で説明されています –
'cKDTree'は100%正確であるか、完全精度よりもスピードを優先させる検索アルゴリズムですか? –