Skip to content

Instantly share code, notes, and snippets.

@geografa
Created November 15, 2011 07:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geografa/1366401 to your computer and use it in GitHub Desktop.
Save geografa/1366401 to your computer and use it in GitHub Desktop.
Haversine formula example in Python for calculating bearing
#!/usr/bin/env python
# Haversine formula example in Python for Calculating Bearing
# Inspired from http://www.movable-type.co.uk/scripts/latlong.html
import math
def bearing(origin, destination):
lat1, lon1 = origin
lat2, lon2 = destination
rlat1 = math.radians(lat1)
rlat2 = math.radians(lat2)
rlon1 = math.radians(lon1)
rlon2 = math.radians(lon2)
dlon = math.radians(lon2-lon1)
b = math.atan2(math.sin(dlon)*math.cos(rlat2),math.cos(rlat1)*math.sin(rlat2)-math.sin(rlat1)*math.cos(rlat2)*math.cos(dlon)) # bearing calc
bd = math.degrees(b)
br,bn = divmod(bd+360,360) # the bearing remainder and final bearing
return bn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment