aeqd.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var adjust_lon = require('../common/adjust_lon');
  2. var HALF_PI = Math.PI/2;
  3. var EPSLN = 1.0e-10;
  4. var mlfn = require('../common/mlfn');
  5. var e0fn = require('../common/e0fn');
  6. var e1fn = require('../common/e1fn');
  7. var e2fn = require('../common/e2fn');
  8. var e3fn = require('../common/e3fn');
  9. var gN = require('../common/gN');
  10. var asinz = require('../common/asinz');
  11. var imlfn = require('../common/imlfn');
  12. exports.init = function() {
  13. this.sin_p12 = Math.sin(this.lat0);
  14. this.cos_p12 = Math.cos(this.lat0);
  15. };
  16. exports.forward = function(p) {
  17. var lon = p.x;
  18. var lat = p.y;
  19. var sinphi = Math.sin(p.y);
  20. var cosphi = Math.cos(p.y);
  21. var dlon = adjust_lon(lon - this.long0);
  22. var e0, e1, e2, e3, Mlp, Ml, tanphi, Nl1, Nl, psi, Az, G, H, GH, Hs, c, kp, cos_c, s, s2, s3, s4, s5;
  23. if (this.sphere) {
  24. if (Math.abs(this.sin_p12 - 1) <= EPSLN) {
  25. //North Pole case
  26. p.x = this.x0 + this.a * (HALF_PI - lat) * Math.sin(dlon);
  27. p.y = this.y0 - this.a * (HALF_PI - lat) * Math.cos(dlon);
  28. return p;
  29. }
  30. else if (Math.abs(this.sin_p12 + 1) <= EPSLN) {
  31. //South Pole case
  32. p.x = this.x0 + this.a * (HALF_PI + lat) * Math.sin(dlon);
  33. p.y = this.y0 + this.a * (HALF_PI + lat) * Math.cos(dlon);
  34. return p;
  35. }
  36. else {
  37. //default case
  38. cos_c = this.sin_p12 * sinphi + this.cos_p12 * cosphi * Math.cos(dlon);
  39. c = Math.acos(cos_c);
  40. kp = c / Math.sin(c);
  41. p.x = this.x0 + this.a * kp * cosphi * Math.sin(dlon);
  42. p.y = this.y0 + this.a * kp * (this.cos_p12 * sinphi - this.sin_p12 * cosphi * Math.cos(dlon));
  43. return p;
  44. }
  45. }
  46. else {
  47. e0 = e0fn(this.es);
  48. e1 = e1fn(this.es);
  49. e2 = e2fn(this.es);
  50. e3 = e3fn(this.es);
  51. if (Math.abs(this.sin_p12 - 1) <= EPSLN) {
  52. //North Pole case
  53. Mlp = this.a * mlfn(e0, e1, e2, e3, HALF_PI);
  54. Ml = this.a * mlfn(e0, e1, e2, e3, lat);
  55. p.x = this.x0 + (Mlp - Ml) * Math.sin(dlon);
  56. p.y = this.y0 - (Mlp - Ml) * Math.cos(dlon);
  57. return p;
  58. }
  59. else if (Math.abs(this.sin_p12 + 1) <= EPSLN) {
  60. //South Pole case
  61. Mlp = this.a * mlfn(e0, e1, e2, e3, HALF_PI);
  62. Ml = this.a * mlfn(e0, e1, e2, e3, lat);
  63. p.x = this.x0 + (Mlp + Ml) * Math.sin(dlon);
  64. p.y = this.y0 + (Mlp + Ml) * Math.cos(dlon);
  65. return p;
  66. }
  67. else {
  68. //Default case
  69. tanphi = sinphi / cosphi;
  70. Nl1 = gN(this.a, this.e, this.sin_p12);
  71. Nl = gN(this.a, this.e, sinphi);
  72. psi = Math.atan((1 - this.es) * tanphi + this.es * Nl1 * this.sin_p12 / (Nl * cosphi));
  73. Az = Math.atan2(Math.sin(dlon), this.cos_p12 * Math.tan(psi) - this.sin_p12 * Math.cos(dlon));
  74. if (Az === 0) {
  75. s = Math.asin(this.cos_p12 * Math.sin(psi) - this.sin_p12 * Math.cos(psi));
  76. }
  77. else if (Math.abs(Math.abs(Az) - Math.PI) <= EPSLN) {
  78. s = -Math.asin(this.cos_p12 * Math.sin(psi) - this.sin_p12 * Math.cos(psi));
  79. }
  80. else {
  81. s = Math.asin(Math.sin(dlon) * Math.cos(psi) / Math.sin(Az));
  82. }
  83. G = this.e * this.sin_p12 / Math.sqrt(1 - this.es);
  84. H = this.e * this.cos_p12 * Math.cos(Az) / Math.sqrt(1 - this.es);
  85. GH = G * H;
  86. Hs = H * H;
  87. s2 = s * s;
  88. s3 = s2 * s;
  89. s4 = s3 * s;
  90. s5 = s4 * s;
  91. c = Nl1 * s * (1 - s2 * Hs * (1 - Hs) / 6 + s3 / 8 * GH * (1 - 2 * Hs) + s4 / 120 * (Hs * (4 - 7 * Hs) - 3 * G * G * (1 - 7 * Hs)) - s5 / 48 * GH);
  92. p.x = this.x0 + c * Math.sin(Az);
  93. p.y = this.y0 + c * Math.cos(Az);
  94. return p;
  95. }
  96. }
  97. };
  98. exports.inverse = function(p) {
  99. p.x -= this.x0;
  100. p.y -= this.y0;
  101. var rh, z, sinz, cosz, lon, lat, con, e0, e1, e2, e3, Mlp, M, N1, psi, Az, cosAz, tmp, A, B, D, Ee, F;
  102. if (this.sphere) {
  103. rh = Math.sqrt(p.x * p.x + p.y * p.y);
  104. if (rh > (2 * HALF_PI * this.a)) {
  105. return;
  106. }
  107. z = rh / this.a;
  108. sinz = Math.sin(z);
  109. cosz = Math.cos(z);
  110. lon = this.long0;
  111. if (Math.abs(rh) <= EPSLN) {
  112. lat = this.lat0;
  113. }
  114. else {
  115. lat = asinz(cosz * this.sin_p12 + (p.y * sinz * this.cos_p12) / rh);
  116. con = Math.abs(this.lat0) - HALF_PI;
  117. if (Math.abs(con) <= EPSLN) {
  118. if (this.lat0 >= 0) {
  119. lon = adjust_lon(this.long0 + Math.atan2(p.x, - p.y));
  120. }
  121. else {
  122. lon = adjust_lon(this.long0 - Math.atan2(-p.x, p.y));
  123. }
  124. }
  125. else {
  126. /*con = cosz - this.sin_p12 * Math.sin(lat);
  127. if ((Math.abs(con) < EPSLN) && (Math.abs(p.x) < EPSLN)) {
  128. //no-op, just keep the lon value as is
  129. } else {
  130. var temp = Math.atan2((p.x * sinz * this.cos_p12), (con * rh));
  131. lon = adjust_lon(this.long0 + Math.atan2((p.x * sinz * this.cos_p12), (con * rh)));
  132. }*/
  133. lon = adjust_lon(this.long0 + Math.atan2(p.x * sinz, rh * this.cos_p12 * cosz - p.y * this.sin_p12 * sinz));
  134. }
  135. }
  136. p.x = lon;
  137. p.y = lat;
  138. return p;
  139. }
  140. else {
  141. e0 = e0fn(this.es);
  142. e1 = e1fn(this.es);
  143. e2 = e2fn(this.es);
  144. e3 = e3fn(this.es);
  145. if (Math.abs(this.sin_p12 - 1) <= EPSLN) {
  146. //North pole case
  147. Mlp = this.a * mlfn(e0, e1, e2, e3, HALF_PI);
  148. rh = Math.sqrt(p.x * p.x + p.y * p.y);
  149. M = Mlp - rh;
  150. lat = imlfn(M / this.a, e0, e1, e2, e3);
  151. lon = adjust_lon(this.long0 + Math.atan2(p.x, - 1 * p.y));
  152. p.x = lon;
  153. p.y = lat;
  154. return p;
  155. }
  156. else if (Math.abs(this.sin_p12 + 1) <= EPSLN) {
  157. //South pole case
  158. Mlp = this.a * mlfn(e0, e1, e2, e3, HALF_PI);
  159. rh = Math.sqrt(p.x * p.x + p.y * p.y);
  160. M = rh - Mlp;
  161. lat = imlfn(M / this.a, e0, e1, e2, e3);
  162. lon = adjust_lon(this.long0 + Math.atan2(p.x, p.y));
  163. p.x = lon;
  164. p.y = lat;
  165. return p;
  166. }
  167. else {
  168. //default case
  169. rh = Math.sqrt(p.x * p.x + p.y * p.y);
  170. Az = Math.atan2(p.x, p.y);
  171. N1 = gN(this.a, this.e, this.sin_p12);
  172. cosAz = Math.cos(Az);
  173. tmp = this.e * this.cos_p12 * cosAz;
  174. A = -tmp * tmp / (1 - this.es);
  175. B = 3 * this.es * (1 - A) * this.sin_p12 * this.cos_p12 * cosAz / (1 - this.es);
  176. D = rh / N1;
  177. Ee = D - A * (1 + A) * Math.pow(D, 3) / 6 - B * (1 + 3 * A) * Math.pow(D, 4) / 24;
  178. F = 1 - A * Ee * Ee / 2 - D * Ee * Ee * Ee / 6;
  179. psi = Math.asin(this.sin_p12 * Math.cos(Ee) + this.cos_p12 * Math.sin(Ee) * cosAz);
  180. lon = adjust_lon(this.long0 + Math.asin(Math.sin(Az) * Math.sin(Ee) / Math.cos(psi)));
  181. lat = Math.atan((1 - this.es * F * this.sin_p12 / Math.sin(psi)) * Math.tan(psi) / (1 - this.es));
  182. p.x = lon;
  183. p.y = lat;
  184. return p;
  185. }
  186. }
  187. };
  188. exports.names = ["Azimuthal_Equidistant", "aeqd"];