#! /usr/bin/python3 # =================================================================== # Given two lat/lon points on a sphere (the earth), # calculate the great circle distance # =================================================================== # www.geeksforgeeks.org/program-distance-two-points-earth/ # wikipedia: en.wikipedia.org/wiki/Haversine_formula # =================================================================== from math import pi, radians, cos, sin, asin, sqrt # ------------------------------------------------------------------- # function: calculate great circle distance # ------------------------------------------------------------------- def great_circle_distance(rad,lat1,lon1,lat2,lon2): # ---- convert degrees to radians lat1 = radians(lat1) lon1 = radians(lon1) lat2 = radians(lat2) lon2 = radians(lon2) # ---- Haversine formula dlat = lat2 - lat1 dlon = lon2 - lon1 a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2 c = 2 * asin(sqrt(a)) return (c * rad) # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- if '__main__' == __name__: # ---- radius of earth radm = 3956.0 # miles radk = 6371.0 # kilometers # ---- test values from article (webpage) d = great_circle_distance(radk, 53.32055555555556, # lat1 -1.7297222222222221, # lon1 53.31861111111111, # lat2 -1.6997222222222223) # lon2 print(f'great circle distance: {d}') print()