gauss.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var FORTPI = Math.PI/4;
  2. var srat = require('../common/srat');
  3. var HALF_PI = Math.PI/2;
  4. var MAX_ITER = 20;
  5. exports.init = function() {
  6. var sphi = Math.sin(this.lat0);
  7. var cphi = Math.cos(this.lat0);
  8. cphi *= cphi;
  9. this.rc = Math.sqrt(1 - this.es) / (1 - this.es * sphi * sphi);
  10. this.C = Math.sqrt(1 + this.es * cphi * cphi / (1 - this.es));
  11. this.phic0 = Math.asin(sphi / this.C);
  12. this.ratexp = 0.5 * this.C * this.e;
  13. this.K = Math.tan(0.5 * this.phic0 + FORTPI) / (Math.pow(Math.tan(0.5 * this.lat0 + FORTPI), this.C) * srat(this.e * sphi, this.ratexp));
  14. };
  15. exports.forward = function(p) {
  16. var lon = p.x;
  17. var lat = p.y;
  18. p.y = 2 * Math.atan(this.K * Math.pow(Math.tan(0.5 * lat + FORTPI), this.C) * srat(this.e * Math.sin(lat), this.ratexp)) - HALF_PI;
  19. p.x = this.C * lon;
  20. return p;
  21. };
  22. exports.inverse = function(p) {
  23. var DEL_TOL = 1e-14;
  24. var lon = p.x / this.C;
  25. var lat = p.y;
  26. var num = Math.pow(Math.tan(0.5 * lat + FORTPI) / this.K, 1 / this.C);
  27. for (var i = MAX_ITER; i > 0; --i) {
  28. lat = 2 * Math.atan(num * srat(this.e * Math.sin(p.y), - 0.5 * this.e)) - HALF_PI;
  29. if (Math.abs(lat - p.y) < DEL_TOL) {
  30. break;
  31. }
  32. p.y = lat;
  33. }
  34. /* convergence failed */
  35. if (!i) {
  36. return null;
  37. }
  38. p.x = lon;
  39. p.y = lat;
  40. return p;
  41. };
  42. exports.names = ["gauss"];