WebMercatorProjection-80c70558.js 7.1 KB

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