ImageryProvider.js 13 KB

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