Skip to content

Instantly share code, notes, and snippets.

@stoermerjp
Created April 14, 2012 14:49
Show Gist options
  • Save stoermerjp/2384911 to your computer and use it in GitHub Desktop.
Save stoermerjp/2384911 to your computer and use it in GitHub Desktop.
DroneMapper.com: Python Get Bearing From 2 Lat/Lon Points
#
# Python code found on web to convert LAT1 LON1 / LAT2 LON2 to a bearing/course.
#
from math import *
import sys
lat1 = float(sys.argv[1])
lat2 = float(sys.argv[2])
lon1 = float(sys.argv[3])
lon2 = float(sys.argv[4])
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
Base = 6371 * c
def calcBearing(lat1, lon1, lat2, lon2):
dLon = lon2 - lon1
y = sin(dLon) * cos(lat2)
x = cos(lat1) * sin(lat2) \
- sin(lat1) * cos(lat2) * cos(dLon)
return atan2(y, x)
Bearing = calcBearing(lat1, lon1, lat2, lon2)
Bearing = degrees(Bearing)
Bearing = (Bearing + 360) % 360
print Bearing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment