How to find the distance between 2 points in 2 different dataframes in pandas?

后端 未结 1 903
你的背包
你的背包 2021-01-24 20:06

I\'ve got two dataframes, each with a set of coordinates. Dataframe 1 is a list of biomass sites, with coordinates in columns \'lat\' and \'lng\'. Dataframe 2

相关标签:
1条回答
  • 2021-01-24 20:55

    To calculate distance between two global coordinates you should use the Haversine Formula, based on this page I have implemented the following method:

    import math
    def distanceBetweenCm(lat1, lon1, lat2, lon2):
        dLat = math.radians(lat2-lat1)
        dLon = math.radians(lon2-lon1)
    
        lat1 = math.radians(lat1)
        lat2 = math.radians(lat2)
    
        a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2)
        c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
        return c * 6371 * 100000 #multiply by 100k to get distance in cm
    

    You can also modify it to return different units, by multiplying by different powers of 10. In the example a multiplication by 100k results in units in centimeters. Without multiplying the method returns distance in km. From there you could perform more unit conversions if necessary .

    Edit: As suggested in the comments, one possible optimization for this would be using power operators instead of regular multiplication, like this:

    a = math.sin(dLat/2)**2 + math.sin(dLon/2)**2 * math.cos(lat1) * math.cos(lat2)
    

    Take a look at this question to read more about different speed complexities of calculating powers in python.

    0 讨论(0)
提交回复
热议问题