WebMercatorProjection-f2dc467d.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './defined-26bd4a03', './Check-da037458', './defaultValue-f2e68450', './Math-fa6e45cb', './Cartesian2-2a723276', './defineProperties-6f7a50f2'], function (exports, defined, Check, defaultValue, _Math, Cartesian2, defineProperties) { '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, Cartesian2.Ellipsoid.WGS84);
  17. this._semimajorAxis = this._ellipsoid.maximumRadius;
  18. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  19. }
  20. defineProperties.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(mercatorAngle) {
  43. return _Math.CesiumMath.PI_OVER_TWO - (2.0 * Math.atan(Math.exp(-mercatorAngle)));
  44. };
  45. /**
  46. * Converts a geodetic latitude in radians, in the range -PI/2 to PI/2, to a Mercator
  47. * angle in the range -PI to PI.
  48. *
  49. * @param {Number} latitude The geodetic latitude in radians.
  50. * @returns {Number} The Mercator angle.
  51. */
  52. WebMercatorProjection.geodeticLatitudeToMercatorAngle = function(latitude) {
  53. // Clamp the latitude coordinate to the valid Mercator bounds.
  54. if (latitude > WebMercatorProjection.MaximumLatitude) {
  55. latitude = WebMercatorProjection.MaximumLatitude;
  56. } else if (latitude < -WebMercatorProjection.MaximumLatitude) {
  57. latitude = -WebMercatorProjection.MaximumLatitude;
  58. }
  59. var sinLatitude = Math.sin(latitude);
  60. return 0.5 * Math.log((1.0 + sinLatitude) / (1.0 - sinLatitude));
  61. };
  62. /**
  63. * The maximum latitude (both North and South) supported by a Web Mercator
  64. * (EPSG:3857) projection. Technically, the Mercator projection is defined
  65. * for any latitude up to (but not including) 90 degrees, but it makes sense
  66. * to cut it off sooner because it grows exponentially with increasing latitude.
  67. * The logic behind this particular cutoff value, which is the one used by
  68. * Google Maps, Bing Maps, and Esri, is that it makes the projection
  69. * square. That is, the rectangle is equal in the X and Y directions.
  70. *
  71. * The constant value is computed by calling:
  72. * WebMercatorProjection.mercatorAngleToGeodeticLatitude(Math.PI)
  73. *
  74. * @type {Number}
  75. */
  76. WebMercatorProjection.MaximumLatitude = WebMercatorProjection.mercatorAngleToGeodeticLatitude(Math.PI);
  77. /**
  78. * Converts geodetic ellipsoid coordinates, in radians, to the equivalent Web Mercator
  79. * X, Y, Z coordinates expressed in meters and returned in a {@link Cartesian3}. The height
  80. * is copied unmodified to the Z coordinate.
  81. *
  82. * @param {Cartographic} cartographic The cartographic coordinates in radians.
  83. * @param {Cartesian3} [result] The instance to which to copy the result, or undefined if a
  84. * new instance should be created.
  85. * @returns {Cartesian3} The equivalent web mercator X, Y, Z coordinates, in meters.
  86. */
  87. WebMercatorProjection.prototype.project = function(cartographic, result) {
  88. var semimajorAxis = this._semimajorAxis;
  89. var x = cartographic.longitude * semimajorAxis;
  90. var y = WebMercatorProjection.geodeticLatitudeToMercatorAngle(cartographic.latitude) * semimajorAxis;
  91. var z = cartographic.height;
  92. if (!defined.defined(result)) {
  93. return new Cartesian2.Cartesian3(x, y, z);
  94. }
  95. result.x = x;
  96. result.y = y;
  97. result.z = z;
  98. return result;
  99. };
  100. /**
  101. * Converts Web Mercator X, Y coordinates, expressed in meters, to a {@link Cartographic}
  102. * containing geodetic ellipsoid coordinates. The Z coordinate is copied unmodified to the
  103. * height.
  104. *
  105. * @param {Cartesian3} cartesian The web mercator Cartesian position to unrproject with height (z) in meters.
  106. * @param {Cartographic} [result] The instance to which to copy the result, or undefined if a
  107. * new instance should be created.
  108. * @returns {Cartographic} The equivalent cartographic coordinates.
  109. */
  110. WebMercatorProjection.prototype.unproject = function(cartesian, result) {
  111. //>>includeStart('debug', pragmas.debug);
  112. if (!defined.defined(cartesian)) {
  113. throw new Check.DeveloperError('cartesian is required');
  114. }
  115. //>>includeEnd('debug');
  116. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  117. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  118. var latitude = WebMercatorProjection.mercatorAngleToGeodeticLatitude(cartesian.y * oneOverEarthSemimajorAxis);
  119. var height = cartesian.z;
  120. if (!defined.defined(result)) {
  121. return new Cartesian2.Cartographic(longitude, latitude, height);
  122. }
  123. result.longitude = longitude;
  124. result.latitude = latitude;
  125. result.height = height;
  126. return result;
  127. };
  128. exports.WebMercatorProjection = WebMercatorProjection;
  129. });