poly.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import e0fn from '../common/e0fn';
  2. import e1fn from '../common/e1fn';
  3. import e2fn from '../common/e2fn';
  4. import e3fn from '../common/e3fn';
  5. import adjust_lon from '../common/adjust_lon';
  6. import adjust_lat from '../common/adjust_lat';
  7. import mlfn from '../common/mlfn';
  8. import {EPSLN} from '../constants/values';
  9. import gN from '../common/gN';
  10. var MAX_ITER = 20;
  11. export function init() {
  12. /* Place parameters in static storage for common use
  13. -------------------------------------------------*/
  14. this.temp = this.b / this.a;
  15. this.es = 1 - Math.pow(this.temp, 2); // devait etre dans tmerc.js mais n y est pas donc je commente sinon retour de valeurs nulles
  16. this.e = Math.sqrt(this.es);
  17. this.e0 = e0fn(this.es);
  18. this.e1 = e1fn(this.es);
  19. this.e2 = e2fn(this.es);
  20. this.e3 = e3fn(this.es);
  21. this.ml0 = this.a * mlfn(this.e0, this.e1, this.e2, this.e3, this.lat0); //si que des zeros le calcul ne se fait pas
  22. }
  23. /* Polyconic forward equations--mapping lat,long to x,y
  24. ---------------------------------------------------*/
  25. export function forward(p) {
  26. var lon = p.x;
  27. var lat = p.y;
  28. var x, y, el;
  29. var dlon = adjust_lon(lon - this.long0);
  30. el = dlon * Math.sin(lat);
  31. if (this.sphere) {
  32. if (Math.abs(lat) <= EPSLN) {
  33. x = this.a * dlon;
  34. y = -1 * this.a * this.lat0;
  35. }
  36. else {
  37. x = this.a * Math.sin(el) / Math.tan(lat);
  38. y = this.a * (adjust_lat(lat - this.lat0) + (1 - Math.cos(el)) / Math.tan(lat));
  39. }
  40. }
  41. else {
  42. if (Math.abs(lat) <= EPSLN) {
  43. x = this.a * dlon;
  44. y = -1 * this.ml0;
  45. }
  46. else {
  47. var nl = gN(this.a, this.e, Math.sin(lat)) / Math.tan(lat);
  48. x = nl * Math.sin(el);
  49. y = this.a * mlfn(this.e0, this.e1, this.e2, this.e3, lat) - this.ml0 + nl * (1 - Math.cos(el));
  50. }
  51. }
  52. p.x = x + this.x0;
  53. p.y = y + this.y0;
  54. return p;
  55. }
  56. /* Inverse equations
  57. -----------------*/
  58. export function inverse(p) {
  59. var lon, lat, x, y, i;
  60. var al, bl;
  61. var phi, dphi;
  62. x = p.x - this.x0;
  63. y = p.y - this.y0;
  64. if (this.sphere) {
  65. if (Math.abs(y + this.a * this.lat0) <= EPSLN) {
  66. lon = adjust_lon(x / this.a + this.long0);
  67. lat = 0;
  68. }
  69. else {
  70. al = this.lat0 + y / this.a;
  71. bl = x * x / this.a / this.a + al * al;
  72. phi = al;
  73. var tanphi;
  74. for (i = MAX_ITER; i; --i) {
  75. tanphi = Math.tan(phi);
  76. dphi = -1 * (al * (phi * tanphi + 1) - phi - 0.5 * (phi * phi + bl) * tanphi) / ((phi - al) / tanphi - 1);
  77. phi += dphi;
  78. if (Math.abs(dphi) <= EPSLN) {
  79. lat = phi;
  80. break;
  81. }
  82. }
  83. lon = adjust_lon(this.long0 + (Math.asin(x * Math.tan(phi) / this.a)) / Math.sin(lat));
  84. }
  85. }
  86. else {
  87. if (Math.abs(y + this.ml0) <= EPSLN) {
  88. lat = 0;
  89. lon = adjust_lon(this.long0 + x / this.a);
  90. }
  91. else {
  92. al = (this.ml0 + y) / this.a;
  93. bl = x * x / this.a / this.a + al * al;
  94. phi = al;
  95. var cl, mln, mlnp, ma;
  96. var con;
  97. for (i = MAX_ITER; i; --i) {
  98. con = this.e * Math.sin(phi);
  99. cl = Math.sqrt(1 - con * con) * Math.tan(phi);
  100. mln = this.a * mlfn(this.e0, this.e1, this.e2, this.e3, phi);
  101. mlnp = this.e0 - 2 * this.e1 * Math.cos(2 * phi) + 4 * this.e2 * Math.cos(4 * phi) - 6 * this.e3 * Math.cos(6 * phi);
  102. ma = mln / this.a;
  103. dphi = (al * (cl * ma + 1) - ma - 0.5 * cl * (ma * ma + bl)) / (this.es * Math.sin(2 * phi) * (ma * ma + bl - 2 * al * ma) / (4 * cl) + (al - ma) * (cl * mlnp - 2 / Math.sin(2 * phi)) - mlnp);
  104. phi -= dphi;
  105. if (Math.abs(dphi) <= EPSLN) {
  106. lat = phi;
  107. break;
  108. }
  109. }
  110. //lat=phi4z(this.e,this.e0,this.e1,this.e2,this.e3,al,bl,0,0);
  111. cl = Math.sqrt(1 - this.es * Math.pow(Math.sin(lat), 2)) * Math.tan(lat);
  112. lon = adjust_lon(this.long0 + Math.asin(x * cl / this.a) / Math.sin(lat));
  113. }
  114. }
  115. p.x = lon;
  116. p.y = lat;
  117. return p;
  118. }
  119. export var names = ["Polyconic", "poly"];
  120. export default {
  121. init: init,
  122. forward: forward,
  123. inverse: inverse,
  124. names: names
  125. };