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

标签 python scipy geolocation geospatial latitude-longitude

我有一个经纬度坐标数组,我正在尝试使用 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/

相关文章:

javascript - django ajax表单提交for循环中的多个值

python - 以 CSR/COO 格式为存储在 Pandas DataFrame 中的分类数据的巨大特征向量创建稀疏矩阵

javascript - AngularJS |根据地理位置(纬度、经度)对 JSON 进行排序

python - 将多个字典作为参数传递给函数

python - Plotly 似乎丢失了热图的标签

python /docker : FileNotFoundError: [Errno 2] No such file or directory:

python - 在计算最小二乘法时,为什么要添加 1 的向量?

python - 在傅立叶域中用内核卷积图像

android - 地理位置跟踪和 Google Cloud Messaging 协同工作

javascript - 无法在 React 中返回带有地理位置坐标的 JSX