As always, Wikipedia has a good article on the subject: Great-circle distance
From there you can see how to convert lat/long to radians:
... and how to calculate the great-circle distance between two points on a sphere:
def coordinate_to_radians(deg, min = 0.0, sec = 0.0):
'''Convert a geographical coordinate into radians.'''
if deg < 0.0:
sign = -1.0
deg = -deg
sign = 1.0
return (sign * (deg + (min + sec / 60.0) / 60.0)) * math.pi / 180.0
Then it's simply multiplication by Earths radius in whatever unit you wish:
def angular_distance(standpoint_lat, standpoint_long, forepoint_lat, forepoint_long):
'''Calculate the circular angular distance of two points on a sphere.'''
phi_s = standpoint_lat
phi_f = forepoint_lat
lambda_diff = standpoint_long - forepoint_long
cos = math.cos
sin = math.sin
num = (cos(phi_f) * sin(lambda_diff)) ** 2.0 + (cos(phi_s) * sin(phi_f) - sin(phi_s) * cos(phi_f) * cos(lambda_diff)) ** 2.0
denom = sin(phi_s) * sin(phi_f) + cos(phi_s) * cos(phi_f) * cos(lambda_diff)
return math.atan2(math.sqrt(num), denom)
You may use 0 for min or sec, if you have the coordinates in decimal format. Here's an example using the example values from the Wikipedia article:
def distance_between_coordinates_km(lat1, long1, lat2, long2):
'''Calculate distance between two points on Earth in kilometers. lat and long are three-tuples (deg, min, sec).'''
rad_lat1 = coordinate_to_radians(lat1, lat1, lat1)
rad_long1 = coordinate_to_radians(long1, long1, long1)
rad_lat2 = coordinate_to_radians(lat2, lat2, lat2)
rad_long2 = coordinate_to_radians(long2, long2, long2)
return angular_distance(rad_lat1, rad_long1, rad_lat2, rad_long2) * 6371.01 # km
- Nashville International Airport (BNA) in Nashville, TN, USA: N 36°7.2', W 86°40.2'
- Los Angeles International Airport (LAX) in Los Angeles, CA, USA: N 33°56.4', W 118°24.0'
>>> distance_between_coordinates_km((36, 7.2, 0), (-86, 40.2, 0), (33, 56.4, 0), (-118, 24.0, 0))