gauss.js 1.4 KB

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