MGRS squares touching the antimeridian read: the zone check compares longitudes unwrapped

This commit is contained in:
Sameh Khamis committed 2026-11-15 09:50:44 -08:00
1 parent 262de71fec
commit 6a4202242d
2 files changed
+8 -1

No files matched your search

+5 -1
View File
@@ -257,7 +257,11 @@ function parseMgrs(text) {
const [zoneWest, zoneEast] = zoneLongitudes(zone, band); const [zoneWest, zoneEast] = zoneLongitudes(zone, band);
const lonSlack = const lonSlack =
slack / Math.max(Math.cos((point.lat * Math.PI) / 180), 0.01); slack / Math.max(Math.cos((point.lat * Math.PI) / 180), 0.01);
return point.lon >= zoneWest - lonSlack && point.lon <= zoneEast + lonSlack // Compare across the antimeridian unwrapped: zone 1 starts at 180°W.
let lon = point.lon;
if (lon - zoneWest > 180) lon -= 360;
if (zoneEast - lon > 180) lon += 360;
return lon >= zoneWest - lonSlack && lon <= zoneEast + lonSlack
? point ? point
: null; : null;
} }
+3
View File
@@ -257,6 +257,9 @@ test('MGRS squares across a zone edge, Norway and Svalbard', () => {
assert.ok(parseGridReference('33X VG 12345 67890')); assert.ok(parseGridReference('33X VG 12345 67890'));
// A square that lands outside its zone's longitudes. // A square that lands outside its zone's longitudes.
assert.equal(parseGridReference('18T SL 50000 50000'), null); assert.equal(parseGridReference('18T SL 50000 50000'), null);
// Squares touching the antimeridian, from either side.
for (const text of ['1HBD1970044714', '60TYQ364873', '1QAC860142'])
assert.ok(parseGridReference(text), text);
}); });
test('a UTM N is the northern hemisphere where band N does not fit', () => { test('a UTM N is the northern hemisphere where band N does not fit', () => {