Skip to content

Instantly share code, notes, and snippets.

@xandout
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xandout/ac521d45db7923ec15d4 to your computer and use it in GitHub Desktop.
Save xandout/ac521d45db7923ec15d4 to your computer and use it in GitHub Desktop.
Convert NC Grid to LatLng
/*
Usage: var convertedGrid = gridToLatLng("3542A8115A");
Returns:
{ sw: [ 35.7125, -81.2625 ],
se: [ 35.7125, -81.26666666666667 ],
ne: [ 35.71666666666667, -81.26666666666667 ],
nw: [ 35.71666666666667, -81.2625 ] }
Grids in NC are 1/4(.25) seconds square. The grid notation is the encoded southwest corner of a .25 second square.
This may not be useful outside of NC 811.
Credit for reverse engineering goes to AH.
*/
function gridToLatLng(grid){
String.prototype.replaceArray = function(find, replace) {
var replaceString = this;
var regex;
for (var i = 0; i < find.length; i++) {
regex = new RegExp(find[i], "g");
replaceString = replaceString.replace(regex, replace[i]);
}
return replaceString;
};
grid = grid.replaceArray(["D","C","B","A"],
["00","15","30","45"]);
var t = [];
for(var x = 0; x<grid.length; x+=2){
t.push(grid.substr(x, 2));
}
var lat = t.splice(0, t.length/2);
var lng = t;
for (var i in lat){
lat[i] = parseFloat(lat[i]);
}
for (var i in lng){
lng[i] = parseFloat(lng[i]);
}
var step = 15/3600;
lat = lat[2]/3600 + lat[1]/60 + lat[0];
lng = -(lng[2]/3600 + lng[1]/60 + lng[0]);
var sw = [lat, lng];
var se = [lat, lng - step];
var nw = [lat + step, lng];
var ne = [lat + step, lng - step];
return {"sw":sw, "se":se, "ne":ne, "nw": nw};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment