WebMercatorProjection.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartographic from './Cartographic.js';
  3. import defaultValue from './defaultValue.js';
  4. import defined from './defined.js';
  5. import defineProperties from './defineProperties.js';
  6. import DeveloperError from './DeveloperError.js';
  7. import Ellipsoid from './Ellipsoid.js';
  8. import CesiumMath from './Math.js';
  9. /**
  10. * The map projection used by Google Maps, Bing Maps, and most of ArcGIS Online, EPSG:3857. This
  11. * projection use longitude and latitude expressed with the WGS84 and transforms them to Mercator using
  12. * the spherical (rather than ellipsoidal) equations.
  13. *
  14. * @alias WebMercatorProjection
  15. * @constructor
  16. *
  17. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  18. *
  19. * @see GeographicProjection
  20. */
  21. function WebMercatorProjection(ellipsoid) {
  22. this._ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  23. this._semimajorAxis = this._ellipsoid.maximumRadius;
  24. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  25. }
  26. defineProperties(WebMercatorProjection.prototype, {
  27. /**
  28. * Gets the {@link Ellipsoid}.
  29. *
  30. * @memberof WebMercatorProjection.prototype
  31. *
  32. * @type {Ellipsoid}
  33. * @readonly
  34. */
  35. ellipsoid : {
  36. get : function() {
  37. return this._ellipsoid;
  38. }
  39. }
  40. });
  41. /**
  42. * Converts a Mercator angle, in the range -PI to PI, to a geodetic latitude
  43. * in the range -PI/2 to PI/2.
  44. *
  45. * @param {Number} mercatorAngle The angle to convert.
  46. * @returns {Number} The geodetic latitude in radians.
  47. */
  48. WebMercatorProjection.mercatorAngleToGeodeticLatitude = function(mercatorAngle) {
  49. return CesiumMath.PI_OVER_TWO - (2.0 * Math.atan(Math.exp(-mercatorAngle)));
  50. };
  51. /**
  52. * Converts a geodetic latitude in radians, in the range -PI/2 to PI/2, to a Mercator
  53. * angle in the range -PI to PI.
  54. *
  55. * @param {Number} latitude The geodetic latitude in radians.
  56. * @returns {Number} The Mercator angle.
  57. */
  58. WebMercatorProjection.geodeticLatitudeToMercatorAngle = function(latitude) {
  59. // Clamp the latitude coordinate to the valid Mercator bounds.
  60. if (latitude > WebMercatorProjection.MaximumLatitude) {
  61. latitude = WebMercatorProjection.MaximumLatitude;
  62. } else if (latitude < -WebMercatorProjection.MaximumLatitude) {
  63. latitude = -WebMercatorProjection.MaximumLatitude;
  64. }
  65. var sinLatitude = Math.sin(latitude);
  66. return 0.5 * Math.log((1.0 + sinLatitude) / (1.0 - sinLatitude));
  67. };
  68. /**
  69. * The maximum latitude (both North and South) supported by a Web Mercator
  70. * (EPSG:3857) projection. Technically, the Mercator projection is defined
  71. * for any latitude up to (but not including) 90 degrees, but it makes sense
  72. * to cut it off sooner because it grows exponentially with increasing latitude.
  73. * The logic behind this particular cutoff value, which is the one used by
  74. * Google Maps, Bing Maps, and Esri, is that it makes the projection
  75. * square. That is, the rectangle is equal in the X and Y directions.
  76. *
  77. * The constant value is computed by calling:
  78. * WebMercatorProjection.mercatorAngleToGeodeticLatitude(Math.PI)
  79. *
  80. * @type {Number}
  81. */
  82. WebMercatorProjection.MaximumLatitude = WebMercatorProjection.mercatorAngleToGeodeticLatitude(Math.PI);
  83. /**
  84. * Converts geodetic ellipsoid coordinates, in radians, to the equivalent Web Mercator
  85. * X, Y, Z coordinates expressed in meters and returned in a {@link Cartesian3}. The height
  86. * is copied unmodified to the Z coordinate.
  87. *
  88. * @param {Cartographic} cartographic The cartographic coordinates in radians.
  89. * @param {Cartesian3} [result] The instance to which to copy the result, or undefined if a
  90. * new instance should be created.
  91. * @returns {Cartesian3} The equivalent web mercator X, Y, Z coordinates, in meters.
  92. */
  93. WebMercatorProjection.prototype.project = function(cartographic, result) {
  94. var semimajorAxis = this._semimajorAxis;
  95. var x = cartographic.longitude * semimajorAxis;
  96. var y = WebMercatorProjection.geodeticLatitudeToMercatorAngle(cartographic.latitude) * semimajorAxis;
  97. var z = cartographic.height;
  98. if (!defined(result)) {
  99. return new Cartesian3(x, y, z);
  100. }
  101. result.x = x;
  102. result.y = y;
  103. result.z = z;
  104. return result;
  105. };
  106. /**
  107. * Converts Web Mercator X, Y coordinates, expressed in meters, to a {@link Cartographic}
  108. * containing geodetic ellipsoid coordinates. The Z coordinate is copied unmodified to the
  109. * height.
  110. *
  111. * @param {Cartesian3} cartesian The web mercator Cartesian position to unrproject with height (z) in meters.
  112. * @param {Cartographic} [result] The instance to which to copy the result, or undefined if a
  113. * new instance should be created.
  114. * @returns {Cartographic} The equivalent cartographic coordinates.
  115. */
  116. WebMercatorProjection.prototype.unproject = function(cartesian, result) {
  117. //>>includeStart('debug', pragmas.debug);
  118. if (!defined(cartesian)) {
  119. throw new DeveloperError('cartesian is required');
  120. }
  121. //>>includeEnd('debug');
  122. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  123. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  124. var latitude = WebMercatorProjection.mercatorAngleToGeodeticLatitude(cartesian.y * oneOverEarthSemimajorAxis);
  125. var height = cartesian.z;
  126. if (!defined(result)) {
  127. return new Cartographic(longitude, latitude, height);
  128. }
  129. result.longitude = longitude;
  130. result.latitude = latitude;
  131. result.height = height;
  132. return result;
  133. };
  134. export default WebMercatorProjection;