mgrs
Version:
Utility for converting between WGS84 lat/lng and MGRS coordinates
3 lines (2 loc) • 6.14 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).mgrs={})}(this,(function(e){"use strict";const t=65,r=73,n=79;function o(e){return e*(Math.PI/180)}function a(e){return e/Math.PI*180}function i(e){const t=e.northing,r=e.easting,{zoneLetter:n,zoneNumber:o}=e;if(o<0||o>60)return null;const s=6378137,c=(1-Math.sqrt(.99330562))/(1+Math.sqrt(.99330562)),f=r-5e5;let h=t;n<"N"&&(h-=1e7);const u=6*(o-1)-180+3,l=h/.9996/6367449.145945056,b=l+(3*c/2-27*c*c*c/32)*Math.sin(2*l)+(21*c*c/16-55*c*c*c*c/32)*Math.sin(4*l)+151*c*c*c/96*Math.sin(6*l),d=s/Math.sqrt(1-.00669438*Math.sin(b)*Math.sin(b)),M=Math.tan(b)*Math.tan(b),g=.006739496752268451*Math.cos(b)*Math.cos(b),p=.99330562*s/Math.pow(1-.00669438*Math.sin(b)*Math.sin(b),1.5),w=f/(.9996*d);let y=b-d*Math.tan(b)/p*(w*w/2-(5+3*M+10*g-4*g*g-.06065547077041606)*w*w*w*w/24+(61+90*M+298*g+45*M*M-1.6983531815716497-3*g*g)*w*w*w*w*w*w/720);y=a(y);let m,A=(w-(1+2*M+g)*w*w*w/6+(5-2*g+28*M-3*g*g+.05391597401814761+24*M*M)*w*w*w*w*w/120)/Math.cos(b);if(A=u+a(A),"number"==typeof e.accuracy){const t=i({northing:e.northing+e.accuracy,easting:e.easting+e.accuracy,zoneLetter:e.zoneLetter,zoneNumber:e.zoneNumber});m={top:t.lat,right:t.lon,bottom:y,left:A}}else m={lat:y,lon:A};return m}function s(e){if(e<=84&&e>=72)return"X";if(e<72&&e>=-80){const t=8,r=-80;return"CDEFGHJKLMNPQRSTUVWX"[Math.floor((e-r)/t)]}return e>84||e<-80?"Z":void 0}function c(e){let t=e%6;return 0===t&&(t=6),t}function f(e){if(e&&0===e.length)throw new TypeError("MGRSPoint coverting from nothing");e=e.replace(/ /g,"");const{length:o}=e;let a,i=null,s="",f=0;for(;!/[A-Z]/.test(a=e.charAt(f));){if(f>=2)throw new Error("MGRSPoint bad conversion from: "+e);s+=a,f++}const u=parseInt(s,10);if(0===f||f+3>o)throw new Error("MGRSPoint bad conversion from "+e);const l=e.charAt(f++);if(l<="A"||"B"===l||"Y"===l||l>="Z"||"I"===l||"O"===l)throw new Error(`MGRSPoint zone letter ${l} not handled: ${e}`);i=e.substring(f,f+=2);const b=c(u),d=function(e,o){let a="AJSAJS".charCodeAt(o-1),i=1e5,s=!1;for(;a!==e.charCodeAt(0);){if(a++,a===r&&a++,a===n&&a++,a>90){if(s)throw new Error("Bad character: "+e);a=t,s=!0}i+=1e5}return i}(i.charAt(0),b);let M=function(e,o){if(e>"V")throw new TypeError("MGRSPoint given invalid Northing "+e);let a="AFAFAF".charCodeAt(o-1),i=0,s=!1;for(;a!==e.charCodeAt(0);){if(a++,a===r&&a++,a===n&&a++,a>86){if(s)throw new Error("Bad character: "+e);a=t,s=!0}i+=1e5}return i}(i.charAt(1),b);for(;M<h(l);)M+=2e6;const g=o-f;if(g%2!=0)throw new Error("MGRSPoint has to have an even number\nof digits after the zone letter and two 100km letters - front\nhalf for easting meters, second half for\nnorthing meters "+e);const p=g/2;let w,y,m,A=0,k=0;p>0&&(w=1e5/Math.pow(10,p),y=e.substring(f,f+p),A=parseFloat(y)*w,m=e.substring(f+p),k=parseFloat(m)*w);return{easting:A+d,northing:k+M,zoneLetter:l,zoneNumber:u,accuracy:w}}function h(e){let t;switch(e){case"C":t=11e5;break;case"D":t=2e6;break;case"E":t=28e5;break;case"F":t=37e5;break;case"G":t=46e5;break;case"H":t=55e5;break;case"J":t=64e5;break;case"K":t=73e5;break;case"L":t=82e5;break;case"M":t=91e5;break;case"N":t=0;break;case"P":t=8e5;break;case"Q":t=17e5;break;case"R":t=26e5;break;case"S":t=35e5;break;case"T":t=44e5;break;case"U":t=53e5;break;case"V":t=62e5;break;case"W":t=7e6;break;case"X":t=79e5;break;default:t=-1}if(t>=0)return t;throw new TypeError("Invalid zone letter: "+e)}e.forward=function(e,a){if(a="number"==typeof a?a:5,!Array.isArray(e))throw new TypeError("forward did not receive an array");if("string"==typeof e[0]||"string"==typeof e[1])throw new TypeError("forward received an array of strings, but it only accepts an array of numbers.");const[i,f]=e;if(i<-180||i>180)throw new TypeError("forward received an invalid longitude of "+i);if(f<-90||f>90)throw new TypeError("forward received an invalid latitude of "+f);if(f<-80||f>84)throw new TypeError(`forward received a latitude of ${f}, but this library does not support conversions of points in polar regions below 80°S and above 84°N`);return function(e,o){const a="00000"+e.easting,i="00000"+e.northing;return e.zoneNumber+e.zoneLetter+function(e,o,a){const i=c(a),s=Math.floor(e/1e5),f=Math.floor(o/1e5)%20;return function(e,o,a){const i=a-1,s="AJSAJS".charCodeAt(i),c="AFAFAF".charCodeAt(i);let f=s+e-1,h=c+o,u=!1;f>90&&(f=f-90+t-1,u=!0);(f===r||s<r&&f>r||(f>r||s<r)&&u)&&f++;(f===n||s<n&&f>n||(f>n||s<n)&&u)&&(f++,f===r&&f++);f>90&&(f=f-90+t-1);h>86?(h=h-86+t-1,u=!0):u=!1;(h===r||c<r&&h>r||(h>r||c<r)&&u)&&h++;(h===n||c<n&&h>n||(h>n||c<n)&&u)&&(h++,h===r&&h++);h>86&&(h=h-86+t-1);return String.fromCharCode(f)+String.fromCharCode(h)}(s,f,i)}(e.easting,e.northing,e.zoneNumber)+a.substr(a.length-5,o)+i.substr(i.length-5,o)}(function(e){const t=e.lat,r=e.lon,n=6378137,a=o(t),i=o(r);let c;c=Math.floor((r+180)/6)+1,180===r&&(c=60);t>=56&&t<64&&r>=3&&r<12&&(c=32);t>=72&&t<84&&(r>=0&&r<9?c=31:r>=9&&r<21?c=33:r>=21&&r<33?c=35:r>=33&&r<42&&(c=37));const f=o(6*(c-1)-180+3),h=n/Math.sqrt(1-.00669438*Math.sin(a)*Math.sin(a)),u=Math.tan(a)*Math.tan(a),l=.006739496752268451*Math.cos(a)*Math.cos(a),b=Math.cos(a)*(i-f),d=n*(.9983242984503243*a-.002514607064228144*Math.sin(2*a)+2639046602129982e-21*Math.sin(4*a)-3.418046101696858e-9*Math.sin(6*a)),M=.9996*h*(b+(1-u+l)*b*b*b/6+(5-18*u+u*u+72*l-.39089081163157013)*b*b*b*b*b/120)+5e5;let g=.9996*(d+h*Math.tan(a)*(b*b/2+(5-u+9*l+4*l*l)*b*b*b*b/24+(61-58*u+u*u+600*l-2.2240339282485886)*b*b*b*b*b*b/720));t<0&&(g+=1e7);return{northing:Math.trunc(g),easting:Math.trunc(M),zoneNumber:c,zoneLetter:s(t)}}({lat:f,lon:i}),a)},e.getLetterDesignator=s,e.inverse=function(e){const t=i(f(e.toUpperCase()));return"number"==typeof t.lat&&"number"==typeof t.lon?[t.lon,t.lat,t.lon,t.lat]:[t.left,t.bottom,t.right,t.top]},e.toPoint=function(e){if(""===e)throw new TypeError("toPoint received a blank string");const t=i(f(e.toUpperCase()));return"number"==typeof t.lat&&"number"==typeof t.lon?[t.lon,t.lat]:[(t.left+t.right)/2,(t.top+t.bottom)/2]},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=mgrs.min.js.map