gchq/CyberChef · error · OperationError

Input must in the format lat1, lng1, lat2, lng2

Error message

Input must in the format lat1, lng1, lat2, lng2

What it means

Thrown by Haversine Distance when the input fails to match the strict regex for four decimal coordinates. The expected format is `lat1, lng1, lat2, lng2` where each value is an optional minus, integer digits, and optional decimal fraction. Whitespace is only tolerated after the commas (one optional space).

Source

Thrown at src/core/operations/HaversineDistance.mjs:39

        this.name = "Haversine distance";
        this.module = "Default";
        this.description = "Returns the distance between two pairs of GPS latitude and longitude co-ordinates in metres.<br><br>e.g. <code>51.487263,-0.124323, 38.9517,-77.1467</code>";
        this.infoURL = "https://wikipedia.org/wiki/Haversine_formula";
        this.inputType = "string";
        this.outputType = "number";
        this.args = [];
    }

    /**
     * @param {string} input
     * @param {Object[]} args
     * @returns {number}
     */
    run(input, args) {

        const values = input.match(/^(-?\d+(\.\d+)?), ?(-?\d+(\.\d+)?), ?(-?\d+(\.\d+)?), ?(-?\d+(\.\d+)?)$/);
        if (!values) {
            throw new OperationError("Input must in the format lat1, lng1, lat2, lng2");
        }

        const lat1 = parseFloat(values[1]);
        const lng1 = parseFloat(values[3]);
        const lat2 = parseFloat(values[5]);
        const lng2 = parseFloat(values[7]);

        const TO_RAD = Math.PI / 180;
        const dLat = (lat2-lat1) * TO_RAD;
        const dLng = (lng2-lng1) * TO_RAD;
        const a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat1 * TO_RAD) * Math.cos(lat2 * TO_RAD) * Math.sin(dLng/2) * Math.sin(dLng/2);
        const metres = 6371000 * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

        return metres;

    }

}

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Format the input as four plain decimal numbers separated by commas: 'lat1, lng1, lat2, lng2'.
  2. Strip cardinal letters, degree symbols, minutes/seconds - convert DMS to decimal first.
  3. Ensure exactly one optional space after each comma and no spaces inside numbers.
  4. Validate the line with the same regex before submitting.

Example fix

// before
run("51.487263N, -0.124323W, 38.9517, -77.1467", []);
// after - decimals only, no cardinal letters
run("51.487263, -0.124323, 38.9517, -77.1467", []);
Defensive patterns

Strategy: validation

Validate before calling

const HAVERSINE_RE = /^(-?\d+(\.\d+)?), ?(-?\d+(\.\d+)?), ?(-?\d+(\.\d+)?), ?(-?\d+(\.\d+)?)$/;
function assertHaversineInput(input) {
  if (!HAVERSINE_RE.test(input.trim())) {
    throw new Error('Input must be four decimal coords: lat1, lng1, lat2, lng2');
  }
}

Type guard

function isHaversineInput(input) {
  return HAVERSINE_RE.test(typeof input === 'string' ? input.trim() : '');
}

Try / catch

try {
  metres = haversine.run(input, []);
} catch (e) {
  if (e instanceof OperationError && /format lat1/i.test(e.message)) {
    // normalise: strip cardinal letters and degree symbols, then retry
    const clean = input.replace(/[NSEW]/gi, '').replace(/°/g, '');
    metres = haversine.run(clean, []);
  } else throw e;
}

Prevention

When it happens

Trigger: Missing a coordinate; extra text; using semicolons or tabs instead of commas; values in scientific/engineering notation (1e3); DMS coordinates (51°30'); trailing units (m/deg); inconsistent spacing ('lat1,lng1,lat2,lng2' with no spaces works, but 'lat1 ,lng1' fails).

Common situations: Pasting coordinates from a map UI that includes cardinal letters (N/S/E/W) or degree symbols; mixing decimal and DMS; CSV export using a different separator; negative longitude missing the minus.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/5952f29b0710a5ef. Report an issue: GitHub.