ImageryProvider.js 13 KB

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