Navigation Systems like Global Positioning System (GPS) plays a crucial role in Manned Aircraft (Commercial & Military) and Unmanned Aircraft Systems. In GPS, the representation of positional data is in Geographic Coordinates System (Latitudes and Longitudes).
Due to the spherical structure of the earth, navigation between two points is complicated and this complexity can be reduced by using the haversine formula.
Image Credits: People illustrations by Storyset
The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes.
Program: Distance and bearing between two geographic locations
Below mentioned code is a simple python program named
distance_bearing.py that returns the distance using haversine formula and the bearing angle between two geographic locations,
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 # Importing necessary Packages import math #Radius of earth in metres R = 6371e3 def distance_bearing(homeLatitude, homeLongitude, destinationLatitude, destinationLongitude): """ Simple function which returns the distance and bearing between two geographic location Inputs: 1. homeLatitude - Latitude of home location 2. homeLongitude - Longitude of home location 3. destinationLatitude - Latitude of Destination 4. destinationLongitude - Longitude of Destination Outputs: 1. [Distance, Bearing] - Distance (in metres) and Bearing angle (in degrees) between home and destination Source: https://github.com/TechnicalVillager/distance-bearing-calculation """ rlat1 = homeLatitude * (math.pi/180) rlat2 = destinationLatitude * (math.pi/180) rlon1 = homeLongitude * (math.pi/180) rlon2 = destinationLongitude * (math.pi/180) dlat = (destinationLatitude - homeLatitude) * (math.pi/180) dlon = (destinationLongitude - homeLongitude) * (math.pi/180) # Haversine formula to find distance a = (math.sin(dlat/2) * math.sin(dlat/2)) + (math.cos(rlat1) * math.cos(rlat2) * (math.sin(dlon/2) * math.sin(dlon/2))) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) # Distance in metres distance = R * c # Formula for bearing y = math.sin(rlon2 - rlon1) * math.cos(rlat2) x = math.cos(rlat1) * math.sin(rlat2) - math.sin(rlat1) * math.cos(rlat2) * math.cos(rlon2 - rlon1) # Bearing in radians bearing = math.atan2(y, x) bearingDegrees = bearing * (180/math.pi) out = [distance, bearingDegrees] return out def main(): # Initializing an Empty Array dist_brng =  # Getting inputs from the user print ("+++++++++++++ Please Enter the values in decimals +++++++++++++") print ("Enter the Latitude of Current location: ") lat1 = float(input()) print ("Enter the Longitude of Current location: ") lon1 = float(input()) print ("Enter the Latitude of Destination: ") lat2 = float(input()) print ("Enter the Longitude of Destination: ") lon2 = float(input()) # Caluculating the Distance and Bearing dist_brng = distance_bearing(lat1, lon1, lat2, lon2) # Displaying the Calculated Distance and Bearing print ('Distance between the home and destination is ', dist_brng, ' m') print ('Bearing angle between home and destination is ', dist_brng, 'degree') if __name__ == "__main__": main()
Execution & Output
The program needs to be executed as below,
1 $ python distance_bearing.py
The following inputs are needed to be entered during the execution of the program,
- Home Latitude
- Home Longitude
- Destination Latitude
- Destination Longitude
The output of the above executed code is,
Image Credits: Dhulkarnayn, Elucidate Drones
I’ve tried to explain the python program which calculates the distance and bearing between two geographic location with the acquired information from various sources. I’m expectantly waiting for your valuable feedback and suggestions regarding this topic.
At last, Sharing is Caring, feel free to share with your friends if you’ve liked this article. Thank you!