Haversine Formula in Python (Bearing and Distance between two GPS points)

后端 未结 10 1771
死守一世寂寞
死守一世寂寞 2020-11-22 09:47

Problem

I would like to know how to get the distance and bearing between 2 GPS points. I have researched on the haversine formula. Someone told me

10条回答
  •  执笔经年
    2020-11-22 10:28

    Here's a numpy vectorized implementation of the Haversine Formula given by @Michael Dunn, gives a 10-50 times improvement over large vectors.

    from numpy import radians, cos, sin, arcsin, sqrt
    
    def haversine(lon1, lat1, lon2, lat2):
        """
        Calculate the great circle distance between two points 
        on the earth (specified in decimal degrees)
        """
    
        #Convert decimal degrees to Radians:
        lon1 = np.radians(lon1.values)
        lat1 = np.radians(lat1.values)
        lon2 = np.radians(lon2.values)
        lat2 = np.radians(lat2.values)
    
        #Implementing Haversine Formula: 
        dlon = np.subtract(lon2, lon1)
        dlat = np.subtract(lat2, lat1)
    
        a = np.add(np.power(np.sin(np.divide(dlat, 2)), 2),  
                              np.multiply(np.cos(lat1), 
                                          np.multiply(np.cos(lat2), 
                                                      np.power(np.sin(np.divide(dlon, 2)), 2))))
        c = np.multiply(2, np.arcsin(np.sqrt(a)))
        r = 6371
    
        return c*r
    

提交回复
热议问题