etmerc.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Heavily based on this etmerc projection implementation
  2. // https://github.com/mbloch/mapshaper-proj/blob/master/src/projections/etmerc.js
  3. import tmerc from '../projections/tmerc';
  4. import sinh from '../common/sinh';
  5. import hypot from '../common/hypot';
  6. import asinhy from '../common/asinhy';
  7. import gatg from '../common/gatg';
  8. import clens from '../common/clens';
  9. import clens_cmplx from '../common/clens_cmplx';
  10. import adjust_lon from '../common/adjust_lon';
  11. export function init() {
  12. if (!this.approx && (isNaN(this.es) || this.es <= 0)) {
  13. throw new Error('Incorrect elliptical usage. Try using the +approx option in the proj string, or PROJECTION["Fast_Transverse_Mercator"] in the WKT.');
  14. }
  15. if (this.approx) {
  16. // When '+approx' is set, use tmerc instead
  17. tmerc.init.apply(this);
  18. this.forward = tmerc.forward;
  19. this.inverse = tmerc.inverse;
  20. }
  21. this.x0 = this.x0 !== undefined ? this.x0 : 0;
  22. this.y0 = this.y0 !== undefined ? this.y0 : 0;
  23. this.long0 = this.long0 !== undefined ? this.long0 : 0;
  24. this.lat0 = this.lat0 !== undefined ? this.lat0 : 0;
  25. this.cgb = [];
  26. this.cbg = [];
  27. this.utg = [];
  28. this.gtu = [];
  29. var f = this.es / (1 + Math.sqrt(1 - this.es));
  30. var n = f / (2 - f);
  31. var np = n;
  32. this.cgb[0] = n * (2 + n * (-2 / 3 + n * (-2 + n * (116 / 45 + n * (26 / 45 + n * (-2854 / 675 ))))));
  33. this.cbg[0] = n * (-2 + n * ( 2 / 3 + n * ( 4 / 3 + n * (-82 / 45 + n * (32 / 45 + n * (4642 / 4725))))));
  34. np = np * n;
  35. this.cgb[1] = np * (7 / 3 + n * (-8 / 5 + n * (-227 / 45 + n * (2704 / 315 + n * (2323 / 945)))));
  36. this.cbg[1] = np * (5 / 3 + n * (-16 / 15 + n * ( -13 / 9 + n * (904 / 315 + n * (-1522 / 945)))));
  37. np = np * n;
  38. this.cgb[2] = np * (56 / 15 + n * (-136 / 35 + n * (-1262 / 105 + n * (73814 / 2835))));
  39. this.cbg[2] = np * (-26 / 15 + n * (34 / 21 + n * (8 / 5 + n * (-12686 / 2835))));
  40. np = np * n;
  41. this.cgb[3] = np * (4279 / 630 + n * (-332 / 35 + n * (-399572 / 14175)));
  42. this.cbg[3] = np * (1237 / 630 + n * (-12 / 5 + n * ( -24832 / 14175)));
  43. np = np * n;
  44. this.cgb[4] = np * (4174 / 315 + n * (-144838 / 6237));
  45. this.cbg[4] = np * (-734 / 315 + n * (109598 / 31185));
  46. np = np * n;
  47. this.cgb[5] = np * (601676 / 22275);
  48. this.cbg[5] = np * (444337 / 155925);
  49. np = Math.pow(n, 2);
  50. this.Qn = this.k0 / (1 + n) * (1 + np * (1 / 4 + np * (1 / 64 + np / 256)));
  51. this.utg[0] = n * (-0.5 + n * ( 2 / 3 + n * (-37 / 96 + n * ( 1 / 360 + n * (81 / 512 + n * (-96199 / 604800))))));
  52. this.gtu[0] = n * (0.5 + n * (-2 / 3 + n * (5 / 16 + n * (41 / 180 + n * (-127 / 288 + n * (7891 / 37800))))));
  53. this.utg[1] = np * (-1 / 48 + n * (-1 / 15 + n * (437 / 1440 + n * (-46 / 105 + n * (1118711 / 3870720)))));
  54. this.gtu[1] = np * (13 / 48 + n * (-3 / 5 + n * (557 / 1440 + n * (281 / 630 + n * (-1983433 / 1935360)))));
  55. np = np * n;
  56. this.utg[2] = np * (-17 / 480 + n * (37 / 840 + n * (209 / 4480 + n * (-5569 / 90720 ))));
  57. this.gtu[2] = np * (61 / 240 + n * (-103 / 140 + n * (15061 / 26880 + n * (167603 / 181440))));
  58. np = np * n;
  59. this.utg[3] = np * (-4397 / 161280 + n * (11 / 504 + n * (830251 / 7257600)));
  60. this.gtu[3] = np * (49561 / 161280 + n * (-179 / 168 + n * (6601661 / 7257600)));
  61. np = np * n;
  62. this.utg[4] = np * (-4583 / 161280 + n * (108847 / 3991680));
  63. this.gtu[4] = np * (34729 / 80640 + n * (-3418889 / 1995840));
  64. np = np * n;
  65. this.utg[5] = np * (-20648693 / 638668800);
  66. this.gtu[5] = np * (212378941 / 319334400);
  67. var Z = gatg(this.cbg, this.lat0);
  68. this.Zb = -this.Qn * (Z + clens(this.gtu, 2 * Z));
  69. }
  70. export function forward(p) {
  71. var Ce = adjust_lon(p.x - this.long0);
  72. var Cn = p.y;
  73. Cn = gatg(this.cbg, Cn);
  74. var sin_Cn = Math.sin(Cn);
  75. var cos_Cn = Math.cos(Cn);
  76. var sin_Ce = Math.sin(Ce);
  77. var cos_Ce = Math.cos(Ce);
  78. Cn = Math.atan2(sin_Cn, cos_Ce * cos_Cn);
  79. Ce = Math.atan2(sin_Ce * cos_Cn, hypot(sin_Cn, cos_Cn * cos_Ce));
  80. Ce = asinhy(Math.tan(Ce));
  81. var tmp = clens_cmplx(this.gtu, 2 * Cn, 2 * Ce);
  82. Cn = Cn + tmp[0];
  83. Ce = Ce + tmp[1];
  84. var x;
  85. var y;
  86. if (Math.abs(Ce) <= 2.623395162778) {
  87. x = this.a * (this.Qn * Ce) + this.x0;
  88. y = this.a * (this.Qn * Cn + this.Zb) + this.y0;
  89. }
  90. else {
  91. x = Infinity;
  92. y = Infinity;
  93. }
  94. p.x = x;
  95. p.y = y;
  96. return p;
  97. }
  98. export function inverse(p) {
  99. var Ce = (p.x - this.x0) * (1 / this.a);
  100. var Cn = (p.y - this.y0) * (1 / this.a);
  101. Cn = (Cn - this.Zb) / this.Qn;
  102. Ce = Ce / this.Qn;
  103. var lon;
  104. var lat;
  105. if (Math.abs(Ce) <= 2.623395162778) {
  106. var tmp = clens_cmplx(this.utg, 2 * Cn, 2 * Ce);
  107. Cn = Cn + tmp[0];
  108. Ce = Ce + tmp[1];
  109. Ce = Math.atan(sinh(Ce));
  110. var sin_Cn = Math.sin(Cn);
  111. var cos_Cn = Math.cos(Cn);
  112. var sin_Ce = Math.sin(Ce);
  113. var cos_Ce = Math.cos(Ce);
  114. Cn = Math.atan2(sin_Cn * cos_Ce, hypot(sin_Ce, cos_Ce * cos_Cn));
  115. Ce = Math.atan2(sin_Ce, cos_Ce * cos_Cn);
  116. lon = adjust_lon(Ce + this.long0);
  117. lat = gatg(this.cgb, Cn);
  118. }
  119. else {
  120. lon = Infinity;
  121. lat = Infinity;
  122. }
  123. p.x = lon;
  124. p.y = lat;
  125. return p;
  126. }
  127. export var names = ["Extended_Transverse_Mercator", "Extended Transverse Mercator", "etmerc", "Transverse_Mercator", "Transverse Mercator", "tmerc"];
  128. export default {
  129. init: init,
  130. forward: forward,
  131. inverse: inverse,
  132. names: names
  133. };