{"record":{"id":"d0956394aa2048ce","repo":"gchq/CyberChef","slug":"could-not-convert-co-ordinates-to-os-national-grid","errorCode":null,"errorMessage":"Could not convert co-ordinates to OS National Grid. Are the co-ordinates in range?","messagePattern":"Could not convert co-ordinates to OS National Grid\\. Are the co-ordinates in range\\?","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/ConvertCoordinates.mjs","lineNumber":278,"sourceCode":"        case \"Geohash\":\n            convLat = geohash.encode(latlon.lat, latlon.lon, precision);\n            break;\n        case \"Military Grid Reference System\":\n            utm = latlon.toUtm();\n            mgrs = utm.toMgrs();\n            // MGRS wants a precision that's an even number between 2 and 10\n            if (precision % 2 !== 0) {\n                precision = precision + 1;\n            }\n            if (precision > 10) {\n                precision = 10;\n            }\n            convLat = mgrs.toString(precision);\n            break;\n        case \"Ordnance Survey National Grid\":\n            osng = OsGridRef.latLonToOsGrid(latlon);\n            if (osng.toString() === \"\") {\n                throw new OperationError(\"Could not convert co-ordinates to OS National Grid. Are the co-ordinates in range?\");\n            }\n            // OSNG wants a precision that's an even number between 2 and 10\n            if (precision % 2 !== 0) {\n                precision = precision + 1;\n            }\n            if (precision > 10) {\n                precision = 10;\n            }\n            convLat = osng.toString(precision);\n            break;\n        case \"Universal Transverse Mercator\":\n            utm = latlon.toUtm();\n            convLat = utm.toString(precision);\n            break;\n    }\n\n    if (convLat === undefined) {\n        throw new OperationError(\"Error converting co-ordinates.\");","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/ConvertCoordinates.mjs#L260-L296","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only request OSNG output for coordinates within Great Britain.","Switch outFormat to a global system (e.g. 'Decimal Degrees', 'Universal Transverse Mercator', 'Military Grid Reference System').","Pre-check the latitude/longitude falls within ~[49.9,60.9] N and [-8.7,1.8] E before choosing OSNG."],"exampleFix":"// before\nconvertCoordinates(\"40.7128, -74.0060\", \"Decimal Degrees\", \"Comma\",\n                  \"Ordnance Survey National Grid\", ...); // NYC -> out of range\n\n// after\nconvertCoordinates(\"40.7128, -74.0060\", \"Decimal Degrees\", \"Comma\",\n                  \"Universal Transverse Mercator\", ...);","handlingStrategy":"validation","validationCode":"function isInGB(lat, lon) {\n  return lat >= 49.9 && lat <= 60.9 && lon >= -8.7 && lon <= 1.8;\n}\nif (outFormat === \"Ordnance Survey National Grid\" && !isInGB(lat, lon)) {\n  throw new Error(\"OSNG output only valid for coordinates within Great Britain.\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  convertCoordinates(input, inFormat, inDelim, \"Ordnance Survey National Grid\", outDelim, includeDir, precision);\n} catch (e) {\n  if (/OS National Grid/i.test(e.message)) {\n    // coordinates out of UK range; fall back to a global format\n    convertCoordinates(input, inFormat, inDelim, \"Decimal Degrees\", outDelim, includeDir, precision);\n  } else throw e;\n}","preventionTips":["Only select OSNG output for UK coordinates.","For global datasets use UTM, MGRS, or Decimal Degrees.","Verify hemisphere/sign of lat/lon before conversion."],"tags":["coordinates","geo","osng","range-check","output-format"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}