SingleTileImageryProvider.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import Credit from '../Core/Credit.js';
  2. import defaultValue from '../Core/defaultValue.js';
  3. import defined from '../Core/defined.js';
  4. import defineProperties from '../Core/defineProperties.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 when from '../ThirdParty/when.js';
  13. import ImageryProvider from './ImageryProvider.js';
  14. /**
  15. * Provides a single, top-level imagery tile. The single image is assumed to use a
  16. * {@link GeographicTilingScheme}.
  17. *
  18. * @alias SingleTileImageryProvider
  19. * @constructor
  20. *
  21. * @param {Object} options Object with the following properties:
  22. * @param {Resource|String} options.url The url for the tile.
  23. * @param {Rectangle} [options.rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
  24. * @param {Credit|String} [options.credit] A credit for the data source, which is displayed on the canvas.
  25. * @param {Ellipsoid} [options.ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  26. *
  27. * @see ArcGisMapServerImageryProvider
  28. * @see BingMapsImageryProvider
  29. * @see GoogleEarthEnterpriseMapsProvider
  30. * @see OpenStreetMapImageryProvider
  31. * @see TileMapServiceImageryProvider
  32. * @see WebMapServiceImageryProvider
  33. * @see WebMapTileServiceImageryProvider
  34. * @see UrlTemplateImageryProvider
  35. */
  36. function SingleTileImageryProvider(options) {
  37. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  38. //>>includeStart('debug', pragmas.debug);
  39. if (!defined(options.url)) {
  40. throw new DeveloperError('options.url is required.');
  41. }
  42. //>>includeEnd('debug');
  43. var resource = Resource.createIfNeeded(options.url);
  44. var rectangle = defaultValue(options.rectangle, Rectangle.MAX_VALUE);
  45. var tilingScheme = new GeographicTilingScheme({
  46. rectangle : rectangle,
  47. numberOfLevelZeroTilesX : 1,
  48. numberOfLevelZeroTilesY : 1,
  49. ellipsoid : options.ellipsoid
  50. });
  51. this._tilingScheme = tilingScheme;
  52. this._resource = resource;
  53. this._image = undefined;
  54. this._texture = undefined;
  55. this._tileWidth = 0;
  56. this._tileHeight = 0;
  57. this._errorEvent = new Event();
  58. this._ready = false;
  59. this._readyPromise = when.defer();
  60. var credit = options.credit;
  61. if (typeof credit === 'string') {
  62. credit = new Credit(credit);
  63. }
  64. this._credit = credit;
  65. var that = this;
  66. var error;
  67. function success(image) {
  68. that._image = image;
  69. that._tileWidth = image.width;
  70. that._tileHeight = image.height;
  71. that._ready = true;
  72. that._readyPromise.resolve(true);
  73. TileProviderError.handleSuccess(that._errorEvent);
  74. }
  75. function failure(e) {
  76. var message = 'Failed to load image ' + resource.url + '.';
  77. error = TileProviderError.handleError(
  78. error,
  79. that,
  80. that._errorEvent,
  81. message,
  82. 0, 0, 0,
  83. doRequest,
  84. e);
  85. that._readyPromise.reject(new RuntimeError(message));
  86. }
  87. function doRequest() {
  88. ImageryProvider
  89. .loadImage(null, resource)
  90. .then(success)
  91. .otherwise(failure);
  92. }
  93. doRequest();
  94. }
  95. defineProperties(SingleTileImageryProvider.prototype, {
  96. /**
  97. * Gets the URL of the single, top-level imagery tile.
  98. * @memberof SingleTileImageryProvider.prototype
  99. * @type {String}
  100. * @readonly
  101. */
  102. url : {
  103. get : function() {
  104. return this._resource.url;
  105. }
  106. },
  107. /**
  108. * Gets the proxy used by this provider.
  109. * @memberof SingleTileImageryProvider.prototype
  110. * @type {Proxy}
  111. * @readonly
  112. */
  113. proxy : {
  114. get : function() {
  115. return this._resource.proxy;
  116. }
  117. },
  118. /**
  119. * Gets the width of each tile, in pixels. This function should
  120. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  121. * @memberof SingleTileImageryProvider.prototype
  122. * @type {Number}
  123. * @readonly
  124. */
  125. tileWidth : {
  126. get : function() {
  127. //>>includeStart('debug', pragmas.debug);
  128. if (!this._ready) {
  129. throw new DeveloperError('tileWidth must not be called before the imagery provider is ready.');
  130. }
  131. //>>includeEnd('debug');
  132. return this._tileWidth;
  133. }
  134. },
  135. /**
  136. * Gets the height of each tile, in pixels. This function should
  137. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  138. * @memberof SingleTileImageryProvider.prototype
  139. * @type {Number}
  140. * @readonly
  141. */
  142. tileHeight: {
  143. get : function() {
  144. //>>includeStart('debug', pragmas.debug);
  145. if (!this._ready) {
  146. throw new DeveloperError('tileHeight must not be called before the imagery provider is ready.');
  147. }
  148. //>>includeEnd('debug');
  149. return this._tileHeight;
  150. }
  151. },
  152. /**
  153. * Gets the maximum level-of-detail that can be requested. This function should
  154. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  155. * @memberof SingleTileImageryProvider.prototype
  156. * @type {Number}
  157. * @readonly
  158. */
  159. maximumLevel : {
  160. get : function() {
  161. //>>includeStart('debug', pragmas.debug);
  162. if (!this._ready) {
  163. throw new DeveloperError('maximumLevel must not be called before the imagery provider is ready.');
  164. }
  165. //>>includeEnd('debug');
  166. return 0;
  167. }
  168. },
  169. /**
  170. * Gets the minimum level-of-detail that can be requested. This function should
  171. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  172. * @memberof SingleTileImageryProvider.prototype
  173. * @type {Number}
  174. * @readonly
  175. */
  176. minimumLevel : {
  177. get : function() {
  178. //>>includeStart('debug', pragmas.debug);
  179. if (!this._ready) {
  180. throw new DeveloperError('minimumLevel must not be called before the imagery provider is ready.');
  181. }
  182. //>>includeEnd('debug');
  183. return 0;
  184. }
  185. },
  186. /**
  187. * Gets the tiling scheme used by this provider. This function should
  188. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  189. * @memberof SingleTileImageryProvider.prototype
  190. * @type {TilingScheme}
  191. * @readonly
  192. */
  193. tilingScheme : {
  194. get : function() {
  195. //>>includeStart('debug', pragmas.debug);
  196. if (!this._ready) {
  197. throw new DeveloperError('tilingScheme must not be called before the imagery provider is ready.');
  198. }
  199. //>>includeEnd('debug');
  200. return this._tilingScheme;
  201. }
  202. },
  203. /**
  204. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  205. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  206. * @memberof SingleTileImageryProvider.prototype
  207. * @type {Rectangle}
  208. * @readonly
  209. */
  210. rectangle : {
  211. get : function() {
  212. return this._tilingScheme.rectangle;
  213. }
  214. },
  215. /**
  216. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  217. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  218. * returns undefined, no tiles are filtered. This function should
  219. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  220. * @memberof SingleTileImageryProvider.prototype
  221. * @type {TileDiscardPolicy}
  222. * @readonly
  223. */
  224. tileDiscardPolicy : {
  225. get : function() {
  226. //>>includeStart('debug', pragmas.debug);
  227. if (!this._ready) {
  228. throw new DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.');
  229. }
  230. //>>includeEnd('debug');
  231. return undefined;
  232. }
  233. },
  234. /**
  235. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  236. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  237. * are passed an instance of {@link TileProviderError}.
  238. * @memberof SingleTileImageryProvider.prototype
  239. * @type {Event}
  240. * @readonly
  241. */
  242. errorEvent : {
  243. get : function() {
  244. return this._errorEvent;
  245. }
  246. },
  247. /**
  248. * Gets a value indicating whether or not the provider is ready for use.
  249. * @memberof SingleTileImageryProvider.prototype
  250. * @type {Boolean}
  251. * @readonly
  252. */
  253. ready : {
  254. get : function() {
  255. return this._ready;
  256. }
  257. },
  258. /**
  259. * Gets a promise that resolves to true when the provider is ready for use.
  260. * @memberof SingleTileImageryProvider.prototype
  261. * @type {Promise.<Boolean>}
  262. * @readonly
  263. */
  264. readyPromise : {
  265. get : function() {
  266. return this._readyPromise.promise;
  267. }
  268. },
  269. /**
  270. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  271. * the source of the imagery. This function should not be called before {@link SingleTileImageryProvider#ready} returns true.
  272. * @memberof SingleTileImageryProvider.prototype
  273. * @type {Credit}
  274. * @readonly
  275. */
  276. credit : {
  277. get : function() {
  278. return this._credit;
  279. }
  280. },
  281. /**
  282. * Gets a value indicating whether or not the images provided by this imagery provider
  283. * include an alpha channel. If this property is false, an alpha channel, if present, will
  284. * be ignored. If this property is true, any images without an alpha channel will be treated
  285. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  286. * and texture upload time are reduced.
  287. * @memberof SingleTileImageryProvider.prototype
  288. * @type {Boolean}
  289. * @readonly
  290. */
  291. hasAlphaChannel : {
  292. get : function() {
  293. return true;
  294. }
  295. }
  296. });
  297. /**
  298. * Gets the credits to be displayed when a given tile is displayed.
  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. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  304. *
  305. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  306. */
  307. SingleTileImageryProvider.prototype.getTileCredits = function(x, y, level) {
  308. return undefined;
  309. };
  310. /**
  311. * Requests the image for a given tile. This function should
  312. * not be called before {@link SingleTileImageryProvider#ready} returns true.
  313. *
  314. * @param {Number} x The tile X coordinate.
  315. * @param {Number} y The tile Y coordinate.
  316. * @param {Number} level The tile level.
  317. * @param {Request} [request] The request object. Intended for internal use only.
  318. * @returns {Promise.<Image|Canvas>|undefined} A promise for the image that will resolve when the image is available, or
  319. * undefined if there are too many active requests to the server, and the request
  320. * should be retried later. The resolved image may be either an
  321. * Image or a Canvas DOM object.
  322. *
  323. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  324. */
  325. SingleTileImageryProvider.prototype.requestImage = function(x, y, level, request) {
  326. //>>includeStart('debug', pragmas.debug);
  327. if (!this._ready) {
  328. throw new DeveloperError('requestImage must not be called before the imagery provider is ready.');
  329. }
  330. //>>includeEnd('debug');
  331. return this._image;
  332. };
  333. /**
  334. * Picking features is not currently supported by this imagery provider, so this function simply returns
  335. * undefined.
  336. *
  337. * @param {Number} x The tile X coordinate.
  338. * @param {Number} y The tile Y coordinate.
  339. * @param {Number} level The tile level.
  340. * @param {Number} longitude The longitude at which to pick features.
  341. * @param {Number} latitude The latitude at which to pick features.
  342. * @return {Promise.<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  343. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  344. * instances. The array may be empty if no features are found at the given location.
  345. * It may also be undefined if picking is not supported.
  346. */
  347. SingleTileImageryProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
  348. return undefined;
  349. };
  350. export default SingleTileImageryProvider;