Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
meh solution in Clear category for Earth Distances by Leonix
import re
from math import pi, cos, sin, radians, acos
R = 6371
def to_radians(pos_str):
degs, mins, secs = map(int, re.search('(\d+)\D(\d+)\D(\d+)\D\w', pos_str).groups())
rads = radians(degs + mins / 60.0 + secs / 3600.0)
if 'W' in pos_str or 'S' in pos_str:
return -rads
return rads
def split_parts(pos_str):
return pos_str.replace(',', ' ').split()
def distance(first, second):
first_lat, first_long, second_lat, second_long = (to_radians(z) for z in split_parts(first) + split_parts(second))
direct_distance = sin(first_lat) * sin(second_lat) + cos(first_lat) * cos(second_lat) * cos(first_long - second_long)
return round(R * (acos(direct_distance) or pi), 1)
July 24, 2019