ImageryProvider.js 12 KB

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