- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这篇文章基于 this one .
我得到了一个 Pandas 数据框,其中包含以地理坐标(大地坐标)作为经度和纬度的城市。
import pandas as pd
df = pd.DataFrame([{'city':"Berlin", 'lat':52.5243700, 'lng':13.4105300},
{'city':"Potsdam", 'lat':52.3988600, 'lng':13.0656600},
{'city':"Hamburg", 'lat':53.5753200, 'lng':10.0153400}]);
对于每个城市,我试图找到另外两个最近的城市。因此我尝试了 scipy.spatial.KDTree。为此,我必须将大地坐标转换为 3D catesian 坐标(ECEF = earth-centered, earth-fixed):
from math import *
def to_Cartesian(lat, lng):
R = 6367 # radius of the Earth in kilometers
x = R * cos(lat) * cos(lng)
y = R * cos(lat) * sin(lng)
z = R * sin(lat)
return x, y, z
df['x'], df['y'], df['z'] = zip(*map(to_Cartesian, df['lat'], df['lng']))
df
给我这个:
有了这个我可以创建 KDTree:
coordinates = list(zip(df['x'], df['y'], df['z']))
from scipy import spatial
tree = spatial.KDTree(coordinates)
tree.data
现在我正在柏林进行测试,
tree.query(coordinates[0], 2)
这正确地给出了柏林(本身)和波茨坦作为我列表中距离柏林最近的两个城市。
问题:但我想知道如何处理该查询的距离?它说 1501 - 但我如何将其转换为米或公里?柏林和波茨坦之间的实际距离是 27 公里,而不是 1501 公里。
备注:我知道我可以获得两个城市的经度/纬度并计算半正弦距离。但是使用 KDTree 的输出会很酷。
(array([ 0. , 1501.59637685]), array([0, 1]))
感谢任何帮助。
最佳答案
KDTree 正在计算两点(城市)之间的欧氏距离。两座城市和地心形成一个isosceles triangle .
德语维基百科条目包含对 geometric properties 的一个很好的概述。英文条目缺少。您可以使用它来计算距离。
import numpy as np
def deg2rad(degree):
rad = degree * 2*np.pi / 360
return(rad)
def distToKM(x):
R = 6367 # earth radius
gamma = 2*np.arcsin(deg2rad(x/(2*R))) # compute the angle of the isosceles triangle
dist = 2*R*sin(gamma/2) # compute the side of the triangle
return(dist)
distToKM(1501.59637685)
# 26.207800812050056
在关于获得相反的评论之后我重新阅读了这个问题并意识到虽然看起来可以使用上面提出的功能,但真正的问题在于其他地方。
to_Cartesian
函数中的
cos
和 sin
期望输入为 弧度
( documentation )而您将角度交给他们。您可以使用上面定义的函数 deg2rad
将纬度和经度转换为弧度。这应该会直接为您提供与 KDTree 的距离(以公里为单位)。
关于python - Scipy:如何将 KD-Tree 距离从查询转换为千米(Python/Pandas),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43020919/
假设您有一个对象列表,每个对象都需要计算距离它最近的对象以进行拍摄。这个对象有一个 x 和一个 y 值。物体也在移动。 kd树还有用吗?我不确定,因为如果物体在移动,你必须继续创建这个 kd 树。 我
我正在查看 KD 树的维基百科页面。例如,我在 python 中实现了用于构建列出的 kd 树的算法。 但是,使用 KD 树进行 KNN 搜索的算法会切换语言并且并不完全清楚。英文解释开始有意义,但它
Kd 算法首先通过划分基元数组(三角形、球体等)来创建根 BSP 节点,以便创建两个新数组(左基元和右基元),用于创建其两个基元子树。 左基元和右基元是通过将给定基元数组划分为两个数组来计算的。通过取
我(尝试)在Processing/Java中实现KD树,并遵循我在数十篇文章和维基百科文章中看到的逻辑,但我一定做错了什么,因为输出如下所示: 而不是这个: 显然有些东西不对劲,因为节点是重复的,而且
我有以下路径数据: id1 p1 p2 0 1 7.935 5.103 1 1 7.93
我正在阅读 C 中的 kd 树实现。这是其中的一些部分。问题出在 findmedian 函数中。我不明白 的实现 *md = start +(end-start)/2; ...... -> 其他不相关
给定一组数据点,kdtree是在它们之上创建的,但是这个 kdtree 是唯一的吗? 最佳答案 这似乎取决于您构建树的方式。维基百科文章提到中点的选择如何影响生成的树是否平衡。如果选择不同的点,那么树
警告:相当长的问题,也许太长了。如果是这样,我深表歉意。 我正在开发一个涉及 kd 树的最近邻搜索的程序(在本例中,它是一棵具有 3961 个独立点的 11 维树)。我们才刚刚了解它们,虽然我很好地掌
我目前正在尝试构建二维(纬度和经度)的 KD 树以查询最近的坐标。我将坐标(ID、纬度、经度)推送到一个 vector 中,并将该 vector 传递到我的 KD-Tree 的构造函数中。 但是当我在
我正在尝试为我的 C++ (DirectX) 项目实现一个 kd-tree 以加速我的碰撞检测。我的实现是一个非常原始的递归函数。第 nth_element 似乎工作正常(如果我将其注释掉,则只有 1
我为两组点构建了 kd 树,以便找到两组点之间最接近的双色配对: kd 树存储为 python 字典,可以在下面的代码中找到,并传递给一个函数('closest'),该函数旨在同时递归地分析两棵树以找
我正在尝试基于递归构建一个二维树。我可以将算法总结如下: > ALGORITHM BuildKDTree(P,depth) > 1. if P contains only one po
我想从平衡的 KD 树中删除一个元素,并且树在不重建整棵树的情况下保持平衡。这有可能在不重建整棵树的情况下平衡树吗?如果是那么怎么办? 最佳答案 对于标准的 k-d 树,您可以删除项目但没有重新平衡,
所以我们有一个无限的 3d 世界,我们需要查询最近的点。然而我们的点有标识符并且一直在移动。支持数据点的 KDtree\Octree 之类的数据结构是什么。连续移动并且在搜索+更新方面不会比 3d 情
你好,有人用 C++ 迭代实现了 Kd-Tree 吗?我试过了,但是当节点数为奇数时它失败了。到目前为止,这是我的代码。我指的是 http://ldots.org/kdtree/#buildingAk
我想扩展一个 kd-tree (2D) 类,以便能够删除节点(点)。这种移除应该在不必重建树的大部分的情况下进行。这些slides中描述的算法,在幻灯片 13 上似乎是我所追求的。但是,我无法理解幻灯
普通的kd-tree是通过递归地将超平面分成两半来构建的。并且用一个查询点做范围搜索,它只会搜索一小串点(对数)而不是所有(线性)。 我想知道 kd 树可以用点积构建吗? 例如,b 是一个 3d 向量
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我刚刚完成了一个 kd-tree 的实现用于进行快速最近邻搜索。除了 Euclidean distance 之外,我有兴趣使用不同的距离指标。 .我对 kd-tree 的理解是,如果度量是非欧几里德的
我使用了 kd-tree algoritham 和 make tree。 但是我发现树不平衡所以我的问题是如果我们使用 kd-tree algoritham 那棵树总是平衡的如果不是那么我们怎样才能使
我是一名优秀的程序员,十分优秀!