-
-
Save Dhulkarnayn/7bce9042ef08f0bbecede64e5231415a to your computer and use it in GitHub Desktop.
Simple python program which returns the distance and bearing between two geographic location
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
# 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[0], ' m') | |
print ('Bearing angle between home and destination is ', dist_brng[1], 'degree') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment