cass.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var mlfn = require('../common/mlfn');
  2. var e0fn = require('../common/e0fn');
  3. var e1fn = require('../common/e1fn');
  4. var e2fn = require('../common/e2fn');
  5. var e3fn = require('../common/e3fn');
  6. var gN = require('../common/gN');
  7. var adjust_lon = require('../common/adjust_lon');
  8. var adjust_lat = require('../common/adjust_lat');
  9. var imlfn = require('../common/imlfn');
  10. var HALF_PI = Math.PI/2;
  11. var EPSLN = 1.0e-10;
  12. exports.init = function() {
  13. if (!this.sphere) {
  14. this.e0 = e0fn(this.es);
  15. this.e1 = e1fn(this.es);
  16. this.e2 = e2fn(this.es);
  17. this.e3 = e3fn(this.es);
  18. this.ml0 = this.a * mlfn(this.e0, this.e1, this.e2, this.e3, this.lat0);
  19. }
  20. };
  21. /* Cassini forward equations--mapping lat,long to x,y
  22. -----------------------------------------------------------------------*/
  23. exports.forward = function(p) {
  24. /* Forward equations
  25. -----------------*/
  26. var x, y;
  27. var lam = p.x;
  28. var phi = p.y;
  29. lam = adjust_lon(lam - this.long0);
  30. if (this.sphere) {
  31. x = this.a * Math.asin(Math.cos(phi) * Math.sin(lam));
  32. y = this.a * (Math.atan2(Math.tan(phi), Math.cos(lam)) - this.lat0);
  33. }
  34. else {
  35. //ellipsoid
  36. var sinphi = Math.sin(phi);
  37. var cosphi = Math.cos(phi);
  38. var nl = gN(this.a, this.e, sinphi);
  39. var tl = Math.tan(phi) * Math.tan(phi);
  40. var al = lam * Math.cos(phi);
  41. var asq = al * al;
  42. var cl = this.es * cosphi * cosphi / (1 - this.es);
  43. var ml = this.a * mlfn(this.e0, this.e1, this.e2, this.e3, phi);
  44. x = nl * al * (1 - asq * tl * (1 / 6 - (8 - tl + 8 * cl) * asq / 120));
  45. y = ml - this.ml0 + nl * sinphi / cosphi * asq * (0.5 + (5 - tl + 6 * cl) * asq / 24);
  46. }
  47. p.x = x + this.x0;
  48. p.y = y + this.y0;
  49. return p;
  50. };
  51. /* Inverse equations
  52. -----------------*/
  53. exports.inverse = function(p) {
  54. p.x -= this.x0;
  55. p.y -= this.y0;
  56. var x = p.x / this.a;
  57. var y = p.y / this.a;
  58. var phi, lam;
  59. if (this.sphere) {
  60. var dd = y + this.lat0;
  61. phi = Math.asin(Math.sin(dd) * Math.cos(x));
  62. lam = Math.atan2(Math.tan(x), Math.cos(dd));
  63. }
  64. else {
  65. /* ellipsoid */
  66. var ml1 = this.ml0 / this.a + y;
  67. var phi1 = imlfn(ml1, this.e0, this.e1, this.e2, this.e3);
  68. if (Math.abs(Math.abs(phi1) - HALF_PI) <= EPSLN) {
  69. p.x = this.long0;
  70. p.y = HALF_PI;
  71. if (y < 0) {
  72. p.y *= -1;
  73. }
  74. return p;
  75. }
  76. var nl1 = gN(this.a, this.e, Math.sin(phi1));
  77. var rl1 = nl1 * nl1 * nl1 / this.a / this.a * (1 - this.es);
  78. var tl1 = Math.pow(Math.tan(phi1), 2);
  79. var dl = x * this.a / nl1;
  80. var dsq = dl * dl;
  81. phi = phi1 - nl1 * Math.tan(phi1) / rl1 * dl * dl * (0.5 - (1 + 3 * tl1) * dl * dl / 24);
  82. lam = dl * (1 - dsq * (tl1 / 3 + (1 + 3 * tl1) * tl1 * dsq / 15)) / Math.cos(phi1);
  83. }
  84. p.x = adjust_lon(lam + this.long0);
  85. p.y = adjust_lat(phi);
  86. return p;
  87. };
  88. exports.names = ["Cassini", "Cassini_Soldner", "cass"];