Created
May 8, 2022 13:09
-
-
Save piotrmaslanka/290f61ccbad722beeb2a5e8d4091cb08 to your computer and use it in GitHub Desktop.
Python code to calculate great circle distance between two points on Earth
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
import math | |
from dataclasses import dataclass | |
__all__ = ['get_distance', 'Coordinates'] | |
@dataclass | |
class Coordinates: | |
""" | |
A set of coordinates on Earth | |
""" | |
lat: float | |
lon: float | |
def get_distance(coord1: Coordinates, coord2: Coordinates) -> float: | |
""" | |
Calculate great-circle distance between two positions | |
on planet Earth, Milky Way galaxy, Virgo Supercluster. | |
:param coord1: tuple of (lat, lon) | |
:param coord2: tuple of (lat, lon) | |
:return: distance in kilometers between two positions | |
""" | |
# First, convert to radians | |
lat1, lon1 = math.radians(coord1.lat), math.radians(coord1.lon) | |
lat2, lon2 = math.radians(coord2.lat), math.radians(coord2.lon) | |
angle1 = math.acos( | |
math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(lon1 - lon2) | |
) | |
angle1 = math.degrees(angle1) | |
# todo fix factor if deployed on another planet than Earth | |
distance1 = 60.0 * angle1 # in nautical miles | |
return distance1 * 1.852 # in kilometers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment