GeographicProjection.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import Cartesian3 from './Cartesian3.js';
  2. import Cartographic from './Cartographic.js';
  3. import defaultValue from './defaultValue.js';
  4. import defined from './defined.js';
  5. import defineProperties from './defineProperties.js';
  6. import DeveloperError from './DeveloperError.js';
  7. import Ellipsoid from './Ellipsoid.js';
  8. /**
  9. * A simple map projection where longitude and latitude are linearly mapped to X and Y by multiplying
  10. * them by the {@link Ellipsoid#maximumRadius}. This projection
  11. * is commonly known as geographic, equirectangular, equidistant cylindrical, or plate carrée. It
  12. * is also known as EPSG:4326.
  13. *
  14. * @alias GeographicProjection
  15. * @constructor
  16. *
  17. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid.
  18. *
  19. * @see WebMercatorProjection
  20. */
  21. function GeographicProjection(ellipsoid) {
  22. this._ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  23. this._semimajorAxis = this._ellipsoid.maximumRadius;
  24. this._oneOverSemimajorAxis = 1.0 / this._semimajorAxis;
  25. }
  26. defineProperties(GeographicProjection.prototype, {
  27. /**
  28. * Gets the {@link Ellipsoid}.
  29. *
  30. * @memberof GeographicProjection.prototype
  31. *
  32. * @type {Ellipsoid}
  33. * @readonly
  34. */
  35. ellipsoid : {
  36. get : function() {
  37. return this._ellipsoid;
  38. }
  39. }
  40. });
  41. /**
  42. * Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters.
  43. * X and Y are the longitude and latitude, respectively, multiplied by the maximum radius of the
  44. * ellipsoid. Z is the unmodified height.
  45. *
  46. * @param {Cartographic} cartographic The coordinates to project.
  47. * @param {Cartesian3} [result] An instance into which to copy the result. If this parameter is
  48. * undefined, a new instance is created and returned.
  49. * @returns {Cartesian3} The projected coordinates. If the result parameter is not undefined, the
  50. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  51. * created and returned.
  52. */
  53. GeographicProjection.prototype.project = function(cartographic, result) {
  54. // Actually this is the special case of equidistant cylindrical called the plate carree
  55. var semimajorAxis = this._semimajorAxis;
  56. var x = cartographic.longitude * semimajorAxis;
  57. var y = cartographic.latitude * semimajorAxis;
  58. var z = cartographic.height;
  59. if (!defined(result)) {
  60. return new Cartesian3(x, y, z);
  61. }
  62. result.x = x;
  63. result.y = y;
  64. result.z = z;
  65. return result;
  66. };
  67. /**
  68. * Unprojects a set of projected {@link Cartesian3} coordinates, in meters, to {@link Cartographic}
  69. * coordinates, in radians. Longitude and Latitude are the X and Y coordinates, respectively,
  70. * divided by the maximum radius of the ellipsoid. Height is the unmodified Z coordinate.
  71. *
  72. * @param {Cartesian3} cartesian The Cartesian position to unproject with height (z) in meters.
  73. * @param {Cartographic} [result] An instance into which to copy the result. If this parameter is
  74. * undefined, a new instance is created and returned.
  75. * @returns {Cartographic} The unprojected coordinates. If the result parameter is not undefined, the
  76. * coordinates are copied there and that instance is returned. Otherwise, a new instance is
  77. * created and returned.
  78. */
  79. GeographicProjection.prototype.unproject = function(cartesian, result) {
  80. //>>includeStart('debug', pragmas.debug);
  81. if (!defined(cartesian)) {
  82. throw new DeveloperError('cartesian is required');
  83. }
  84. //>>includeEnd('debug');
  85. var oneOverEarthSemimajorAxis = this._oneOverSemimajorAxis;
  86. var longitude = cartesian.x * oneOverEarthSemimajorAxis;
  87. var latitude = cartesian.y * oneOverEarthSemimajorAxis;
  88. var height = cartesian.z;
  89. if (!defined(result)) {
  90. return new Cartographic(longitude, latitude, height);
  91. }
  92. result.longitude = longitude;
  93. result.latitude = latitude;
  94. result.height = height;
  95. return result;
  96. };
  97. export default GeographicProjection;