Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Earth Distances by tokiojapan55
R = 6371
import math
import re
def distance(first, second):
def to_rad(expr):
token = re.findall(r'[0-9]+|[NEWS]', expr)
deg = int(token[0]) + int(token[1])/60 + int(token[2])/3600
return math.radians(deg if token[3] not in 'WS' else -deg)
f = re.split('[\s,]+', first)
s = re.split('[\s,]+', second)
lat1,lon1 = to_rad(f[0]),to_rad(f[1])
lat2,lon2 = to_rad(s[0]),to_rad(s[1])
lat_ave = (lat1 - lat2) / 2
lon_ave = (lon1 - lon2) / 2
return R*2 * math.asin((math.sin(lat_ave)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(lon_ave)**2)**0.5)
June 25, 2020
Comments: