TheAlgorithms/Python · error · ValueError

Longitude must be between -180 and 180 degrees

Error message

Longitude must be between -180 and 180 degrees

What it means

Raised by lamberts_ellipsoidal_distance() when either longitude falls outside [-180, 180]. The formula feeds longitudes into trigonometric central-angle computation; values outside the geodetic range produce wrong distances silently, so the function rejects them up front. Checked after latitude, so a bad latitude masks a bad longitude.

Source

Thrown at geodesy/lamberts_ellipsoidal_distance.py:75

    >>> 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_RADIUS

    # Intermediate P and Q values
    p_value = (b_lat1 + b_lat2) / 2
    q_value = (b_lat2 - b_lat1) / 2

View on GitHub (pinned to f5988cc097)

Solutions

  1. Normalize longitudes to [-180, 180]: lon = ((lon + 180) % 360) - 180.
  2. Fix the source convention: convert [0, 360) data before calling.
  3. Double-check argument order — the function signature is (lat1, lon1, lat2, lon2).

Example fix

# before
d = lamberts_ellipsoidal_distance(37.8, 190.0, 40.7, -74.0)  # 0..360 convention

# after
lon1 = ((190.0 + 180) % 360) - 180  # -170.0
d = lamberts_ellipsoidal_distance(37.8, lon1, 40.7, -74.0)
Defensive patterns

Strategy: validation

Validate before calling

def wrap_lon(lon: float) -> float:
    return ((lon + 180.0) % 360.0) - 180.0
lon1, lon2 = wrap_lon(lon1), wrap_lon(lon2)
if not all(-180 <= l <= 180 for l in (lon1, lon2)):
    raise ValueError('longitude out of range')
lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)

Type guard

def is_valid_lon(lon: float) -> bool:
    return isinstance(lon, (int, float)) and -180.0 <= lon <= 180.0

Try / catch

try:
    d = lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)
except ValueError as exc:
    if 'Longitude' in str(exc):
        lon1, lon2 = wrap_lon(lon1), wrap_lon(lon2)
        d = lamberts_ellipsoidal_distance(lat1, lon1, lat2, lon2)
    else:
        raise

Prevention

When it happens

Trigger: Calling lamberts_ellipsoidal_distance(40.7, -190.0, 45.4, 12.3) — lon1 < -180. Values just above 180 (e.g. 190) typically come from 0-360-convention data.

Common situations: Longitudes from systems using [0, 360) instead of [-180, 180] (common in satellite/remote-sensing data — 190 vs -170), arithmetic that adds offsets past 180 without wrapping, or dateline-crossing computations that never normalize.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/f032737166bb6e56. Report an issue: GitHub.