gpt4 book ai didi

python - KDTree 正在返回半径以外的点

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:41 33 4
gpt4 key购买 nike

我有一个经纬度坐标数组,我正在尝试使用 KDTree 和 scipy 的 query_ball_point 返回指定纬度和经度 1 英里半径范围内的所有数据点。

问题是 query_ball_point 返回指定 1 英里半径之外的点。这是我的代码:

import pandas as pd
import scipy as sp
import geocoder
import pysal as psl


search_list = df['coordinates'].tolist()
tree = psl.cg.KDTree(search_list, distance_metric='Arc', radius=psl.cg.RADIUS_EARTH_MILES)
latlong = (39.698840000000004, -104.975916)
index = tree.query_ball_point(latlong,r=1)

结果是一个坐标数组,如下所示:

+---------------------------------------+
| coordinates |
+---------------------------------------+
| (39.676973877551, -104.966231826172) |
| (39.6777407534644, -104.988982458831) |
| ... |
+---------------------------------------+

当我尝试使用 haversine 公式来验证这些结果时,我看到第一个坐标是 1.6 英里的距离

from haversine import haversine
haversine((39.676973877551, -104.966231826172),
(39.698840000000004, -104.975916),miles=True)

1.5961362762187963

最佳答案

Pysal 不使用 haversine 函数来计算 query_ball_point 方法的距离。它使用不同的 pysal.cg.sphere.arcdist 函数。

import pysal
from pysal.cg.kdtree import KDTree

locations = [(40.702566, -73.816859),
(40.70546, -73.810708),
(40.709179, -73.820574),
(40.700486, -73.807969),
(40.694624, -73.820593),
(40.695132, -73.820841),
(40.694095, -73.821334),
(40.694165, -73.822368),
(40.695077, -73.822817),
(40.6747769261, -73.8092618174)]
tree = KDTree(locations, distance_metric='Arc', radius=pysal.cg.RADIUS_EARTH_MILES)
current_point = (40.709523, -73.802472)
# get all points within X miles of 'current_point'
indices = tree.query_ball_point(current_point, 1)
for i in indices:
print(locations[i])

1英里内有3个点

(40.70546, -73.810708)
(40.700486, -73.807969)
(40.6747769261, -73.8092618174)

根据 haversine 公式,并非所有这些点都在 1 英里以内:

from haversine import haversine
for i in indices:
print(haversine(current_points, locations[i], miles = True))

0.5146716729994124
0.6875825817591269
2.4269297885659022

但是根据 pysal 的 arcdist 公式使用 3958.756 英里的半径,它们在 1 英里以内:

from pysal.cg.sphere import arcdist
for i in indices:
print(arcdist(current_points, locations[i], 3958.756))

0.5744128196875283
0.4178272122350164
0.8175408580090955

关于python - KDTree 正在返回半径以外的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49266244/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com