Header Ads

Header ADS

Distance, Initial & Final Bearing

 From the beginning of my cadet life I was trying to write some code which can solve my problem. But in Academy it was not possible. I am at home now so here is the code....


you can also visit professional website here


The code:

import math
# from geopy.distance import geodesic

# def bearing(point1, point2):
#     #this function is not in use i will use it letter on
#     # point1 and point2 are tuples of (latitude, longitude)
#     # Calculate distance and bearing
#     distance = geodesic(point1, point2)
#     initial_bearing = distance.initial_bearing
#     final_bearing = distance.final_bearing
#     return distance.m, initial_bearing, final_bearing


def decimal_to_dms(decimal):
    #convert decimal to degree-minute-second
    degrees = int(decimal)
    minutes = int((decimal - degrees) * 60)
    seconds = round((((decimal - degrees) * 60) - minutes) * 60, 2)
    return (degrees, minutes, seconds)


def convert_to_decimal(degrees, minutes, seconds, direction):
    # convert degree-minute-second to decimal degree
    decimal = degrees + minutes/60 + seconds/3600
    if direction.upper() == 'S' or direction.upper() == 'W':
        decimal = -decimal
    return decimal

def distance(lat1, lon1, lat2, lon2):
    # Convert latitude and longitude to radians
    lat1 = math.radians(lat1)
    lon1 = math.radians(lon1)
    lat2 = math.radians(lat2)
    lon2 = math.radians(lon2)

    # Earth's radius in nautical miles
    earth_radius = 3440.064794816

    # Compute distance
    d = math.acos(math.sin(lat1)*math.sin(lat2) + math.cos(lat1)*math.cos(lat2)*math.cos(lon2-lon1)) * earth_radius
   
    return d

def bearing(lat1, lon1, lat2, lon2):
    # Convert latitude and longitude to radians
    lat1 = math.radians(lat1)
    lon1 = math.radians(lon1)
    lat2 = math.radians(lat2)
    lon2 = math.radians(lon2)
   
    y = math.sin(lon2-lon1) * math.cos(lat2)
    x = math.cos(lat1)*math.sin(lat2) - math.sin(lat1)*math.cos(lat2)*math.cos(lon2-lon1)
    initial_bearing = math.degrees(math.atan2(y, x))
    final_bearing = (initial_bearing + 180) % 360 if initial_bearing < 180 else (initial_bearing - 180) % 360
    return initial_bearing, final_bearing


# Input coordinates
print("Enter the latitude and longitude of first location:")
lat1_d = int(input("Latitude (Degrees): "))
lat1_m = int(input("Latitude (Minutes): "))
lat1_s = int(input("Latitude (Seconds): "))
lat1_dir = input("Latitude (Direction N/S): ")
lon1_d = int(input("Longitude (Degrees): "))
lon1_m = int(input("Longitude (Minutes): "))
lon1_s = int(input("Longitude (Seconds): "))
lon1_dir = input("Longitude (Direction E/W): ")

print("Enter the latitude and longitude of second location:")
lat2_d = int(input("Latitude (Degrees): "))
lat2_m = int(input("Latitude (Minutes): "))
lat2_s = int(input("Latitude (Seconds): "))
lat2_dir = input("Latitude (Direction N/S): ")
lon2_d = int(input("Longitude (Degrees): "))
lon2_m = int(input("Longitude (Minutes): "))
lon2_s = int(input("Longitude (Seconds): "))
lon2_dir = input("Longitude (Direction E/W): ")

lat1 = convert_to_decimal(lat1_d, lat1_m, lat1_s, lat1_dir)
lon1 = convert_to_decimal(lon1_d, lon1_m, lon1_s, lon1_dir)
lat2 = convert_to_decimal(lat2_d, lat2_m, lat2_s, lat2_dir)
lon2 = convert_to_decimal(lon2_d, lon2_m, lon2_s, lon2_dir)

# Print distance
print("Distance: ", distance(lat1, lon1, lat2, lon2), "nautical miles")

# Print initial and final bearing
initial_bearing, final_bearing = bearing(lat1, lon1, lat2, lon2)
print("Initial bearing: ", initial_bearing, "degrees")
print("Final bearing: ", final_bearing, "degrees")


#want to convert result in dms
initial_bearing_dms = decimal_to_dms(initial_bearing)
final_bearing_dms = decimal_to_dms(final_bearing)


print("Initial bearing: ", initial_bearing_dms[0], "degrees ", initial_bearing_dms[1], "minutes ", initial_bearing_dms[2], "seconds ", lat1_dir)
print("Final bearing: ", final_bearing_dms[0], "degrees ", final_bearing_dms[1], "minutes ", final_bearing_dms[2], "seconds ", lat2_dir)


Result: 




Explanatin:

for better understanding you need to read code. It is not that much hard...😊

No comments

Powered by Blogger.