Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OsGridRef.parse not handling grid references starting with H #82

Closed
plet56 opened this issue May 25, 2020 · 1 comment
Closed

OsGridRef.parse not handling grid references starting with H #82

plet56 opened this issue May 25, 2020 · 1 comment

Comments

@plet56
Copy link

plet56 commented May 25, 2020

For example: HY 2022 0003. It seems to fail at the second "sanity check" in OsGridRef.parse. The converted coordinates, if the sanity check removed, seem correct so I think it is just the sanity check which is wrong.

I'd suggest moving this check into the regular expression:

// validate format match = gridref.match(/^[HJNOST][A-Z]\s*[0-9]+\s*[0-9]+$/i); if (!match) throw new Error(invalid grid reference ‘${gridref}’`);

    // get numeric values of letter references, mapping A->0, B->1, C->2, etc:
    let l1 = gridref.toUpperCase().charCodeAt(0) - 'A'.charCodeAt(0);
    let l2 = gridref.toUpperCase().charCodeAt(1) - 'A'.charCodeAt(0);
    // shuffle down letters after 'I' since 'I' is not used in grid:
    if (l1 > 7) l1--;
    if (l2 > 7) l2--;

    // convert grid letters into 100km-square indexes from false origin (grid square SV):
    const e100km = ((l1 - 2) % 5) * 5 + (l2 % 5);
    const n100km = (19 - Math.floor(l1 / 5) * 5) - Math.floor(l2 / 5);

`

This is not perfect since invalid grid references can sneak through (e.g. TC) but it is probably an improvement.

@chrisveness
Copy link
Owner

Thanks for this. This has been lurking for a while, I guess there's not been so many users in Orkney / Shetland!

A more specific RegExp does seem the best way of doing this. Some meaningless watery grid references can still get through, but TC will be caught by the 'invalid easting' check in the constructor.

Fixed in 5941244.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants