WebMercatorProjection-f88d3d05.js 5.6 KB

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