计算空间中的最近点python3

时间:2018-03-16 12:29:19

标签: 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 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

 def findNearest(ilon,ilat,lon,lat):
     """
     i,j = findNearest(ilon,ilat,lon,lat):
     find indices of closest point within lat and lon data
     Input:
     ilon, ilat (input longitude and latitude to match)
     lon, lat = longitude and latitude in which to look
     lon and lat must have same dimension
     return:
     i,j =  row and colum index of match in lon, lat
     lon = ravel(lon)
     """
     from numpy import where, ravel, shape, meshgrid, array

     d=[]
     if len(shape(lon))==1:
        lon,lat = meshgrid(lon,lat)
     a = ravel(lon)
     b = ravel(lat)
     for x, y in zip(a,b):
        d.append(distance([ilon,ilat],[x,y]))
     i, = where(d==min(array(d)))
     i=i[-1]
     j,i = where((a[i]==lon) & (b[i]==lat))
     return int(i),int(j)

请帮助我提高效率并帮助识别任何错误。 我目前使用的两个数据集是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

非常感谢

1 个答案:

答案 0 :(得分:1)

无需ravel您的lonlat数组:广播和通用功能可以挽救您的生命:

dist = distance(ilon, ilat, lon, lat)

生成一个2D数组,其中包含(ilon,ilat)与(lon,lat)中所有项之间的距离。然后

index = np.argmin(dist)

得到(raveled)距离矩阵中最小距离的索引,最后

minlon, minlat = lon.ravel()[index], lat.ravel()[index]

提供最终答案