Find the nearest point in distance for all the points in the dataset - Python

前端 未结 3 1718
栀梦
栀梦 2020-12-15 13:04

I have a dataset as follows,

Id     Latitude      longitude
1      25.42         55.47
2      25.39         55.47
3      24.48         54.38
4      24.51             


        
3条回答
  •  自闭症患者
    2020-12-15 13:30

    You can do this very efficiently by calling a library that implements smart algorithms for this, one example would be sklearn which has a NearestNeighbors method that does exactly this.

    Example of the code modified to do this:

    from sklearn.neighbors import NearestNeighbors
    import numpy as np
    
    def distance(p1, p2):
        """
        Calculate the great circle distance between two points
        on the earth (specified in decimal degrees)
        """
        lon1, lat1 = p1
        lon2, lat2 = p2
        # convert decimal degrees to radians
        lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
        c = 2 * np.arcsin(np.sqrt(a))
        km = 6367 * c
        return km
    
    points = [[25.42, 55.47],
              [25.39, 55.47],
              [24.48, 54.38],
              [24.51, 54.54]]
    
    nbrs = NearestNeighbors(n_neighbors=2, metric=distance).fit(points)
    
    distances, indices = nbrs.kneighbors(points)
    
    result = distances[:, 1]
    

    which gives

    >>> result
    array([  1.889697  ,   1.889697  ,  17.88530556,  17.88530556])
    

提交回复
热议问题