I am working on the calculation of the distance between two global positions based on their coordinates. When using only two locations, I get results:
def global_distance(location1, location2):
lat1, lon1 = location1
lat2, lon2 = location2
radius = 6371 # radius of the Earth
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
lat1 = 55.32; lat2 = 54.276; long1 = -118.8634; long2 = -117.276
print( global_distance((lat1, long1), (lat2, long2)))
What if I want to calculate the distance between several locations? Assuming I have a CSV file containing three locations:
Location Lat1 Long1
A 55.322 -117.17
B 57.316 -117.456
C 54.275 -116.567
How can I iterate over these two columns and produce distances between (A,B), (A,C) and (B,C) ?