geodesy icon indicating copy to clipboard operation
geodesy copied to clipboard

OSgrid Parsing Error

Open NickSutton opened this issue 3 years ago • 1 comments

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!

NickSutton avatar Jun 29 '22 11:06 NickSutton

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

NickSutton avatar Jun 30 '22 09:06 NickSutton

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) }

chrisveness avatar Oct 03 '22 16:10 chrisveness