ortho.js 2.3 KB

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