elastic/elasticsearch · error · IllegalArgumentException

invalid longitude

Error message

invalid longitude 

What it means

Thrown by GeographyValidator.checkLongitude when a longitude value is NaN or falls outside the inclusive range [-180.0, 180.0]. Note that longitudes outside [-180, 180] are NOT auto-wrapped here; the validator rejects them. (Antimeridian crossing is handled by allowing minX > maxX in the bbox, not by accepting out-of-range longitudes.)

Source

Thrown at libs/geo/src/main/java/org/elasticsearch/geometry/utils/GeographyValidator.java:82

    }

    /**
     * validates latitude value is within standard +/-90 coordinate bounds
     */
    protected void checkLatitude(double latitude) {
        if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) {
            throw new IllegalArgumentException(
                "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL
            );
        }
    }

    /**
     * validates longitude value is within standard +/-180 coordinate bounds
     */
    protected void checkLongitude(double longitude) {
        if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) {
            throw new IllegalArgumentException(
                "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL
            );
        }
    }

    protected void checkAltitude(double zValue) {
        if (ignoreZValue == false && Double.isNaN(zValue) == false) {
            throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] parameter is [" + ignoreZValue + "]");
        }
    }

    @Override
    public void validateCoordinate(double x, double y, double z) {
        checkLongitude(x);
        checkLatitude(y);
        checkAltitude(z);
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Normalize longitudes to [-180, 180] before validating: `lon = ((lon + 540) % 360) - 180;`
  2. Confirm the field ordering is (lon, lat) for validateCoordinate — GeographyValidator.validateCoordinate treats x as longitude, y as latitude.
  3. Filter NaN if missing data should be skipped.

Example fix

// before
GeographyValidator.instance(true).validateCoordinate(350.0, 45.0, Double.NaN); // lon=350 invalid

// after
double lon = ((350.0 + 540) % 360) - 180; // -> -10
GeographyValidator.instance(true).validateCoordinate(lon, 45.0, Double.NaN);
Defensive patterns

Strategy: validation

Validate before calling

if (Double.isNaN(lon) || lon < -180.0 || lon > 180.0) {
    lon = ((lon + 540) % 360) - 180; // normalize from [0,360) or other
    if (Double.isNaN(lon) || lon < -180.0 || lon > 180.0) {
        throw new IllegalArgumentException("longitude out of range: " + lon);
    }
}
GeographyValidator.instance(ignoreZ).validateCoordinate(lon, lat, z);

Type guard

static boolean isValidLongitude(double lon) {
    return !Double.isNaN(lon) && lon >= -180.0 && lon <= 180.0;
}

Prevention

When it happens

Trigger: Any path that calls checkLongitude directly or indirectly (validateCoordinate, validate(Geometry), etc.) with an x-value that is NaN, less than -180.0, or greater than 180.0.

Common situations: Raw longitudes in [0, 360) range (some data sources use this instead of [-180, 180)); computations that sum/subtract longitudes without wrapping; swapped lat/lon where a latitude is sent as longitude (this rarely fails since lat is in [-90, 90]); NaN from missing fields.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/194896cc66535293. Report an issue: GitHub.