Skip to content

Instantly share code, notes, and snippets.

@jerinisready
Last active April 5, 2018 10:38
Show Gist options
  • Save jerinisready/65d7269b36131ba463ce21778fe8f1c9 to your computer and use it in GitHub Desktop.
Save jerinisready/65d7269b36131ba463ce21778fe8f1c9 to your computer and use it in GitHub Desktop.
Python Function to describe the distance between two points given by its latitude and longitude
from math import sin, cos, radians, acos
def calculate_distance(lat_a, long_a, lat_b, long_b, in_miles=True):
"""all angles in degrees, default result in miles"""
lat_a = radians(lat_a)
lat_b = radians(lat_b)
delta_long = radians(long_a - long_b)
cos_x = (
sin(lat_a) * sin(lat_b) +
cos(lat_a) * cos(lat_b) * cos(delta_long)
)
EARTH_RADIUS_IN_MILES = 3958.761
distance = acos(cos_x) * EARTH_RADIUS_IN_MILES
return distance if in_miles else distance/0.62137
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment