vandg.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import adjust_lon from '../common/adjust_lon';
  2. import {HALF_PI, EPSLN} from '../constants/values';
  3. import asinz from '../common/asinz';
  4. /* Initialize the Van Der Grinten projection
  5. ----------------------------------------*/
  6. export function init() {
  7. //this.R = 6370997; //Radius of earth
  8. this.R = this.a;
  9. }
  10. export function forward(p) {
  11. var lon = p.x;
  12. var lat = p.y;
  13. /* Forward equations
  14. -----------------*/
  15. var dlon = adjust_lon(lon - this.long0);
  16. var x, y;
  17. if (Math.abs(lat) <= EPSLN) {
  18. x = this.x0 + this.R * dlon;
  19. y = this.y0;
  20. }
  21. var theta = asinz(2 * Math.abs(lat / Math.PI));
  22. if ((Math.abs(dlon) <= EPSLN) || (Math.abs(Math.abs(lat) - HALF_PI) <= EPSLN)) {
  23. x = this.x0;
  24. if (lat >= 0) {
  25. y = this.y0 + Math.PI * this.R * Math.tan(0.5 * theta);
  26. }
  27. else {
  28. y = this.y0 + Math.PI * this.R * -Math.tan(0.5 * theta);
  29. }
  30. // return(OK);
  31. }
  32. var al = 0.5 * Math.abs((Math.PI / dlon) - (dlon / Math.PI));
  33. var asq = al * al;
  34. var sinth = Math.sin(theta);
  35. var costh = Math.cos(theta);
  36. var g = costh / (sinth + costh - 1);
  37. var gsq = g * g;
  38. var m = g * (2 / sinth - 1);
  39. var msq = m * m;
  40. var con = Math.PI * this.R * (al * (g - msq) + Math.sqrt(asq * (g - msq) * (g - msq) - (msq + asq) * (gsq - msq))) / (msq + asq);
  41. if (dlon < 0) {
  42. con = -con;
  43. }
  44. x = this.x0 + con;
  45. //con = Math.abs(con / (Math.PI * this.R));
  46. var q = asq + g;
  47. con = Math.PI * this.R * (m * q - al * Math.sqrt((msq + asq) * (asq + 1) - q * q)) / (msq + asq);
  48. if (lat >= 0) {
  49. //y = this.y0 + Math.PI * this.R * Math.sqrt(1 - con * con - 2 * al * con);
  50. y = this.y0 + con;
  51. }
  52. else {
  53. //y = this.y0 - Math.PI * this.R * Math.sqrt(1 - con * con - 2 * al * con);
  54. y = this.y0 - con;
  55. }
  56. p.x = x;
  57. p.y = y;
  58. return p;
  59. }
  60. /* Van Der Grinten inverse equations--mapping x,y to lat/long
  61. ---------------------------------------------------------*/
  62. export function inverse(p) {
  63. var lon, lat;
  64. var xx, yy, xys, c1, c2, c3;
  65. var a1;
  66. var m1;
  67. var con;
  68. var th1;
  69. var d;
  70. /* inverse equations
  71. -----------------*/
  72. p.x -= this.x0;
  73. p.y -= this.y0;
  74. con = Math.PI * this.R;
  75. xx = p.x / con;
  76. yy = p.y / con;
  77. xys = xx * xx + yy * yy;
  78. c1 = -Math.abs(yy) * (1 + xys);
  79. c2 = c1 - 2 * yy * yy + xx * xx;
  80. c3 = -2 * c1 + 1 + 2 * yy * yy + xys * xys;
  81. d = yy * yy / c3 + (2 * c2 * c2 * c2 / c3 / c3 / c3 - 9 * c1 * c2 / c3 / c3) / 27;
  82. a1 = (c1 - c2 * c2 / 3 / c3) / c3;
  83. m1 = 2 * Math.sqrt(-a1 / 3);
  84. con = ((3 * d) / a1) / m1;
  85. if (Math.abs(con) > 1) {
  86. if (con >= 0) {
  87. con = 1;
  88. }
  89. else {
  90. con = -1;
  91. }
  92. }
  93. th1 = Math.acos(con) / 3;
  94. if (p.y >= 0) {
  95. lat = (-m1 * Math.cos(th1 + Math.PI / 3) - c2 / 3 / c3) * Math.PI;
  96. }
  97. else {
  98. lat = -(-m1 * Math.cos(th1 + Math.PI / 3) - c2 / 3 / c3) * Math.PI;
  99. }
  100. if (Math.abs(xx) < EPSLN) {
  101. lon = this.long0;
  102. }
  103. else {
  104. lon = adjust_lon(this.long0 + Math.PI * (xys - 1 + Math.sqrt(1 + 2 * (xx * xx - yy * yy) + xys * xys)) / 2 / xx);
  105. }
  106. p.x = lon;
  107. p.y = lat;
  108. return p;
  109. }
  110. export var names = ["Van_der_Grinten_I", "VanDerGrinten", "vandg"];
  111. export default {
  112. init: init,
  113. forward: forward,
  114. inverse: inverse,
  115. names: names
  116. };