WebMercatorProjection-3b121d41.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './Matrix2-9e1c22e2', './defaultValue-97284df2', './RuntimeError-4f8ec8a2', './ComponentDatatype-4eeb6d9b'], (function (exports, Matrix2, defaultValue, RuntimeError, ComponentDatatype) { 'use strict';
  26. /**
  27. * The map projection used by Google Maps, Bing Maps, and most of ArcGIS Online, EPSG:3857. This
  28. * projection use longitude and latitude expressed with the WGS84 and transforms them to Mercator using
  29. * the spherical (rather than ellipsoidal) equations.
  30. *
  31. * @alias WebMercatorProjection
  32. * @constructor
  33. *
  34. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  35. *
  36. * @see GeographicProjection
  37. */
  38. function WebMercatorProjection(ellipsoid) {
  39. this._ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix2.Ellipsoid.WGS84);
  40. this._semimajorAxis = this._ellipsoid.maximumRadius;
  41. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  42. }
  43. Object.defineProperties(WebMercatorProjection.prototype, {
  44. /**
  45. * Gets the {@link Ellipsoid}.
  46. *
  47. * @memberof WebMercatorProjection.prototype
  48. *
  49. * @type {Ellipsoid}
  50. * @readonly
  51. */
  52. ellipsoid: {
  53. get: function () {
  54. return this._ellipsoid;
  55. },
  56. },
  57. });
  58. /**
  59. * Converts a Mercator angle, in the range -PI to PI, to a geodetic latitude
  60. * in the range -PI/2 to PI/2.
  61. *
  62. * @param {Number} mercatorAngle The angle to convert.
  63. * @returns {Number} The geodetic latitude in radians.
  64. */
  65. WebMercatorProjection.mercatorAngleToGeodeticLatitude = function (
  66. mercatorAngle
  67. ) {
  68. return ComponentDatatype.CesiumMath.PI_OVER_TWO - 2.0 * Math.atan(Math.exp(-mercatorAngle));
  69. };
  70. /**
  71. * Converts a geodetic latitude in radians, in the range -PI/2 to PI/2, to a Mercator
  72. * angle in the range -PI to PI.
  73. *
  74. * @param {Number} latitude The geodetic latitude in radians.
  75. * @returns {Number} The Mercator angle.
  76. */
  77. WebMercatorProjection.geodeticLatitudeToMercatorAngle = function (latitude) {
  78. // Clamp the latitude coordinate to the valid Mercator bounds.
  79. if (latitude > WebMercatorProjection.MaximumLatitude) {
  80. latitude = WebMercatorProjection.MaximumLatitude;
  81. } else if (latitude < -WebMercatorProjection.MaximumLatitude) {
  82. latitude = -WebMercatorProjection.MaximumLatitude;
  83. }
  84. const sinLatitude = Math.sin(latitude);
  85. return 0.5 * Math.log((1.0 + sinLatitude) / (1.0 - sinLatitude));
  86. };
  87. /**
  88. * The maximum latitude (both North and South) supported by a Web Mercator
  89. * (EPSG:3857) projection. Technically, the Mercator projection is defined
  90. * for any latitude up to (but not including) 90 degrees, but it makes sense
  91. * to cut it off sooner because it grows exponentially with increasing latitude.
  92. * The logic behind this particular cutoff value, which is the one used by
  93. * Google Maps, Bing Maps, and Esri, is that it makes the projection
  94. * square. That is, the rectangle is equal in the X and Y directions.
  95. *
  96. * The constant value is computed by calling:
  97. * WebMercatorProjection.mercatorAngleToGeodeticLatitude(Math.PI)
  98. *
  99. * @type {Number}
  100. */
  101. WebMercatorProjection.MaximumLatitude = WebMercatorProjection.mercatorAngleToGeodeticLatitude(
  102. Math.PI
  103. );
  104. /**
  105. * Converts geodetic ellipsoid coordinates, in radians, to the equivalent Web Mercator
  106. * X, Y, Z coordinates expressed in meters and returned in a {@link Cartesian3}. The height
  107. * is copied unmodified to the Z coordinate.
  108. *
  109. * @param {Cartographic} cartographic The cartographic coordinates in radians.
  110. * @param {Cartesian3} [result] The instance to which to copy the result, or undefined if a
  111. * new instance should be created.
  112. * @returns {Cartesian3} The equivalent web mercator X, Y, Z coordinates, in meters.
  113. */
  114. WebMercatorProjection.prototype.project = function (cartographic, result) {
  115. const semimajorAxis = this._semimajorAxis;
  116. const x = cartographic.longitude * semimajorAxis;
  117. const y =
  118. WebMercatorProjection.geodeticLatitudeToMercatorAngle(
  119. cartographic.latitude
  120. ) * semimajorAxis;
  121. const z = cartographic.height;
  122. if (!defaultValue.defined(result)) {
  123. return new Matrix2.Cartesian3(x, y, z);
  124. }
  125. result.x = x;
  126. result.y = y;
  127. result.z = z;
  128. return result;
  129. };
  130. /**
  131. * Converts Web Mercator X, Y coordinates, expressed in meters, to a {@link Cartographic}
  132. * containing geodetic ellipsoid coordinates. The Z coordinate is copied unmodified to the
  133. * height.
  134. *
  135. * @param {Cartesian3} cartesian The web mercator Cartesian position to unrproject with height (z) in meters.
  136. * @param {Cartographic} [result] The instance to which to copy the result, or undefined if a
  137. * new instance should be created.
  138. * @returns {Cartographic} The equivalent cartographic coordinates.
  139. */
  140. WebMercatorProjection.prototype.unproject = function (cartesian, result) {
  141. //>>includeStart('debug', pragmas.debug);
  142. if (!defaultValue.defined(cartesian)) {
  143. throw new RuntimeError.DeveloperError("cartesian is required");
  144. }
  145. //>>includeEnd('debug');
  146. const oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  147. const longitude = cartesian.x * oneOverEarthSemimajorAxis;
  148. const latitude = WebMercatorProjection.mercatorAngleToGeodeticLatitude(
  149. cartesian.y * oneOverEarthSemimajorAxis
  150. );
  151. const height = cartesian.z;
  152. if (!defaultValue.defined(result)) {
  153. return new Matrix2.Cartographic(longitude, latitude, height);
  154. }
  155. result.longitude = longitude;
  156. result.latitude = latitude;
  157. result.height = height;
  158. return result;
  159. };
  160. exports.WebMercatorProjection = WebMercatorProjection;
  161. }));
  162. //# sourceMappingURL=WebMercatorProjection-3b121d41.js.map