sampleTerrain.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import when from '../ThirdParty/when.js';
  2. import Check from './Check.js';
  3. /**
  4. * Initiates a terrain height query for an array of {@link Cartographic} positions by
  5. * requesting tiles from a terrain provider, sampling, and interpolating. The interpolation
  6. * matches the triangles used to render the terrain at the specified level. The query
  7. * happens asynchronously, so this function returns a promise that is resolved when
  8. * the query completes. Each point height is modified in place. If a height can not be
  9. * determined because no terrain data is available for the specified level at that location,
  10. * or another error occurs, the height is set to undefined. As is typical of the
  11. * {@link Cartographic} type, the supplied height is a height above the reference ellipsoid
  12. * (such as {@link Ellipsoid.WGS84}) rather than an altitude above mean sea level. In other
  13. * words, it will not necessarily be 0.0 if sampled in the ocean. This function needs the
  14. * terrain level of detail as input, if you need to get the altitude of the terrain as precisely
  15. * as possible (i.e. with maximum level of detail) use {@link sampleTerrainMostDetailed}.
  16. *
  17. * @exports sampleTerrain
  18. *
  19. * @param {TerrainProvider} terrainProvider The terrain provider from which to query heights.
  20. * @param {Number} level The terrain level-of-detail from which to query terrain heights.
  21. * @param {Cartographic[]} positions The positions to update with terrain heights.
  22. * @returns {Promise.<Cartographic[]>} A promise that resolves to the provided list of positions when terrain the query has completed.
  23. *
  24. * @see sampleTerrainMostDetailed
  25. *
  26. * @example
  27. * // Query the terrain height of two Cartographic positions
  28. * var terrainProvider = Cesium.createWorldTerrain();
  29. * var positions = [
  30. * Cesium.Cartographic.fromDegrees(86.925145, 27.988257),
  31. * Cesium.Cartographic.fromDegrees(87.0, 28.0)
  32. * ];
  33. * var promise = Cesium.sampleTerrain(terrainProvider, 11, positions);
  34. * Cesium.when(promise, function(updatedPositions) {
  35. * // positions[0].height and positions[1].height have been updated.
  36. * // updatedPositions is just a reference to positions.
  37. * });
  38. */
  39. function sampleTerrain(terrainProvider, level, positions) {
  40. //>>includeStart('debug', pragmas.debug);
  41. Check.typeOf.object('terrainProvider', terrainProvider);
  42. Check.typeOf.number('level', level);
  43. Check.defined('positions', positions);
  44. //>>includeEnd('debug');
  45. return terrainProvider.readyPromise.then(function() { return doSampling(terrainProvider, level, positions); });
  46. }
  47. function doSampling(terrainProvider, level, positions) {
  48. var tilingScheme = terrainProvider.tilingScheme;
  49. var i;
  50. // Sort points into a set of tiles
  51. var tileRequests = []; // Result will be an Array as it's easier to work with
  52. var tileRequestSet = {}; // A unique set
  53. for (i = 0; i < positions.length; ++i) {
  54. var xy = tilingScheme.positionToTileXY(positions[i], level);
  55. var key = xy.toString();
  56. if (!tileRequestSet.hasOwnProperty(key)) {
  57. // When tile is requested for the first time
  58. var value = {
  59. x : xy.x,
  60. y : xy.y,
  61. level : level,
  62. tilingScheme : tilingScheme,
  63. terrainProvider : terrainProvider,
  64. positions : []
  65. };
  66. tileRequestSet[key] = value;
  67. tileRequests.push(value);
  68. }
  69. // Now append to array of points for the tile
  70. tileRequestSet[key].positions.push(positions[i]);
  71. }
  72. // Send request for each required tile
  73. var tilePromises = [];
  74. for (i = 0; i < tileRequests.length; ++i) {
  75. var tileRequest = tileRequests[i];
  76. var requestPromise = tileRequest.terrainProvider.requestTileGeometry(tileRequest.x, tileRequest.y, tileRequest.level);
  77. var tilePromise = requestPromise
  78. .then(createInterpolateFunction(tileRequest))
  79. .otherwise(createMarkFailedFunction(tileRequest));
  80. tilePromises.push(tilePromise);
  81. }
  82. return when.all(tilePromises, function() {
  83. return positions;
  84. });
  85. }
  86. function createInterpolateFunction(tileRequest) {
  87. var tilePositions = tileRequest.positions;
  88. var rectangle = tileRequest.tilingScheme.tileXYToRectangle(tileRequest.x, tileRequest.y, tileRequest.level);
  89. return function(terrainData) {
  90. for (var i = 0; i < tilePositions.length; ++i) {
  91. var position = tilePositions[i];
  92. position.height = terrainData.interpolateHeight(rectangle, position.longitude, position.latitude);
  93. }
  94. };
  95. }
  96. function createMarkFailedFunction(tileRequest) {
  97. var tilePositions = tileRequest.positions;
  98. return function() {
  99. for (var i = 0; i < tilePositions.length; ++i) {
  100. var position = tilePositions[i];
  101. position.height = undefined;
  102. }
  103. };
  104. }
  105. export default sampleTerrain;