SingleTileImageryProvider.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. import Credit from "../Core/Credit.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defer from "../Core/defer.js";
  4. import defined from "../Core/defined.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import Event from "../Core/Event.js";
  7. import GeographicTilingScheme from "../Core/GeographicTilingScheme.js";
  8. import Rectangle from "../Core/Rectangle.js";
  9. import Resource from "../Core/Resource.js";
  10. import RuntimeError from "../Core/RuntimeError.js";
  11. import TileProviderError from "../Core/TileProviderError.js";
  12. import ImageryProvider from "./ImageryProvider.js";
  13. /**
  14. * @typedef {Object} SingleTileImageryProvider.ConstructorOptions
  15. *
  16. * Initialization options for the SingleTileImageryProvider constructor
  17. *
  18. * @property {Resource|String} url The url for the tile.
  19. * @property {Rectangle} [rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
  20. * @property {Credit|String} [credit] A credit for the data source, which is displayed on the canvas.
  21. * @property {Ellipsoid} [ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  22. */
  23. /**
  24. * Provides a single, top-level imagery tile. The single image is assumed to use a
  25. * {@link GeographicTilingScheme}.
  26. *
  27. * @alias SingleTileImageryProvider
  28. * @constructor
  29. *
  30. * @param {SingleTileImageryProvider.ConstructorOptions} options Object describing initialization options
  31. *
  32. * @see ArcGisMapServerImageryProvider
  33. * @see BingMapsImageryProvider
  34. * @see GoogleEarthEnterpriseMapsProvider
  35. * @see OpenStreetMapImageryProvider
  36. * @see TileMapServiceImageryProvider
  37. * @see WebMapServiceImageryProvider
  38. * @see WebMapTileServiceImageryProvider
  39. * @see UrlTemplateImageryProvider
  40. */
  41. function SingleTileImageryProvider(options) {
  42. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  43. //>>includeStart('debug', pragmas.debug);
  44. if (!defined(options.url)) {
  45. throw new DeveloperError("options.url is required.");
  46. }
  47. //>>includeEnd('debug');
  48. /**
  49. * The default alpha blending value of this provider, with 0.0 representing fully transparent and
  50. * 1.0 representing fully opaque.
  51. *
  52. * @type {Number|undefined}
  53. * @default undefined
  54. */
  55. this.defaultAlpha = undefined;
  56. /**
  57. * The default alpha blending value on the night side of the globe of this provider, with 0.0 representing fully transparent and
  58. * 1.0 representing fully opaque.
  59. *
  60. * @type {Number|undefined}
  61. * @default undefined
  62. */
  63. this.defaultNightAlpha = undefined;
  64. /**
  65. * The default alpha blending value on the day side of the globe of this provider, with 0.0 representing fully transparent and
  66. * 1.0 representing fully opaque.
  67. *
  68. * @type {Number|undefined}
  69. * @default undefined
  70. */
  71. this.defaultDayAlpha = undefined;
  72. /**
  73. * The default brightness of this provider. 1.0 uses the unmodified imagery color. Less than 1.0
  74. * makes the imagery darker while greater than 1.0 makes it brighter.
  75. *
  76. * @type {Number|undefined}
  77. * @default undefined
  78. */
  79. this.defaultBrightness = undefined;
  80. /**
  81. * The default contrast of this provider. 1.0 uses the unmodified imagery color. Less than 1.0 reduces
  82. * the contrast while greater than 1.0 increases it.
  83. *
  84. * @type {Number|undefined}
  85. * @default undefined
  86. */
  87. this.defaultContrast = undefined;
  88. /**
  89. * The default hue of this provider in radians. 0.0 uses the unmodified imagery color.
  90. *
  91. * @type {Number|undefined}
  92. * @default undefined
  93. */
  94. this.defaultHue = undefined;
  95. /**
  96. * The default saturation of this provider. 1.0 uses the unmodified imagery color. Less than 1.0 reduces the
  97. * saturation while greater than 1.0 increases it.
  98. *
  99. * @type {Number|undefined}
  100. * @default undefined
  101. */
  102. this.defaultSaturation = undefined;
  103. /**
  104. * The default gamma correction to apply to this provider. 1.0 uses the unmodified imagery color.
  105. *
  106. * @type {Number|undefined}
  107. * @default undefined
  108. */
  109. this.defaultGamma = undefined;
  110. /**
  111. * The default texture minification filter to apply to this provider.
  112. *
  113. * @type {TextureMinificationFilter}
  114. * @default undefined
  115. */
  116. this.defaultMinificationFilter = undefined;
  117. /**
  118. * The default texture magnification filter to apply to this provider.
  119. *
  120. * @type {TextureMagnificationFilter}
  121. * @default undefined
  122. */
  123. this.defaultMagnificationFilter = undefined;
  124. const resource = Resource.createIfNeeded(options.url);
  125. const rectangle = defaultValue(options.rectangle, Rectangle.MAX_VALUE);
  126. const tilingScheme = new GeographicTilingScheme({
  127. rectangle: rectangle,
  128. numberOfLevelZeroTilesX: 1,
  129. numberOfLevelZeroTilesY: 1,
  130. ellipsoid: options.ellipsoid,
  131. });
  132. this._tilingScheme = tilingScheme;
  133. this._resource = resource;
  134. this._image = undefined;
  135. this._texture = undefined;
  136. this._tileWidth = 0;
  137. this._tileHeight = 0;
  138. this._errorEvent = new Event();
  139. this._ready = false;
  140. this._readyPromise = defer();
  141. let credit = options.credit;
  142. if (typeof credit === "string") {
  143. credit = new Credit(credit);
  144. }
  145. this._credit = credit;
  146. const that = this;
  147. let error;
  148. function success(image) {
  149. that._image = image;
  150. that._tileWidth = image.width;
  151. that._tileHeight = image.height;
  152. that._ready = true;
  153. that._readyPromise.resolve(true);
  154. TileProviderError.handleSuccess(that._errorEvent);
  155. }
  156. function failure(e) {
  157. const message = `Failed to load image ${resource.url}.`;
  158. error = TileProviderError.handleError(
  159. error,
  160. that,
  161. that._errorEvent,
  162. message,
  163. 0,
  164. 0,
  165. 0,
  166. doRequest,
  167. e
  168. );
  169. if (!error.retry) {
  170. that._readyPromise.reject(new RuntimeError(message));
  171. }
  172. }
  173. function doRequest() {
  174. ImageryProvider.loadImage(null, resource).then(success).catch(failure);
  175. }
  176. doRequest();
  177. }
  178. Object.defineProperties(SingleTileImageryProvider.prototype, {
  179. /**
  180. * Gets the URL of the single, top-level imagery tile.
  181. * @memberof SingleTileImageryProvider.prototype
  182. * @type {String}
  183. * @readonly
  184. */
  185. url: {
  186. get: function () {
  187. return this._resource.url;
  188. },
  189. },
  190. /**
  191. * Gets the proxy used by this provider.
  192. * @memberof SingleTileImageryProvider.prototype
  193. * @type {Proxy}
  194. * @readonly
  195. */
  196. proxy: {
  197. get: function () {
  198. return this._resource.proxy;
  199. },
  200. },
  201. /**
  202. * Gets the width of each tile, in pixels. This function should
  203. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  204. * @memberof SingleTileImageryProvider.prototype
  205. * @type {Number}
  206. * @readonly
  207. */
  208. tileWidth: {
  209. get: function () {
  210. //>>includeStart('debug', pragmas.debug);
  211. if (!this._ready) {
  212. throw new DeveloperError(
  213. "tileWidth must not be called before the imagery provider is ready."
  214. );
  215. }
  216. //>>includeEnd('debug');
  217. return this._tileWidth;
  218. },
  219. },
  220. /**
  221. * Gets the height of each tile, in pixels. This function should
  222. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  223. * @memberof SingleTileImageryProvider.prototype
  224. * @type {Number}
  225. * @readonly
  226. */
  227. tileHeight: {
  228. get: function () {
  229. //>>includeStart('debug', pragmas.debug);
  230. if (!this._ready) {
  231. throw new DeveloperError(
  232. "tileHeight must not be called before the imagery provider is ready."
  233. );
  234. }
  235. //>>includeEnd('debug');
  236. return this._tileHeight;
  237. },
  238. },
  239. /**
  240. * Gets the maximum level-of-detail that can be requested. This function should
  241. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  242. * @memberof SingleTileImageryProvider.prototype
  243. * @type {Number|undefined}
  244. * @readonly
  245. */
  246. maximumLevel: {
  247. get: function () {
  248. //>>includeStart('debug', pragmas.debug);
  249. if (!this._ready) {
  250. throw new DeveloperError(
  251. "maximumLevel must not be called before the imagery provider is ready."
  252. );
  253. }
  254. //>>includeEnd('debug');
  255. return 0;
  256. },
  257. },
  258. /**
  259. * Gets the minimum level-of-detail that can be requested. This function should
  260. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  261. * @memberof SingleTileImageryProvider.prototype
  262. * @type {Number}
  263. * @readonly
  264. */
  265. minimumLevel: {
  266. get: function () {
  267. //>>includeStart('debug', pragmas.debug);
  268. if (!this._ready) {
  269. throw new DeveloperError(
  270. "minimumLevel must not be called before the imagery provider is ready."
  271. );
  272. }
  273. //>>includeEnd('debug');
  274. return 0;
  275. },
  276. },
  277. /**
  278. * Gets the tiling scheme used by this provider. This function should
  279. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  280. * @memberof SingleTileImageryProvider.prototype
  281. * @type {TilingScheme}
  282. * @readonly
  283. */
  284. tilingScheme: {
  285. get: function () {
  286. //>>includeStart('debug', pragmas.debug);
  287. if (!this._ready) {
  288. throw new DeveloperError(
  289. "tilingScheme must not be called before the imagery provider is ready."
  290. );
  291. }
  292. //>>includeEnd('debug');
  293. return this._tilingScheme;
  294. },
  295. },
  296. /**
  297. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  298. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  299. * @memberof SingleTileImageryProvider.prototype
  300. * @type {Rectangle}
  301. * @readonly
  302. */
  303. rectangle: {
  304. get: function () {
  305. return this._tilingScheme.rectangle;
  306. },
  307. },
  308. /**
  309. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  310. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  311. * returns undefined, no tiles are filtered. This function should
  312. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  313. * @memberof SingleTileImageryProvider.prototype
  314. * @type {TileDiscardPolicy}
  315. * @readonly
  316. */
  317. tileDiscardPolicy: {
  318. get: function () {
  319. //>>includeStart('debug', pragmas.debug);
  320. if (!this._ready) {
  321. throw new DeveloperError(
  322. "tileDiscardPolicy must not be called before the imagery provider is ready."
  323. );
  324. }
  325. //>>includeEnd('debug');
  326. return undefined;
  327. },
  328. },
  329. /**
  330. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  331. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  332. * are passed an instance of {@link TileProviderError}.
  333. * @memberof SingleTileImageryProvider.prototype
  334. * @type {Event}
  335. * @readonly
  336. */
  337. errorEvent: {
  338. get: function () {
  339. return this._errorEvent;
  340. },
  341. },
  342. /**
  343. * Gets a value indicating whether or not the provider is ready for use.
  344. * @memberof SingleTileImageryProvider.prototype
  345. * @type {Boolean}
  346. * @readonly
  347. */
  348. ready: {
  349. get: function () {
  350. return this._ready;
  351. },
  352. },
  353. /**
  354. * Gets a promise that resolves to true when the provider is ready for use.
  355. * @memberof SingleTileImageryProvider.prototype
  356. * @type {Promise.<Boolean>}
  357. * @readonly
  358. */
  359. readyPromise: {
  360. get: function () {
  361. return this._readyPromise.promise;
  362. },
  363. },
  364. /**
  365. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  366. * the source of the imagery. This function should not be called before {@link SingleTileImageryProvider#ready} returns true.
  367. * @memberof SingleTileImageryProvider.prototype
  368. * @type {Credit}
  369. * @readonly
  370. */
  371. credit: {
  372. get: function () {
  373. return this._credit;
  374. },
  375. },
  376. /**
  377. * Gets a value indicating whether or not the images provided by this imagery provider
  378. * include an alpha channel. If this property is false, an alpha channel, if present, will
  379. * be ignored. If this property is true, any images without an alpha channel will be treated
  380. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  381. * and texture upload time are reduced.
  382. * @memberof SingleTileImageryProvider.prototype
  383. * @type {Boolean}
  384. * @readonly
  385. */
  386. hasAlphaChannel: {
  387. get: function () {
  388. return true;
  389. },
  390. },
  391. });
  392. /**
  393. * Gets the credits to be displayed when a given tile is displayed.
  394. *
  395. * @param {Number} x The tile X coordinate.
  396. * @param {Number} y The tile Y coordinate.
  397. * @param {Number} level The tile level;
  398. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  399. *
  400. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  401. */
  402. SingleTileImageryProvider.prototype.getTileCredits = function (x, y, level) {
  403. return undefined;
  404. };
  405. /**
  406. * Requests the image for a given tile. This function should
  407. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  408. *
  409. * @param {Number} x The tile X coordinate.
  410. * @param {Number} y The tile Y coordinate.
  411. * @param {Number} level The tile level.
  412. * @param {Request} [request] The request object. Intended for internal use only.
  413. * @returns {Promise.<ImageryTypes>|undefined} The resolved image
  414. *
  415. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  416. */
  417. SingleTileImageryProvider.prototype.requestImage = function (
  418. x,
  419. y,
  420. level,
  421. request
  422. ) {
  423. //>>includeStart('debug', pragmas.debug);
  424. if (!this._ready) {
  425. throw new DeveloperError(
  426. "requestImage must not be called before the imagery provider is ready."
  427. );
  428. }
  429. //>>includeEnd('debug');
  430. if (!defined(this._image)) {
  431. return;
  432. }
  433. return Promise.resolve(this._image);
  434. };
  435. /**
  436. * Picking features is not currently supported by this imagery provider, so this function simply returns
  437. * undefined.
  438. *
  439. * @param {Number} x The tile X coordinate.
  440. * @param {Number} y The tile Y coordinate.
  441. * @param {Number} level The tile level.
  442. * @param {Number} longitude The longitude at which to pick features.
  443. * @param {Number} latitude The latitude at which to pick features.
  444. * @return {undefined} Undefined since picking is not supported.
  445. */
  446. SingleTileImageryProvider.prototype.pickFeatures = function (
  447. x,
  448. y,
  449. level,
  450. longitude,
  451. latitude
  452. ) {
  453. return undefined;
  454. };
  455. export default SingleTileImageryProvider;