-
-
Save rochacbruno/2883505 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
# Haversine formula example in Python | |
# Author: Wayne Dyck | |
import math | |
def distance(origin, destination): | |
lat1, lon1 = origin | |
lat2, lon2 = destination | |
radius = 6371 # km | |
dlat = math.radians(lat2-lat1) | |
dlon = math.radians(lon2-lon1) | |
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \ | |
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2) | |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) | |
d = radius * c | |
return d |
Hello, thank you very much for this masterpiece. But my concern is how to do so when you have an excel file, I have bunch of cities and finding the distance from those cities to one reference point (which is also a city). Thanks
Hello @Zagroz, did you able to find the distances from an excel file using this code? I am also stuck in similar kind of situation. Thank you!
Hello, thank you very much for this masterpiece. But my concern is how to do so when you have an excel file, I have a bunch of cities and finding the distance from those cities to one reference point (which is also a city). Thanks
Hello @Zagroz, did you able to find the distances from an excel file using this code? I am also stuck in a similar kind of situation. Thank you!
Hello, I have implemented something similar to what you are trying to do. You can visit my repo which involves what you desire. The link is here:
Please inform if this solves your problem.
All the Best!!
Thanks for the eloquent and easily understandable code!
Thanks for this, can anyone explain what are 'a' and 'c' assignments , can we have better names for these variables ?
#I am adding the code to calculate the distance between two coordinates, you can call this function inside a for loop to get the distance between source and destination as soon as your bot makes a displacement.
#CALLING THE FUNCTION
Hope this helps!!