gchq/CyberChef · error · OperationError

Could not convert co-ordinates to OS National Grid. Are the

Error message

Could not convert co-ordinates to OS National Grid. Are the co-ordinates in range?

What it means

Thrown in the 'Ordnance Survey National Grid' output branch when OsGridRef.latLonToOsGrid returns an object whose toString() is empty. The OS National Grid only covers Great Britain (roughly lat 49.9-60.9 N, lon -8.7 to 1.8 E); coordinates outside that region produce an empty grid reference and the error fires.

Source

Thrown at src/core/lib/ConvertCoordinates.mjs:278

        case "Geohash":
            convLat = geohash.encode(latlon.lat, latlon.lon, precision);
            break;
        case "Military Grid Reference System":
            utm = latlon.toUtm();
            mgrs = utm.toMgrs();
            // MGRS wants a precision that's an even number between 2 and 10
            if (precision % 2 !== 0) {
                precision = precision + 1;
            }
            if (precision > 10) {
                precision = 10;
            }
            convLat = mgrs.toString(precision);
            break;
        case "Ordnance Survey National Grid":
            osng = OsGridRef.latLonToOsGrid(latlon);
            if (osng.toString() === "") {
                throw new OperationError("Could not convert co-ordinates to OS National Grid. Are the co-ordinates in range?");
            }
            // OSNG wants a precision that's an even number between 2 and 10
            if (precision % 2 !== 0) {
                precision = precision + 1;
            }
            if (precision > 10) {
                precision = 10;
            }
            convLat = osng.toString(precision);
            break;
        case "Universal Transverse Mercator":
            utm = latlon.toUtm();
            convLat = utm.toString(precision);
            break;
    }

    if (convLat === undefined) {
        throw new OperationError("Error converting co-ordinates.");

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Only request OSNG output for coordinates within Great Britain.
  2. Switch outFormat to a global system (e.g. 'Decimal Degrees', 'Universal Transverse Mercator', 'Military Grid Reference System').
  3. Pre-check the latitude/longitude falls within ~[49.9,60.9] N and [-8.7,1.8] E before choosing OSNG.

Example fix

// before
convertCoordinates("40.7128, -74.0060", "Decimal Degrees", "Comma",
                  "Ordnance Survey National Grid", ...); // NYC -> out of range

// after
convertCoordinates("40.7128, -74.0060", "Decimal Degrees", "Comma",
                  "Universal Transverse Mercator", ...);
Defensive patterns

Strategy: validation

Validate before calling

function isInGB(lat, lon) {
  return lat >= 49.9 && lat <= 60.9 && lon >= -8.7 && lon <= 1.8;
}
if (outFormat === "Ordnance Survey National Grid" && !isInGB(lat, lon)) {
  throw new Error("OSNG output only valid for coordinates within Great Britain.");
}

Try / catch

try {
  convertCoordinates(input, inFormat, inDelim, "Ordnance Survey National Grid", outDelim, includeDir, precision);
} catch (e) {
  if (/OS National Grid/i.test(e.message)) {
    // coordinates out of UK range; fall back to a global format
    convertCoordinates(input, inFormat, inDelim, "Decimal Degrees", outDelim, includeDir, precision);
  } else throw e;
}

Prevention

When it happens

Trigger: outFormat='Ordnance Survey National Grid' with input coordinates anywhere outside mainland UK — e.g. US, continental Europe, or the southern hemisphere. The latlon-to-OSGB conversion yields an empty string.

Common situations: Bulk-converting a global dataset to OSNG; selecting OSNG as output by mistake; coordinates given in the wrong hemisphere due to a missing negative sign.

Related errors


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