Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
crystal-clear solution in Clear category for Earth Distances by David_Jones
from math import acos, cos, radians, sin
R = 6371
def to_radians(coord):
coord = ''.join(ch if ch.isalnum() else ' ' for ch in coord)
degrees, minutes, seconds, dir = coord.split()
degrees = int(degrees) + int(minutes)/60 + int(seconds)/3600
return radians(degrees if dir in 'SW' else -degrees)
def distance(first, second):
lat_1, long_1 = first.replace(',', ' ').split()
lat_2, long_2 = second.replace(',', ' ').split()
a1, b1 = to_radians(lat_1), to_radians(long_1)
a2, b2 = to_radians(lat_2), to_radians(long_2)
return R * acos(sin(a1)*sin(a2) + cos(a1)*cos(a2)*cos(b1-b2))
May 21, 2019