In this video lesson we will develop a python program to calculate the distance and heading between any two GPs positions on earth. The equations we will be coding up are below:
The code which we developed is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import math earthRadius = 6371000 def calculateDistance(lat1,lon1,lat2,lon2): lat1=lat1*2*math.pi/360 lon1=lon1*2*math.pi/360 lat2=lat2*2*math.pi/360 lon2=lon2*2*math.pi/360 theta = 2*math.asin(math.sqrt( math.sin((lat2-lat1)/2)**2 + math.cos(lat1)*math.cos(lat2)*math.sin((lon2-lon1)/2)**2 )) distance = earthRadius * theta return distance def calculateHeading(lat1,lon1,lat2,lon2): lat1=lat1*2*math.pi/360 lon1=lon1*2*math.pi/360 lat2=lat2*2*math.pi/360 lon2=lon2*2*math.pi/360 deltaLon=lon2-lon1 xC=math.sin(deltaLon)*math.cos(lat2) yC=math.cos(lat1)*math.sin(lat2)-math.sin(lat1)*math.cos(lat2)*math.cos(deltaLon) beta = math.atan2(xC,yC) betaDeg = beta*360/2/math.pi return betaDeg print("Enter Coordinates in Degrees") lat1= float(input("lat1? ")) lon1= float(input("lon1? ")) lat2= float(input("lat2? ")) lon2= float(input("lon2? ")) distance = calculateDistance(lat1,lon1,lat2,lon2) heading = calculateHeading(lat1,lon1,lat2,lon2) print("REPORT: ") print("Distance = ",distance) print("Heading = ",heading) |