js ngr to lat/long


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function to_rad(deg) {
  2. return deg * (Math.PI/180);
  3. }
  4.  
  5. function to_deg(rad) {
  6. return rad * (180/Math.PI);
  7. }
  8.  
  9. function tanx(px, pa) {
  10. var tres = Math.tan(pa);
  11. for(i=1;i<px;i++) {
  12. tres *= Math.tan(pa);
  13. }
  14. return tres;
  15. }
  16.  
  17. function sinx(px, pa) {
  18. var tres = Math.sin(pa);
  19. for(i=1;i<px;i++) {
  20. tres *= Math.sin(pa);
  21. }
  22. return tres;
  23. }
  24.  
  25. function cosx(px, pa) {
  26. var tres = Math.cos(pa);
  27. for(i=1;i<px;i++) {
  28. tres *= Math.cos(pa);
  29. }
  30. return tres;
  31. }
  32.  
  33. function sec(z) {
  34. return 1 / Math.cos(z);
  35. }
  36.  
  37. function ppGC(c) {
  38. var tSeconds = Math.abs(c*3600);
  39. var tDeg = Math.floor(tSeconds / 3600);
  40. var tMin = Math.floor((tSeconds / 60) % 60);
  41. tSeconds = Math.abs(tSeconds % 60);
  42.  
  43. return tDeg + "&deg; " + tMin + "' " + Math.round(tSeconds) + "\"";
  44. }
  45.  
  46. function convert_ngr(e, n) {
  47. var a = 6377563.396;
  48. var b = 6356256.910;
  49. var n0 = -100000;
  50. var e0 = 400000;
  51. var t0 = 49;
  52. var l0 = -2;
  53. var f0 = 0.9996012717;
  54. var e2 = 0;
  55. var nx = 0;
  56. var tp = 0;
  57. var Ma = 0;
  58. var Mb = 0;
  59. var Mc = 0;
  60. var Md = 0;
  61. var M = 0;
  62. var c0 = 0;
  63. var v = 0;
  64. var p = 0;
  65. var n2 = 0;
  66. var VII = 0;
  67. var VIII = 0;
  68. var IX = 0;
  69. var tX = 0;
  70. var XI = 0;
  71. var XII = 0;
  72. var XIIA = 0;
  73. var T = 0;
  74. var L = 0;
  75. var res = new Array(2);
  76.  
  77. t0 = to_rad(t0);
  78. l0 = to_rad(l0);
  79. e2 = 1 - (b*b)/(a*a);
  80. nx = (a-b)/(a+b);
  81.  
  82. tp = ((n - n0) / (a * f0)) + t0;
  83.  
  84. Ma = (1 + nx + (5/4)*(nx*nx) + (5/4)*(nx*nx*nx))*(tp-t0);
  85. Mb = (3*nx + 3*(nx*nx) + (21/8)*(nx*nx*nx)) * Math.sin(tp - t0) * Math.cos(tp + t0);
  86. Mc = ((15/8)*(nx*nx) + (15/8)*(nx*nx*nx)) * Math.sin(2*(tp - t0)) * Math.cos(2*(tp + t0));
  87. Md = (35/24)*(nx*nx*nx) * Math.sin(3*(tp - t0)) * Math.cos(3*(tp + t0));
  88. M = b*f0*(Ma - Mb + Mc - Md);
  89.  
  90. while ((n - n0 - M) >= 0.01) {
  91. // continue to calc tp and M
  92. tp = ((n - n0 - M) / (a * f0)) + tp;
  93. Ma = (1 + nx + (5/4)*(nx*nx) + (5/4)*(nx*nx*nx))*(tp-t0);
  94. Mb = (3*nx + 3*(nx*nx) + (21/8)*(nx*nx*nx)) * Math.sin(tp - t0) * Math.cos(tp + t0);
  95. Mc = ((15/8)*(nx*nx) + (15/8)*(nx*nx*nx)) * Math.sin(2*(tp - t0)) * Math.cos(2*(tp + t0));
  96. Md = (35/24)*(nx*nx*nx) * Math.sin(3*(tp - t0)) * Math.cos(3*(tp + t0));
  97. M = b*f0*(Ma - Mb + Mc - Md);
  98. }
  99.  
  100. tp = ((n - n0 - M) / (a * f0)) + tp;
  101.  
  102. c0 = (1 - e2*sinx(2,tp));
  103. v = a*f0*(1 / Math.sqrt(c0));
  104. p = a*f0*(1 - e2)*((1 / c0) * (1 / Math.sqrt(c0)));
  105. n2 = (v/p) - 1;
  106.  
  107. VII = Math.tan(tp) / (2*p*v);
  108. VIII = (Math.tan(tp) / (24*p*(v*v*v)))*(5+3*tanx(2,tp)+n2-9*(tanx(2,tp))*n2);
  109. IX = (Math.tan(tp) / (720*p*(v*v*v*v*v)))*(61+90*tanx(2,tp)+45*tanx(4,tp));
  110. tX = sec(tp) / v;
  111. XI = (sec(tp) / (6*(v*v*v)))*((v/p)+2*tanx(2,tp));
  112. XII = (sec(tp) / (120*(v*v*v*v*v)))*(5+28*tanx(2,tp)+24*tanx(4,tp));
  113. XIIA = (sec(tp) / (5040*(v*v*v*v*v*v*v)))*(61+662*tanx(2,tp)+1320*tanx(4,tp)+720*tanx(6,tp));
  114.  
  115. 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)));
  116. 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));
  117.  
  118. res[0] = to_deg(T);
  119. res[1] = to_deg(L);
  120. return res;
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.