WebMercatorProjection-13ed1a6e.js 5.5 KB

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