Calculate Bearing Between 2 GPS coordinates
Posted: Fri Jan 03, 2014 6:57 am
Hello all,
I'm trying to use the Haversine formula to calculate the bearing between two GPS coordinates. I've been following this site's directions: http://www.movable-type.co.uk/scripts/latlong.html. This is my code so far:
However, my result does not match the web site's:
Coordinate 1: (53.32055, -1.72972)
Coordinate 2: ( 53.31861, -1.69972)
My result: 86.21102 degrees
Correct Result: 96.04555 degrees
I'm using IDLE to run the program on a model B Revision 2 Raspberry Pi.
Thank you for any help!
I'm trying to use the Haversine formula to calculate the bearing between two GPS coordinates. I've been following this site's directions: http://www.movable-type.co.uk/scripts/latlong.html. This is my code so far:
Code: Select all
import math
#coordinates1
lat1 = 53.32055
lon1 = -1.72972
#coordinates2
lat2 = 53.31861
lon2 = -1.69972
dlon = lon2-lon1
#use haversine formula
deltaY = math.sin(dlon) * math.cos(lat2)
deltaX = math.cos(lat1)*math.sin(lat2)-math.sin(lat1)*math.cos(lat2)*math.cos(dlon)
#convert to degrees
bearing = (math.atan2(deltaY, deltaX))* (180/math.pi)
#normalize to compass headings
bearing = (bearing + 180) % 360
print 'bearing: ', bearing
print 'deltaY: ', deltaY
print 'deltaX: ', deltaX
Coordinate 1: (53.32055, -1.72972)
Coordinate 2: ( 53.31861, -1.69972)
My result: 86.21102 degrees
Correct Result: 96.04555 degrees
I'm using IDLE to run the program on a model B Revision 2 Raspberry Pi.
Thank you for any help!