From ae73973129c32e4f6eaf10d82cd00671eb221675 Mon Sep 17 00:00:00 2001 From: mkellogg Date: Wed, 4 Aug 2021 15:38:23 -0400 Subject: [PATCH 1/2] add MGRStoUSNG and MGRStoLL capability --- src/usng.ts | 29 ++++++++++++++++++++++++++ tests/tests.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/usng.ts b/src/usng.ts index b87ed03..ec87359 100755 --- a/src/usng.ts +++ b/src/usng.ts @@ -1403,6 +1403,35 @@ extend(Converter.prototype, { } }, + // convert MGRS to USNG by adding the appropriate spaces + MGRStoUSNG: function(mgrs){ + let eastNorthSpace, squareIdEastSpace, gridZoneSquareIdSpace + for(let i = mgrs.length - 1; i > -1; i--){ + // check if we have hit letters yet + if(isNaN(mgrs.substr(i,1))){ + squareIdEastSpace = i + 1 + break; + }; + } + gridZoneSquareIdSpace = squareIdEastSpace - 2 + let numPartLength = mgrs.substr(squareIdEastSpace).length / 2; + + eastNorthSpace = squareIdEastSpace + numPartLength; + let stringArray = mgrs.split(""); + + stringArray.splice(eastNorthSpace, 0, " "); + stringArray.splice(squareIdEastSpace, 0, " "); + stringArray.splice(gridZoneSquareIdSpace, 0, " "); + + let rejoinedArray = stringArray.join(""); + return rejoinedArray; + }, + + // convert MGRS to Lat Lon by chaining MGRStoUSNG and USNGtoLL together + MGRStoLL: function(mgrs){ + return mgrs ? this.USNGtoLL(this.MGRStoUSNG(mgrs)) : null; + }, + // wrapper function specific to Google Maps, to make a converstion to lat/lng return a GLatLon instance. // takes a usng string, converts it to lat/lng using a call to USNGtoLL, // and returns an instance of GLatLng diff --git a/tests/tests.js b/tests/tests.js index 9e993c4..ae89c3f 100755 --- a/tests/tests.js +++ b/tests/tests.js @@ -2705,3 +2705,59 @@ describe('Consistency with GEOTRANS', () => { }); }) }); + + +/* +{ + "mgrs": "31NFA0000000000", + "latitude": 0, + "longitude": 3.89864 + }, + { + "mgrs": "31NGA0000000000", + "latitude": 0, + "longitude": 4.79705 + }, + { + "mgrs": "31NHA0000000000", + "latitude": 0, + "longitude": 5.69502 + }, + { + "mgrs": "32NKF3200700000", + "latitude": 0, + "longitude": 6.59233 + }, + { + "mgrs": "30NYK6593199447", + "latitude": 4.5146, + "longitude": -0.60345 + }, +*/ + +describe('MGRStoUSNG', function(){ + it('should return 31N FA 00000 00000', function(){ + chai.assert.equal("31N FA 00000 00000", converter.MGRStoUSNG("31NFA0000000000")); + }); + + it('should return 31N GA 00000 00000', function(){ + chai.assert.equal("31N GA 00000 00000", converter.MGRStoUSNG("31NGA0000000000")); + }); + + it('should return 32N KF 32007 00000', function(){ + chai.assert.equal("32N KF 32007 00000", converter.MGRStoUSNG("32NKF3200700000")); + }); + + it('should return 30N YK 65931 99447', function(){ + chai.assert.equal("30N YK 65931 99447", converter.MGRStoUSNG("30NYK6593199447")); + }); + + it('should return 30N YK 6 5', function(){ + chai.assert.equal("30N YK 6 5", converter.MGRStoUSNG("30NYK65")); + }); + + it('should return 30N YK 659 319', function(){ + chai.assert.equal("30N YK 659 319", converter.MGRStoUSNG("30NYK659319")); + }); +}); + From 2735fe21082b2d9a5c7ecfbc85ac46ad21c860a2 Mon Sep 17 00:00:00 2001 From: mkellogg Date: Wed, 4 Aug 2021 15:43:38 -0400 Subject: [PATCH 2/2] removed comments --- tests/tests.js | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index ae89c3f..3283e11 100755 --- a/tests/tests.js +++ b/tests/tests.js @@ -2706,35 +2706,6 @@ describe('Consistency with GEOTRANS', () => { }) }); - -/* -{ - "mgrs": "31NFA0000000000", - "latitude": 0, - "longitude": 3.89864 - }, - { - "mgrs": "31NGA0000000000", - "latitude": 0, - "longitude": 4.79705 - }, - { - "mgrs": "31NHA0000000000", - "latitude": 0, - "longitude": 5.69502 - }, - { - "mgrs": "32NKF3200700000", - "latitude": 0, - "longitude": 6.59233 - }, - { - "mgrs": "30NYK6593199447", - "latitude": 4.5146, - "longitude": -0.60345 - }, -*/ - describe('MGRStoUSNG', function(){ it('should return 31N FA 00000 00000', function(){ chai.assert.equal("31N FA 00000 00000", converter.MGRStoUSNG("31NFA0000000000"));