geodesy
geodesy copied to clipboard
OSgrid Parsing Error
Hi,
I attempted to parse NA 991 920 expecting { easting: 099100, northing: 912000 } but instead receive { easting: 99100, northing: 912000 } when using OsGridRef.parse(na 991 920)
This looks to be an error with NA and NL grid squares, where the easting is losing it's leading '0'... could you please confirm?
I went through the pain of upgrading from 1.1.3 to 2.4.0 and including the ESM modules but it's still behaving as above. Please tell me I'm not crazy!
if (easting.length < 6) {
// Leading 0 is stripped off the easting value for westerly grid squares 01xxx - 09xxx
// add it back in
easting = '0' + easting;
console.log('Easting value updated to include leading 0, easting now', easting);
}
My temporary workaround
The (internal) easting and northing values are numerics rather than texts strings, so have no leading zeros.
If you want eastings/northings with leading zeros, a simpler way than testing the length of the stringified values would be to use padStart:
refPadded = { easting: String(ref.easting).padStart(6, '0'), northing: String(ref.northing).padStart(6, '0')) };
The OsGridRef.toString() method uses toLocaleString:
const format = { useGrouping: false, minimumIntegerDigits: 6, maximumFractionDigits: 3 };
refPadded = { easting: ref.easting.toLocaleString('en', format), northing: ref.northing.toLocaleString('en', format) }