Last active
April 5, 2018 10:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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