ortho.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var adjust_lon = require('../common/adjust_lon');
  2. var EPSLN = 1.0e-10;
  3. var asinz = require('../common/asinz');
  4. var HALF_PI = Math.PI/2;
  5. exports.init = function() {
  6. //double temp; /* temporary variable */
  7. /* Place parameters in static storage for common use
  8. -------------------------------------------------*/
  9. this.sin_p14 = Math.sin(this.lat0);
  10. this.cos_p14 = Math.cos(this.lat0);
  11. };
  12. /* Orthographic forward equations--mapping lat,long to x,y
  13. ---------------------------------------------------*/
  14. exports.forward = function(p) {
  15. var sinphi, cosphi; /* sin and cos value */
  16. var dlon; /* delta longitude value */
  17. var coslon; /* cos of longitude */
  18. var ksp; /* scale factor */
  19. var g, x, y;
  20. var lon = p.x;
  21. var lat = p.y;
  22. /* Forward equations
  23. -----------------*/
  24. dlon = adjust_lon(lon - this.long0);
  25. sinphi = Math.sin(lat);
  26. cosphi = Math.cos(lat);
  27. coslon = Math.cos(dlon);
  28. g = this.sin_p14 * sinphi + this.cos_p14 * cosphi * coslon;
  29. ksp = 1;
  30. if ((g > 0) || (Math.abs(g) <= EPSLN)) {
  31. x = this.a * ksp * cosphi * Math.sin(dlon);
  32. y = this.y0 + this.a * ksp * (this.cos_p14 * sinphi - this.sin_p14 * cosphi * coslon);
  33. }
  34. p.x = x;
  35. p.y = y;
  36. return p;
  37. };
  38. exports.inverse = function(p) {
  39. var rh; /* height above ellipsoid */
  40. var z; /* angle */
  41. var sinz, cosz; /* sin of z and cos of z */
  42. var con;
  43. var lon, lat;
  44. /* Inverse equations
  45. -----------------*/
  46. p.x -= this.x0;
  47. p.y -= this.y0;
  48. rh = Math.sqrt(p.x * p.x + p.y * p.y);
  49. z = asinz(rh / this.a);
  50. sinz = Math.sin(z);
  51. cosz = Math.cos(z);
  52. lon = this.long0;
  53. if (Math.abs(rh) <= EPSLN) {
  54. lat = this.lat0;
  55. p.x = lon;
  56. p.y = lat;
  57. return p;
  58. }
  59. lat = asinz(cosz * this.sin_p14 + (p.y * sinz * this.cos_p14) / rh);
  60. con = Math.abs(this.lat0) - HALF_PI;
  61. if (Math.abs(con) <= EPSLN) {
  62. if (this.lat0 >= 0) {
  63. lon = adjust_lon(this.long0 + Math.atan2(p.x, - p.y));
  64. }
  65. else {
  66. lon = adjust_lon(this.long0 - Math.atan2(-p.x, p.y));
  67. }
  68. p.x = lon;
  69. p.y = lat;
  70. return p;
  71. }
  72. lon = adjust_lon(this.long0 + Math.atan2((p.x * sinz), rh * this.cos_p14 * cosz - p.y * this.sin_p14 * sinz));
  73. p.x = lon;
  74. p.y = lat;
  75. return p;
  76. };
  77. exports.names = ["ortho"];