Skip to content

Commit fe42660

Browse files
Pamela CanoPamela Cano
authored andcommitted
Add latitude and longitude validation with doctests
1 parent 7e4b60b commit fe42660

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

geodesy/lamberts_ellipsoidal_distance.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,28 @@
66
AXIS_B = 6356752.314245
77
EQUATORIAL_RADIUS = 6378137
88

9-
109
def lamberts_ellipsoidal_distance(
1110
lat1: float, lon1: float, lat2: float, lon2: float
1211
) -> float:
1312
"""
14-
Calculate the shortest distance along the surface of an ellipsoid between
15-
two points on the surface of earth given longitudes and latitudes
16-
https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines
17-
18-
NOTE: This algorithm uses geodesy/haversine_distance.py to compute central angle,
19-
sigma
20-
21-
Representing the earth as an ellipsoid allows us to approximate distances between
22-
points on the surface much better than a sphere. Ellipsoidal formulas treat the
23-
Earth as an oblate ellipsoid which means accounting for the flattening that happens
24-
at the North and South poles. Lambert's formulae provide accuracy on the order of
25-
10 meteres over thousands of kilometeres. Other methods can provide
26-
millimeter-level accuracy but this is a simpler method to calculate long range
27-
distances without increasing computational intensity.
2813
2914
Args:
3015
lat1, lon1: latitude and longitude of coordinate 1
3116
lat2, lon2: latitude and longitude of coordinate 2
17+
3218
Returns:
3319
geographical distance between two points in metres
3420
21+
>>> lamberts_ellipsoidal_distance(100, 0, 0, 0)
22+
Traceback (most recent call last):
23+
...
24+
ValueError: Latitude must be between -90 and 90 degrees
25+
26+
>>> lamberts_ellipsoidal_distance(0, 200, 0, 0)
27+
Traceback (most recent call last):
28+
...
29+
ValueError: Longitude must be between -180 and 180 degrees
30+
3531
>>> from collections import namedtuple
3632
>>> point_2d = namedtuple("point_2d", "lat lon")
3733
>>> SAN_FRANCISCO = point_2d(37.774856, -122.424227)
@@ -46,6 +42,14 @@ def lamberts_ellipsoidal_distance(
4642
'9,737,326 meters'
4743
"""
4844

45+
# Validate latitude values
46+
if not -90 <= lat1 <= 90 or not -90 <= lat2 <= 90:
47+
raise ValueError("Latitude must be between -90 and 90 degrees")
48+
49+
# Validate longitude values
50+
if not -180 <= lon1 <= 180 or not -180 <= lon2 <= 180:
51+
raise ValueError("Longitude must be between -180 and 180 degrees")
52+
4953
# CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System
5054
# Distance in metres(m)
5155
# Equation Parameters

0 commit comments

Comments
 (0)