ImageryProvider.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import loadKTX2 from "../Core/loadKTX2.js";
  5. import Resource from "../Core/Resource.js";
  6. /**
  7. * @typedef {HTMLImageElement|HTMLCanvasElement|ImageBitmap} ImageryTypes
  8. *
  9. * The format in which {@link ImageryProvider} methods return an image may
  10. * vary by provider, configuration, or server settings. Most common are
  11. * <code>HTMLImageElement</code>, <code>HTMLCanvasElement</code>, or on supported
  12. * browsers, <code>ImageBitmap</code>.
  13. *
  14. * See the documentation for each ImageryProvider class for more information about how they return images.
  15. */
  16. /**
  17. * Provides imagery to be displayed on the surface of an ellipsoid. This type describes an
  18. * interface and is not intended to be instantiated directly.
  19. *
  20. * @alias ImageryProvider
  21. * @constructor
  22. * @abstract
  23. *
  24. * @see ArcGisMapServerImageryProvider
  25. * @see BingMapsImageryProvider
  26. * @see OpenStreetMapImageryProvider
  27. * @see TileMapServiceImageryProvider
  28. * @see GoogleEarthEnterpriseImageryProvider
  29. * @see GoogleEarthEnterpriseMapsProvider
  30. * @see GridImageryProvider
  31. * @see IonImageryProvider
  32. * @see MapboxImageryProvider
  33. * @see MapboxStyleImageryProvider
  34. * @see SingleTileImageryProvider
  35. * @see TileCoordinatesImageryProvider
  36. * @see UrlTemplateImageryProvider
  37. * @see WebMapServiceImageryProvider
  38. * @see WebMapTileServiceImageryProvider
  39. *
  40. * @demo {@link https://sandcastle.cesium.com/index.html?src=Imagery%20Layers.html|Cesium Sandcastle Imagery Layers Demo}
  41. * @demo {@link https://sandcastle.cesium.com/index.html?src=Imagery%20Layers%20Manipulation.html|Cesium Sandcastle Imagery Manipulation Demo}
  42. */
  43. function ImageryProvider() {
  44. DeveloperError.throwInstantiationError();
  45. }
  46. Object.defineProperties(ImageryProvider.prototype, {
  47. /**
  48. * Gets a value indicating whether or not the provider is ready for use.
  49. * @memberof ImageryProvider.prototype
  50. * @type {boolean}
  51. * @readonly
  52. * @deprecated
  53. */
  54. ready: {
  55. get: DeveloperError.throwInstantiationError,
  56. },
  57. /**
  58. * Gets a promise that resolves to true when the provider is ready for use.
  59. * @memberof ImageryProvider.prototype
  60. * @type {Promise<boolean>}
  61. * @readonly
  62. * @deprecated
  63. */
  64. readyPromise: {
  65. get: DeveloperError.throwInstantiationError,
  66. },
  67. /**
  68. * Gets the rectangle, in radians, of the imagery provided by the instance.
  69. * @memberof ImageryProvider.prototype
  70. * @type {Rectangle}
  71. * @readonly
  72. */
  73. rectangle: {
  74. get: DeveloperError.throwInstantiationError,
  75. },
  76. /**
  77. * Gets the width of each tile, in pixels.
  78. * @memberof ImageryProvider.prototype
  79. * @type {number}
  80. * @readonly
  81. */
  82. tileWidth: {
  83. get: DeveloperError.throwInstantiationError,
  84. },
  85. /**
  86. * Gets the height of each tile, in pixels.
  87. * @memberof ImageryProvider.prototype
  88. * @type {number}
  89. * @readonly
  90. */
  91. tileHeight: {
  92. get: DeveloperError.throwInstantiationError,
  93. },
  94. /**
  95. * Gets the maximum level-of-detail that can be requested.
  96. * @memberof ImageryProvider.prototype
  97. * @type {number|undefined}
  98. * @readonly
  99. */
  100. maximumLevel: {
  101. get: DeveloperError.throwInstantiationError,
  102. },
  103. /**
  104. * Gets the minimum level-of-detail that can be requested. Generally,
  105. * a minimum level should only be used when the rectangle of the imagery is small
  106. * enough that the number of tiles at the minimum level is small. An imagery
  107. * provider with more than a few tiles at the minimum level will lead to
  108. * rendering problems.
  109. * @memberof ImageryProvider.prototype
  110. * @type {number}
  111. * @readonly
  112. */
  113. minimumLevel: {
  114. get: DeveloperError.throwInstantiationError,
  115. },
  116. /**
  117. * Gets the tiling scheme used by the provider.
  118. * @memberof ImageryProvider.prototype
  119. * @type {TilingScheme}
  120. * @readonly
  121. */
  122. tilingScheme: {
  123. get: DeveloperError.throwInstantiationError,
  124. },
  125. /**
  126. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  127. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  128. * returns undefined, no tiles are filtered.
  129. * @memberof ImageryProvider.prototype
  130. * @type {TileDiscardPolicy}
  131. * @readonly
  132. */
  133. tileDiscardPolicy: {
  134. get: DeveloperError.throwInstantiationError,
  135. },
  136. /**
  137. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  138. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  139. * are passed an instance of {@link TileProviderError}.
  140. * @memberof ImageryProvider.prototype
  141. * @type {Event}
  142. * @readonly
  143. */
  144. errorEvent: {
  145. get: DeveloperError.throwInstantiationError,
  146. },
  147. /**
  148. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  149. * the source of the imagery.
  150. * @memberof ImageryProvider.prototype
  151. * @type {Credit}
  152. * @readonly
  153. */
  154. credit: {
  155. get: DeveloperError.throwInstantiationError,
  156. },
  157. /**
  158. * Gets the proxy used by this provider.
  159. * @memberof ImageryProvider.prototype
  160. * @type {Proxy}
  161. * @readonly
  162. */
  163. proxy: {
  164. get: DeveloperError.throwInstantiationError,
  165. },
  166. /**
  167. * Gets a value indicating whether or not the images provided by this imagery provider
  168. * include an alpha channel. If this property is false, an alpha channel, if present, will
  169. * be ignored. If this property is true, any images without an alpha channel will be treated
  170. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  171. * and texture upload time are reduced.
  172. * @memberof ImageryProvider.prototype
  173. * @type {boolean}
  174. * @readonly
  175. */
  176. hasAlphaChannel: {
  177. get: DeveloperError.throwInstantiationError,
  178. },
  179. /**
  180. * The default alpha blending value of this provider, with 0.0 representing fully transparent and
  181. * 1.0 representing fully opaque.
  182. * @memberof ImageryProvider.prototype
  183. * @type {Number|undefined}
  184. * @deprecated
  185. */
  186. defaultAlpha: {
  187. get: DeveloperError.throwInstantiationError,
  188. set: DeveloperError.throwInstantiationError,
  189. },
  190. /**
  191. * The default alpha blending value on the night side of the globe of this provider, with 0.0 representing fully transparent and
  192. * 1.0 representing fully opaque.
  193. * @memberof ImageryProvider.prototype
  194. * @type {Number|undefined}
  195. * @deprecated
  196. */
  197. defaultNightAlpha: {
  198. get: DeveloperError.throwInstantiationError,
  199. set: DeveloperError.throwInstantiationError,
  200. },
  201. /**
  202. * The default alpha blending value on the day side of the globe of this provider, with 0.0 representing fully transparent and
  203. * 1.0 representing fully opaque.
  204. * @memberof ImageryProvider.prototype
  205. * @type {Number|undefined}
  206. * @deprecated
  207. */
  208. defaultDayAlpha: {
  209. get: DeveloperError.throwInstantiationError,
  210. set: DeveloperError.throwInstantiationError,
  211. },
  212. /**
  213. * The default brightness of this provider. 1.0 uses the unmodified imagery color. Less than 1.0
  214. * makes the imagery darker while greater than 1.0 makes it brighter.
  215. * @memberof ImageryProvider.prototype
  216. * @type {Number|undefined}
  217. * @deprecated
  218. */
  219. defaultBrightness: {
  220. get: DeveloperError.throwInstantiationError,
  221. set: DeveloperError.throwInstantiationError,
  222. },
  223. /**
  224. * The default contrast of this provider. 1.0 uses the unmodified imagery color. Less than 1.0 reduces
  225. * the contrast while greater than 1.0 increases it.
  226. * @memberof ImageryProvider.prototype
  227. * @type {Number|undefined}
  228. * @deprecated
  229. */
  230. defaultContrast: {
  231. get: DeveloperError.throwInstantiationError,
  232. set: DeveloperError.throwInstantiationError,
  233. },
  234. /**
  235. * The default hue of this provider in radians. 0.0 uses the unmodified imagery color.
  236. * @memberof ImageryProvider.prototype
  237. * @type {Number|undefined}
  238. * @deprecated
  239. */
  240. defaultHue: {
  241. get: DeveloperError.throwInstantiationError,
  242. set: DeveloperError.throwInstantiationError,
  243. },
  244. /**
  245. * The default saturation of this provider. 1.0 uses the unmodified imagery color. Less than 1.0 reduces the
  246. * saturation while greater than 1.0 increases it.
  247. * @memberof ImageryProvider.prototype
  248. * @type {Number|undefined}
  249. * @deprecated
  250. */
  251. defaultSaturation: {
  252. get: DeveloperError.throwInstantiationError,
  253. set: DeveloperError.throwInstantiationError,
  254. },
  255. /**
  256. * The default gamma correction to apply to this provider. 1.0 uses the unmodified imagery color.
  257. * @memberof ImageryProvider.prototype
  258. * @type {Number|undefined}
  259. * @deprecated
  260. */
  261. defaultGamma: {
  262. get: DeveloperError.throwInstantiationError,
  263. set: DeveloperError.throwInstantiationError,
  264. },
  265. /**
  266. * The default texture minification filter to apply to this provider.
  267. * @memberof ImageryProvider.prototype
  268. * @type {TextureMinificationFilter}
  269. * @deprecated
  270. */
  271. defaultMinificationFilter: {
  272. get: DeveloperError.throwInstantiationError,
  273. set: DeveloperError.throwInstantiationError,
  274. },
  275. /**
  276. * The default texture magnification filter to apply to this provider.
  277. * @memberof ImageryProvider.prototype
  278. * @type {TextureMagnificationFilter}
  279. * @deprecated
  280. */
  281. defaultMagnificationFilter: {
  282. get: DeveloperError.throwInstantiationError,
  283. set: DeveloperError.throwInstantiationError,
  284. },
  285. });
  286. /**
  287. * Gets the credits to be displayed when a given tile is displayed.
  288. *
  289. * @param {number} x The tile X coordinate.
  290. * @param {number} y The tile Y coordinate.
  291. * @param {number} level The tile level;
  292. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  293. */
  294. ImageryProvider.prototype.getTileCredits = function (x, y, level) {
  295. DeveloperError.throwInstantiationError();
  296. };
  297. /**
  298. * Requests the image for a given tile.
  299. *
  300. * @param {number} x The tile X coordinate.
  301. * @param {number} y The tile Y coordinate.
  302. * @param {number} level The tile level.
  303. * @param {Request} [request] The request object. Intended for internal use only.
  304. * @returns {Promise<ImageryTypes>|undefined} Returns a promise for the image that will resolve when the image is available, or
  305. * undefined if there are too many active requests to the server, and the request should be retried later.
  306. */
  307. ImageryProvider.prototype.requestImage = function (x, y, level, request) {
  308. DeveloperError.throwInstantiationError();
  309. };
  310. /**
  311. * Asynchronously determines what features, if any, are located at a given longitude and latitude within
  312. * a tile.
  313. * This function is optional, so it may not exist on all ImageryProviders.
  314. *
  315. * @function
  316. *
  317. * @param {number} x The tile X coordinate.
  318. * @param {number} y The tile Y coordinate.
  319. * @param {number} level The tile level.
  320. * @param {number} longitude The longitude at which to pick features.
  321. * @param {number} latitude The latitude at which to pick features.
  322. * @return {Promise<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  323. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  324. * instances. The array may be empty if no features are found at the given location.
  325. * It may also be undefined if picking is not supported.
  326. *
  327. */
  328. ImageryProvider.prototype.pickFeatures = function (
  329. x,
  330. y,
  331. level,
  332. longitude,
  333. latitude
  334. ) {
  335. DeveloperError.throwInstantiationError();
  336. };
  337. const ktx2Regex = /\.ktx2$/i;
  338. /**
  339. * Loads an image from a given URL. If the server referenced by the URL already has
  340. * too many requests pending, this function will instead return undefined, indicating
  341. * that the request should be retried later.
  342. *
  343. * @param {ImageryProvider} imageryProvider The imagery provider for the URL.
  344. * @param {Resource|string} url The URL of the image.
  345. * @returns {Promise<ImageryTypes|CompressedTextureBuffer>|undefined} A promise for the image that will resolve when the image is available, or
  346. * undefined if there are too many active requests to the server, and the request should be retried later.
  347. */
  348. ImageryProvider.loadImage = function (imageryProvider, url) {
  349. //>>includeStart('debug', pragmas.debug);
  350. Check.defined("url", url);
  351. //>>includeEnd('debug');
  352. const resource = Resource.createIfNeeded(url);
  353. if (ktx2Regex.test(resource.url)) {
  354. // Resolves with `CompressedTextureBuffer`
  355. return loadKTX2(resource);
  356. } else if (
  357. defined(imageryProvider) &&
  358. defined(imageryProvider.tileDiscardPolicy)
  359. ) {
  360. // Resolves with `HTMLImageElement` or `ImageBitmap`
  361. return resource.fetchImage({
  362. preferBlob: true,
  363. preferImageBitmap: true,
  364. flipY: true,
  365. });
  366. }
  367. return resource.fetchImage({
  368. preferImageBitmap: true,
  369. flipY: true,
  370. });
  371. };
  372. export default ImageryProvider;