2020-02-20 13:34:43 +00:00
|
|
|
from math import atan, cos, radians, sin, tan
|
2020-07-06 07:44:19 +00:00
|
|
|
|
2020-09-28 21:41:04 +00:00
|
|
|
from .haversine_distance import haversine_distance
|
2020-02-20 13:34:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def lamberts_ellipsoidal_distance(
|
|
|
|
lat1: float, lon1: float, lat2: float, lon2: float
|
|
|
|
) -> float:
|
|
|
|
|
|
|
|
"""
|
|
|
|
Calculate the shortest distance along the surface of an ellipsoid between
|
|
|
|
two points on the surface of earth given longitudes and latitudes
|
|
|
|
https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines
|
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
NOTE: This algorithm uses geodesy/haversine_distance.py to compute central angle,
|
|
|
|
sigma
|
2020-02-20 13:34:43 +00:00
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
Representing the earth as an ellipsoid allows us to approximate distances between
|
|
|
|
points on the surface much better than a sphere. Ellipsoidal formulas treat the
|
|
|
|
Earth as an oblate ellipsoid which means accounting for the flattening that happens
|
|
|
|
at the North and South poles. Lambert's formulae provide accuracy on the order of
|
|
|
|
10 meteres over thousands of kilometeres. Other methods can provide
|
|
|
|
millimeter-level accuracy but this is a simpler method to calculate long range
|
|
|
|
distances without increasing computational intensity.
|
2020-02-20 13:34:43 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
lat1, lon1: latitude and longitude of coordinate 1
|
|
|
|
lat2, lon2: latitude and longitude of coordinate 2
|
|
|
|
Returns:
|
|
|
|
geographical distance between two points in metres
|
|
|
|
|
|
|
|
>>> from collections import namedtuple
|
|
|
|
>>> point_2d = namedtuple("point_2d", "lat lon")
|
|
|
|
>>> SAN_FRANCISCO = point_2d(37.774856, -122.424227)
|
|
|
|
>>> YOSEMITE = point_2d(37.864742, -119.537521)
|
|
|
|
>>> NEW_YORK = point_2d(40.713019, -74.012647)
|
|
|
|
>>> VENICE = point_2d(45.443012, 12.313071)
|
|
|
|
>>> f"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *YOSEMITE):0,.0f} meters"
|
|
|
|
'254,351 meters'
|
|
|
|
>>> f"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *NEW_YORK):0,.0f} meters"
|
|
|
|
'4,138,992 meters'
|
|
|
|
>>> f"{lamberts_ellipsoidal_distance(*SAN_FRANCISCO, *VENICE):0,.0f} meters"
|
|
|
|
'9,737,326 meters'
|
|
|
|
"""
|
|
|
|
|
|
|
|
# CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System
|
|
|
|
# Distance in metres(m)
|
2022-10-12 22:54:20 +00:00
|
|
|
AXIS_A = 6378137.0 # noqa: N806
|
|
|
|
AXIS_B = 6356752.314245 # noqa: N806
|
|
|
|
EQUATORIAL_RADIUS = 6378137 # noqa: N806
|
2020-02-20 13:34:43 +00:00
|
|
|
|
|
|
|
# Equation Parameters
|
|
|
|
# https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines
|
|
|
|
flattening = (AXIS_A - AXIS_B) / AXIS_A
|
2020-06-16 08:09:19 +00:00
|
|
|
# Parametric latitudes
|
|
|
|
# https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude
|
2020-02-20 13:34:43 +00:00
|
|
|
b_lat1 = atan((1 - flattening) * tan(radians(lat1)))
|
|
|
|
b_lat2 = atan((1 - flattening) * tan(radians(lat2)))
|
|
|
|
|
|
|
|
# Compute central angle between two points
|
|
|
|
# using haversine theta. sigma = haversine_distance / equatorial radius
|
|
|
|
sigma = haversine_distance(lat1, lon1, lat2, lon2) / EQUATORIAL_RADIUS
|
|
|
|
|
|
|
|
# Intermediate P and Q values
|
2022-10-12 22:54:20 +00:00
|
|
|
p_value = (b_lat1 + b_lat2) / 2
|
|
|
|
q_value = (b_lat2 - b_lat1) / 2
|
2020-02-20 13:34:43 +00:00
|
|
|
|
|
|
|
# Intermediate X value
|
|
|
|
# X = (sigma - sin(sigma)) * sin^2Pcos^2Q / cos^2(sigma/2)
|
2022-10-12 22:54:20 +00:00
|
|
|
x_numerator = (sin(p_value) ** 2) * (cos(q_value) ** 2)
|
|
|
|
x_demonimator = cos(sigma / 2) ** 2
|
|
|
|
x_value = (sigma - sin(sigma)) * (x_numerator / x_demonimator)
|
2020-02-20 13:34:43 +00:00
|
|
|
|
|
|
|
# Intermediate Y value
|
|
|
|
# Y = (sigma + sin(sigma)) * cos^2Psin^2Q / sin^2(sigma/2)
|
2022-10-12 22:54:20 +00:00
|
|
|
y_numerator = (cos(p_value) ** 2) * (sin(q_value) ** 2)
|
|
|
|
y_denominator = sin(sigma / 2) ** 2
|
|
|
|
y_value = (sigma + sin(sigma)) * (y_numerator / y_denominator)
|
2020-02-20 13:34:43 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
return EQUATORIAL_RADIUS * (sigma - ((flattening / 2) * (x_value + y_value)))
|
2020-02-20 13:34:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|