merc.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var msfnz = require('../common/msfnz');
  2. var HALF_PI = Math.PI/2;
  3. var EPSLN = 1.0e-10;
  4. var R2D = 57.29577951308232088;
  5. var adjust_lon = require('../common/adjust_lon');
  6. var FORTPI = Math.PI/4;
  7. var tsfnz = require('../common/tsfnz');
  8. var phi2z = require('../common/phi2z');
  9. exports.init = function() {
  10. var con = this.b / this.a;
  11. this.es = 1 - con * con;
  12. if(!('x0' in this)){
  13. this.x0 = 0;
  14. }
  15. if(!('y0' in this)){
  16. this.y0 = 0;
  17. }
  18. this.e = Math.sqrt(this.es);
  19. if (this.lat_ts) {
  20. if (this.sphere) {
  21. this.k0 = Math.cos(this.lat_ts);
  22. }
  23. else {
  24. this.k0 = msfnz(this.e, Math.sin(this.lat_ts), Math.cos(this.lat_ts));
  25. }
  26. }
  27. else {
  28. if (!this.k0) {
  29. if (this.k) {
  30. this.k0 = this.k;
  31. }
  32. else {
  33. this.k0 = 1;
  34. }
  35. }
  36. }
  37. };
  38. /* Mercator forward equations--mapping lat,long to x,y
  39. --------------------------------------------------*/
  40. exports.forward = function(p) {
  41. var lon = p.x;
  42. var lat = p.y;
  43. // convert to radians
  44. if (lat * R2D > 90 && lat * R2D < -90 && lon * R2D > 180 && lon * R2D < -180) {
  45. return null;
  46. }
  47. var x, y;
  48. if (Math.abs(Math.abs(lat) - HALF_PI) <= EPSLN) {
  49. return null;
  50. }
  51. else {
  52. if (this.sphere) {
  53. x = this.x0 + this.a * this.k0 * adjust_lon(lon - this.long0);
  54. y = this.y0 + this.a * this.k0 * Math.log(Math.tan(FORTPI + 0.5 * lat));
  55. }
  56. else {
  57. var sinphi = Math.sin(lat);
  58. var ts = tsfnz(this.e, lat, sinphi);
  59. x = this.x0 + this.a * this.k0 * adjust_lon(lon - this.long0);
  60. y = this.y0 - this.a * this.k0 * Math.log(ts);
  61. }
  62. p.x = x;
  63. p.y = y;
  64. return p;
  65. }
  66. };
  67. /* Mercator inverse equations--mapping x,y to lat/long
  68. --------------------------------------------------*/
  69. exports.inverse = function(p) {
  70. var x = p.x - this.x0;
  71. var y = p.y - this.y0;
  72. var lon, lat;
  73. if (this.sphere) {
  74. lat = HALF_PI - 2 * Math.atan(Math.exp(-y / (this.a * this.k0)));
  75. }
  76. else {
  77. var ts = Math.exp(-y / (this.a * this.k0));
  78. lat = phi2z(this.e, ts);
  79. if (lat === -9999) {
  80. return null;
  81. }
  82. }
  83. lon = adjust_lon(this.long0 + x / (this.a * this.k0));
  84. p.x = lon;
  85. p.y = lat;
  86. return p;
  87. };
  88. exports.names = ["Mercator", "Popular Visualisation Pseudo Mercator", "Mercator_1SP", "Mercator_Auxiliary_Sphere", "merc"];