WebMercatorProjection.js 5.3 KB

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