Created
November 15, 2011 07:38
-
-
Save geografa/1366401 to your computer and use it in GitHub Desktop.
Haversine formula example in Python for calculating bearing
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
#!/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