SingleTileImageryProvider.js 13 KB

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