GeographicProjection.js 3.8 KB

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