TheAlgorithms/Python · error · ValueError
Latitude must be between -90 and 90 degrees
Error message
Latitude must be between -90 and 90 degrees
What it means
Raised by lamberts_ellipsoidal_distance() in geodesy/lamberts_ellipsoidal_distance.py when either latitude argument falls outside [-90, 90]. Lambert's formula converts latitudes via tan/radians for the parametric latitude; out-of-range values are geometrically invalid on the WGS84 ellipsoid and rejected before the trigonometry runs.
Source
Thrown at geodesy/lamberts_ellipsoidal_distance.py:71
ValueError: Longitude must be between -180 and 180 degrees
>>> 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'
"""
# Validate latitude values
if not -90 <= lat1 <= 90 or not -90 <= lat2 <= 90:
raise ValueError("Latitude must be between -90 and 90 degrees")
# Validate longitude values
if not -180 <= lon1 <= 180 or not -180 <= lon2 <= 180:
raise ValueError("Longitude must be between -180 and 180 degrees")
# CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System
# Distance in metres(m)
# Equation Parameters
# https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines
flattening = (AXIS_A - AXIS_B) / AXIS_A
# Parametric latitudes
# https://en.wikipedia.org/wiki/Latitude#Parametric_(or_reduced)_latitude
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_RADIUSView on GitHub (pinned to f5988cc097)
Solutions
- Ensure both latitudes are within [-90, 90].
- If lat/lon were swapped at the call site, swap them back — check the longitude error that may follow.
- Normalize coordinates upstream: clamp or recompute from the source CRS.
Example fix
# before lamberts_ellipsoidal_distance(lon1, lat1, lon2, lat2) # swapped order # after lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2) # lat first
Defensive patterns
Strategy: validation
Validate before calling
for name, lat in (('lat1', lat1), ('lat2', lat2)):
if not -90 <= lat <= 90:
raise ValueError(f'{name}={lat} outside [-90, 90]')
lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2) Type guard
def is_valid_lat(lat: float) -> bool:
return isinstance(lat, (int, float)) and -90.0 <= lat <= 90.0 Try / catch
try:
d = lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)
except ValueError as exc:
if 'Latitude' in str(exc):
raise ValueError(f'bad input coords: {(lat1, lon1, lat2, lon2)}') from exc
raise Prevention
- Validate lat/lon order at ingestion — order swaps are the top cause.
- Validate both latitude and longitude together; the function checks lat before lon.
When it happens
Trigger: Calling lamberts_ellipsoidal_distance(95.5, -122.4, 40.7, -74.0) — lat1 > 90. Note the check tests both lat1 and lat2 with one condition, so either bad coordinate raises the same error without saying which.
Common situations: Coordinate-order mixups (lat/lon swapped — longitudes like 40.7 pass as valid latitudes, so swaps often surface here or at the longitude check), missing minus signs on southern-hemisphere latitudes after arithmetic, or unnormalized data from projections emitting values beyond geodetic range.
Related errors
- Longitude must be between -180 and 180 degrees
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
- Depth cannot be less than 0
- The parameter costs should be a list of three integers
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/6443f7d6ad3433f2.
Report an issue: GitHub.