merc.js 2.3 KB

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