Skip to content

Add mgrs conversions #44

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/usng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2705,3 +2705,30 @@ describe('Consistency with GEOTRANS', () => {
});
})
});

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