# Posted By

richardkendall on 01/25/11

# Statistics

Viewed 376 times
Favorited by 0 user(s)

# js ngr to lat/long

/ Published in: JavaScript
`function to_rad(deg) {    return deg * (Math.PI/180);} function to_deg(rad) {    return rad * (180/Math.PI);} function tanx(px, pa) {    var tres = Math.tan(pa);    for(i=1;i<px;i++) {        tres *= Math.tan(pa);    }    return tres;} function sinx(px, pa) {    var tres = Math.sin(pa);    for(i=1;i<px;i++) {        tres *= Math.sin(pa);    }    return tres;} function cosx(px, pa) {    var tres = Math.cos(pa);    for(i=1;i<px;i++) {        tres *= Math.cos(pa);    }    return tres;} function sec(z) {    return 1 / Math.cos(z);} function ppGC(c) {    var tSeconds = Math.abs(c*3600);    var tDeg = Math.floor(tSeconds / 3600);    var tMin = Math.floor((tSeconds / 60) % 60);    tSeconds = Math.abs(tSeconds % 60);     return tDeg + "&deg; " + tMin + "' " + Math.round(tSeconds) + "\"";} function convert_ngr(e, n) {    var a = 6377563.396;    var b = 6356256.910;    var n0 = -100000;    var e0 = 400000;    var t0 = 49;    var l0 = -2;    var f0 = 0.9996012717;    var e2 = 0;    var nx = 0;    var tp = 0;    var Ma = 0;    var Mb = 0;    var Mc = 0;    var Md = 0;    var M = 0;    var c0 = 0;    var v = 0;    var p = 0;    var n2 = 0;    var VII = 0;    var VIII = 0;    var IX = 0;    var tX = 0;    var XI = 0;    var XII = 0;    var XIIA = 0;    var T = 0;    var L = 0;    var res = new Array(2);     t0 = to_rad(t0);    l0 = to_rad(l0);    e2 = 1 - (b*b)/(a*a);    nx = (a-b)/(a+b);     tp = ((n - n0) / (a * f0)) + t0;     Ma = (1 + nx + (5/4)*(nx*nx) + (5/4)*(nx*nx*nx))*(tp-t0);    Mb = (3*nx + 3*(nx*nx) + (21/8)*(nx*nx*nx)) * Math.sin(tp - t0) * Math.cos(tp + t0);    Mc = ((15/8)*(nx*nx) + (15/8)*(nx*nx*nx)) * Math.sin(2*(tp - t0)) * Math.cos(2*(tp + t0));    Md = (35/24)*(nx*nx*nx) * Math.sin(3*(tp - t0)) *  Math.cos(3*(tp + t0));    M = b*f0*(Ma - Mb + Mc - Md);     while ((n - n0 - M) >= 0.01) {        // continue to calc tp and M        tp = ((n - n0 - M) / (a * f0)) + tp;        Ma = (1 + nx + (5/4)*(nx*nx) + (5/4)*(nx*nx*nx))*(tp-t0);        Mb = (3*nx + 3*(nx*nx) + (21/8)*(nx*nx*nx)) * Math.sin(tp - t0) * Math.cos(tp + t0);        Mc = ((15/8)*(nx*nx) + (15/8)*(nx*nx*nx)) * Math.sin(2*(tp - t0)) * Math.cos(2*(tp + t0));        Md = (35/24)*(nx*nx*nx) * Math.sin(3*(tp - t0)) *  Math.cos(3*(tp + t0));        M = b*f0*(Ma - Mb + Mc - Md);    }     tp = ((n - n0 - M) / (a * f0)) + tp;     c0 = (1 - e2*sinx(2,tp));    v = a*f0*(1 / Math.sqrt(c0));    p = a*f0*(1 - e2)*((1 / c0) * (1 / Math.sqrt(c0)));    n2 = (v/p) - 1;     VII = Math.tan(tp) / (2*p*v);    VIII = (Math.tan(tp) / (24*p*(v*v*v)))*(5+3*tanx(2,tp)+n2-9*(tanx(2,tp))*n2);    IX = (Math.tan(tp) / (720*p*(v*v*v*v*v)))*(61+90*tanx(2,tp)+45*tanx(4,tp));    tX = sec(tp) / v;    XI = (sec(tp) / (6*(v*v*v)))*((v/p)+2*tanx(2,tp));    XII = (sec(tp) / (120*(v*v*v*v*v)))*(5+28*tanx(2,tp)+24*tanx(4,tp));    XIIA = (sec(tp) / (5040*(v*v*v*v*v*v*v)))*(61+662*tanx(2,tp)+1320*tanx(4,tp)+720*tanx(6,tp));     T = tp - (VII*((e-e0)*(e-e0))) + (VIII*((e-e0)*(e-e0)*(e-e0)*(e-e0))) - (IX*((e-e0)*(e-e0)*(e-e0)*(e-e0)*(e-e0)*(e-e0)));    L = l0 + tX*(e-e0) - XI*((e-e0)*(e-e0)*(e-e0)) + XII*((e-e0)*(e-e0)*(e-e0)*(e-e0)*(e-e0)) - XIIA*((e-e0)*(e-e0)*(e-e0)*(e-e0)*(e-e0)*(e-e0)*(e-e0));     res[0] = to_deg(T);    res[1] = to_deg(L);    return res;}`