Python 计算空间中最近的点,蟒蛇3
我有两个不同坐标集的数据集。我试图找出距离最短的点的指数。为此,我尝试编写了两个函数:Python 计算空间中最近的点,蟒蛇3,python,python-3.x,numpy,Python,Python 3.x,Numpy,我有两个不同坐标集的数据集。我试图找出距离最短的点的指数。为此,我尝试编写了两个函数: def distance(a, b): """ Calculates the distance between two GPS points (decimal) @param a: 2-tuple of point A @param b: 2-tuple of point B @return: distance in m """ from
def distance(a, b):
"""
Calculates the distance between two GPS points (decimal)
@param a: 2-tuple of point A
@param b: 2-tuple of point B
@return: distance in m
"""
from numpy import sin, cos, sqrt, arctan2, radians, add, subtract, multiply
r = 6371000 # average earth radius in m
dLat = radians(subtract(a[0],b[0]))
dLon = radians(subtract(a[1],b[1]))
x1 = add(sin(dLat/2) ** 2, cos(radians(a[0])))
x2 = multiply(cos(radians(b[0])),sin(dLon/2))
x = multiply(x1,x2) **2
y = 2 * arctan2(sqrt(x), sqrt(1-x))
d = r * y
return d
及
请你能帮我使这些更有效,并帮助识别任何错误。
我目前使用的两个数据集是2874 x 1和4346 x 1。
以下是一些示例数据:
dataset 1:
longitude, latitude
-29.12777024, 31.97120842
-29.12931210, 31.97415242
-29.1305217, 31.97645816
-29.13207462, 31.97936922
-29.13390954, 31.98294568
-29.1358673, 31.98642314
-29.1379327, 31.99051119
-29.1402072, 31.99354618
-29.14214358, 31.99680022
-29.14373422, 31.9999497
-29.1456006, 32.00320652
-29.1477001, 32.00602422
-29.14998566, 32.0084005
-29.1536702, 32.01045613
Dataset 2:
Lon_1, Lat_1
25.0, -34.0
25.1, -34.0
25.2, -34.0
25.3, -34.0
25.4, -34.0
25.5, -34.0
25.6, -34.0
25.7, -34.0
25.8, -34.0
25.9, -34.0
26.0, -34.0
26.1, -34.0
非常感谢无需
ravel
您的lon
和lat
阵列:广播和通用功能在这里拯救您的生命:
dist = distance(ilon, ilat, lon, lat)
生成一个二维数组,其中包含(ilon,ilat)和(lon,lat)中所有项之间的距离。然后
获取距离矩阵中的最小距离的索引,最后
minlon, minlat = lon.ravel()[index], lat.ravel()[index]
提供最终答案我会仔细考虑这个问题,但乍一看,我建议将导入语句从函数定义中移出,并将它们放在文件的顶部。也可以这样导入:
将numpy导入为np
,然后将函数引用为np.sin()
。这被认为是良好的实践。谢谢@scirocorics。这很有帮助。
index = np.argmin(dist)
minlon, minlat = lon.ravel()[index], lat.ravel()[index]