ArcGisMapServerImageryProvider.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*global define*/
  2. define([
  3. '../Core/Cartesian2',
  4. '../Core/Credit',
  5. '../Core/defaultValue',
  6. '../Core/defined',
  7. '../Core/defineProperties',
  8. '../Core/DeveloperError',
  9. '../Core/Event',
  10. '../Core/GeographicProjection',
  11. '../Core/GeographicTilingScheme',
  12. '../Core/jsonp',
  13. '../Core/Rectangle',
  14. '../Core/TileProviderError',
  15. '../Core/WebMercatorProjection',
  16. '../Core/WebMercatorTilingScheme',
  17. '../ThirdParty/when',
  18. './DiscardMissingTileImagePolicy',
  19. './ImageryProvider'
  20. ], function(
  21. Cartesian2,
  22. Credit,
  23. defaultValue,
  24. defined,
  25. defineProperties,
  26. DeveloperError,
  27. Event,
  28. GeographicProjection,
  29. GeographicTilingScheme,
  30. jsonp,
  31. Rectangle,
  32. TileProviderError,
  33. WebMercatorProjection,
  34. WebMercatorTilingScheme,
  35. when,
  36. DiscardMissingTileImagePolicy,
  37. ImageryProvider) {
  38. "use strict";
  39. /**
  40. * Provides tiled imagery hosted by an ArcGIS MapServer. By default, the server's pre-cached tiles are
  41. * used, if available.
  42. *
  43. * @alias ArcGisMapServerImageryProvider
  44. * @constructor
  45. *
  46. * @param {Object} options Object with the following properties:
  47. * @param {String} options.url The URL of the ArcGIS MapServer service.
  48. * @param {TileDiscardPolicy} [options.tileDiscardPolicy] The policy that determines if a tile
  49. * is invalid and should be discarded. If this value is not specified, a default
  50. * {@link DiscardMissingTileImagePolicy} is used for tiled map servers, and a
  51. * {@link NeverTileDiscardPolicy} is used for non-tiled map servers. In the former case,
  52. * we request tile 0,0 at the maximum tile level and check pixels (0,0), (200,20), (20,200),
  53. * (80,110), and (160, 130). If all of these pixels are transparent, the discard check is
  54. * disabled and no tiles are discarded. If any of them have a non-transparent color, any
  55. * tile that has the same values in these pixel locations is discarded. The end result of
  56. * these defaults should be correct tile discarding for a standard ArcGIS Server. To ensure
  57. * that no tiles are discarded, construct and pass a {@link NeverTileDiscardPolicy} for this
  58. * parameter.
  59. * @param {Proxy} [options.proxy] A proxy to use for requests. This object is
  60. * expected to have a getURL function which returns the proxied URL, if needed.
  61. * @param {Boolean} [options.usePreCachedTilesIfAvailable=true] If true, the server's pre-cached
  62. * tiles are used if they are available. If false, any pre-cached tiles are ignored and the
  63. * 'export' service is used.
  64. *
  65. * @see BingMapsImageryProvider
  66. * @see GoogleEarthImageryProvider
  67. * @see OpenStreetMapImageryProvider
  68. * @see SingleTileImageryProvider
  69. * @see TileMapServiceImageryProvider
  70. * @see WebMapServiceImageryProvider
  71. *
  72. * @see {@link http://resources.esri.com/help/9.3/arcgisserver/apis/rest/|ArcGIS Server REST API}
  73. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  74. *
  75. * @example
  76. * var esri = new Cesium.ArcGisMapServerImageryProvider({
  77. * url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
  78. * });
  79. */
  80. var ArcGisMapServerImageryProvider = function ArcGisMapServerImageryProvider(options) {
  81. options = defaultValue(options, {});
  82. //>>includeStart('debug', pragmas.debug);
  83. if (!defined(options.url)) {
  84. throw new DeveloperError('options.url is required.');
  85. }
  86. //>>includeEnd('debug');
  87. this._url = options.url;
  88. this._tileDiscardPolicy = options.tileDiscardPolicy;
  89. this._proxy = options.proxy;
  90. this._tileWidth = undefined;
  91. this._tileHeight = undefined;
  92. this._maximumLevel = undefined;
  93. this._tilingScheme = undefined;
  94. this._credit = undefined;
  95. this._useTiles = defaultValue(options.usePreCachedTilesIfAvailable, true);
  96. this._rectangle = undefined;
  97. this._errorEvent = new Event();
  98. this._ready = false;
  99. // Grab the details of this MapServer.
  100. var that = this;
  101. var metadataError;
  102. function metadataSuccess(data) {
  103. var tileInfo = data.tileInfo;
  104. if (!that._useTiles || !defined(tileInfo)) {
  105. that._tileWidth = 256;
  106. that._tileHeight = 256;
  107. that._tilingScheme = new GeographicTilingScheme();
  108. that._rectangle = that._tilingScheme.rectangle;
  109. that._useTiles = false;
  110. } else {
  111. that._tileWidth = tileInfo.rows;
  112. that._tileHeight = tileInfo.cols;
  113. if (tileInfo.spatialReference.wkid === 102100 ||
  114. tileInfo.spatialReference.wkid === 102113) {
  115. that._tilingScheme = new WebMercatorTilingScheme();
  116. } else if (data.tileInfo.spatialReference.wkid === 4326) {
  117. that._tilingScheme = new GeographicTilingScheme();
  118. } else {
  119. var message = 'Tile spatial reference WKID ' + data.tileInfo.spatialReference.wkid + ' is not supported.';
  120. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, message, undefined, undefined, undefined, requestMetadata);
  121. return;
  122. }
  123. that._maximumLevel = data.tileInfo.lods.length - 1;
  124. if (defined(data.fullExtent)) {
  125. var projection = that._tilingScheme.projection;
  126. if (defined(data.fullExtent.spatialReference) && defined(data.fullExtent.spatialReference.wkid)) {
  127. if (data.fullExtent.spatialReference.wkid === 102100 ||
  128. data.fullExtent.spatialReference.wkid === 102113) {
  129. projection = new WebMercatorProjection();
  130. } else if (data.fullExtent.spatialReference.wkid === 4326) {
  131. projection = new GeographicProjection();
  132. } else {
  133. var extentMessage = 'fullExtent.spatialReference WKID ' + data.fullExtent.spatialReference.wkid + ' is not supported.';
  134. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, extentMessage, undefined, undefined, undefined, requestMetadata);
  135. return;
  136. }
  137. }
  138. var sw = projection.unproject(new Cartesian2(data.fullExtent.xmin, data.fullExtent.ymin));
  139. var ne = projection.unproject(new Cartesian2(data.fullExtent.xmax, data.fullExtent.ymax));
  140. that._rectangle = new Rectangle(sw.longitude, sw.latitude, ne.longitude, ne.latitude);
  141. } else {
  142. that._rectangle = that._tilingScheme.rectangle;
  143. }
  144. // Install the default tile discard policy if none has been supplied.
  145. if (!defined(that._tileDiscardPolicy)) {
  146. that._tileDiscardPolicy = new DiscardMissingTileImagePolicy({
  147. missingImageUrl : buildImageUrl(that, 0, 0, that._maximumLevel),
  148. pixelsToCheck : [new Cartesian2(0, 0), new Cartesian2(200, 20), new Cartesian2(20, 200), new Cartesian2(80, 110), new Cartesian2(160, 130)],
  149. disableCheckIfAllPixelsAreTransparent : true
  150. });
  151. }
  152. that._useTiles = true;
  153. }
  154. if (defined(data.copyrightText) && data.copyrightText.length > 0) {
  155. that._credit = new Credit(data.copyrightText);
  156. }
  157. that._ready = true;
  158. TileProviderError.handleSuccess(metadataError);
  159. }
  160. function metadataFailure(e) {
  161. var message = 'An error occurred while accessing ' + that._url + '.';
  162. metadataError = TileProviderError.handleError(metadataError, that, that._errorEvent, message, undefined, undefined, undefined, requestMetadata);
  163. }
  164. function requestMetadata() {
  165. var metadata = jsonp(that._url, {
  166. parameters : {
  167. f : 'json'
  168. },
  169. proxy : that._proxy
  170. });
  171. when(metadata, metadataSuccess, metadataFailure);
  172. }
  173. requestMetadata();
  174. };
  175. function buildImageUrl(imageryProvider, x, y, level) {
  176. var url;
  177. if (imageryProvider._useTiles) {
  178. url = imageryProvider._url + '/tile/' + level + '/' + y + '/' + x;
  179. } else {
  180. var nativeRectangle = imageryProvider._tilingScheme.tileXYToNativeRectangle(x, y, level);
  181. var bbox = nativeRectangle.west + '%2C' + nativeRectangle.south + '%2C' + nativeRectangle.east + '%2C' + nativeRectangle.north;
  182. url = imageryProvider._url + '/export?';
  183. url += 'bbox=' + bbox;
  184. url += '&bboxSR=4326&size=256%2C256&imageSR=4326&format=png&transparent=true&f=image';
  185. }
  186. var proxy = imageryProvider._proxy;
  187. if (defined(proxy)) {
  188. url = proxy.getURL(url);
  189. }
  190. return url;
  191. }
  192. defineProperties(ArcGisMapServerImageryProvider.prototype, {
  193. /**
  194. * Gets the URL of the ArcGIS MapServer.
  195. * @memberof ArcGisMapServerImageryProvider.prototype
  196. * @type {String}
  197. * @readonly
  198. */
  199. url : {
  200. get : function() {
  201. return this._url;
  202. }
  203. },
  204. /**
  205. * Gets the proxy used by this provider.
  206. * @memberof ArcGisMapServerImageryProvider.prototype
  207. * @type {Proxy}
  208. * @readonly
  209. */
  210. proxy : {
  211. get : function() {
  212. return this._proxy;
  213. }
  214. },
  215. /**
  216. * Gets the width of each tile, in pixels. This function should
  217. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  218. * @memberof ArcGisMapServerImageryProvider.prototype
  219. * @type {Number}
  220. * @readonly
  221. */
  222. tileWidth : {
  223. get : function() {
  224. //>>includeStart('debug', pragmas.debug);
  225. if (!this._ready) {
  226. throw new DeveloperError('tileWidth must not be called before the imagery provider is ready.');
  227. }
  228. //>>includeEnd('debug');
  229. return this._tileWidth;
  230. }
  231. },
  232. /**
  233. * Gets the height of each tile, in pixels. This function should
  234. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  235. * @memberof ArcGisMapServerImageryProvider.prototype
  236. * @type {Number}
  237. * @readonly
  238. */
  239. tileHeight: {
  240. get : function() {
  241. //>>includeStart('debug', pragmas.debug);
  242. if (!this._ready) {
  243. throw new DeveloperError('tileHeight must not be called before the imagery provider is ready.');
  244. }
  245. //>>includeEnd('debug');
  246. return this._tileHeight;
  247. }
  248. },
  249. /**
  250. * Gets the maximum level-of-detail that can be requested. This function should
  251. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  252. * @memberof ArcGisMapServerImageryProvider.prototype
  253. * @type {Number}
  254. * @readonly
  255. */
  256. maximumLevel : {
  257. get : function() {
  258. //>>includeStart('debug', pragmas.debug);
  259. if (!this._ready) {
  260. throw new DeveloperError('maximumLevel must not be called before the imagery provider is ready.');
  261. }
  262. //>>includeEnd('debug');
  263. return this._maximumLevel;
  264. }
  265. },
  266. /**
  267. * Gets the minimum level-of-detail that can be requested. This function should
  268. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  269. * @memberof ArcGisMapServerImageryProvider.prototype
  270. * @type {Number}
  271. * @readonly
  272. */
  273. minimumLevel : {
  274. get : function() {
  275. //>>includeStart('debug', pragmas.debug);
  276. if (!this._ready) {
  277. throw new DeveloperError('minimumLevel must not be called before the imagery provider is ready.');
  278. }
  279. //>>includeEnd('debug');
  280. return 0;
  281. }
  282. },
  283. /**
  284. * Gets the tiling scheme used by this provider. This function should
  285. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  286. * @memberof ArcGisMapServerImageryProvider.prototype
  287. * @type {TilingScheme}
  288. * @readonly
  289. */
  290. tilingScheme : {
  291. get : function() {
  292. //>>includeStart('debug', pragmas.debug);
  293. if (!this._ready) {
  294. throw new DeveloperError('tilingScheme must not be called before the imagery provider is ready.');
  295. }
  296. //>>includeEnd('debug');
  297. return this._tilingScheme;
  298. }
  299. },
  300. /**
  301. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  302. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  303. * @memberof ArcGisMapServerImageryProvider.prototype
  304. * @type {Rectangle}
  305. * @readonly
  306. */
  307. rectangle : {
  308. get : function() {
  309. //>>includeStart('debug', pragmas.debug);
  310. if (!this._ready) {
  311. throw new DeveloperError('rectangle must not be called before the imagery provider is ready.');
  312. }
  313. //>>includeEnd('debug');
  314. return this._rectangle;
  315. }
  316. },
  317. /**
  318. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  319. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  320. * returns undefined, no tiles are filtered. This function should
  321. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  322. * @memberof ArcGisMapServerImageryProvider.prototype
  323. * @type {TileDiscardPolicy}
  324. * @readonly
  325. */
  326. tileDiscardPolicy : {
  327. get : function() {
  328. //>>includeStart('debug', pragmas.debug);
  329. if (!this._ready) {
  330. throw new DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.');
  331. }
  332. //>>includeEnd('debug');
  333. return this._tileDiscardPolicy;
  334. }
  335. },
  336. /**
  337. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  338. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  339. * are passed an instance of {@link TileProviderError}.
  340. * @memberof ArcGisMapServerImageryProvider.prototype
  341. * @type {Event}
  342. * @readonly
  343. */
  344. errorEvent : {
  345. get : function() {
  346. return this._errorEvent;
  347. }
  348. },
  349. /**
  350. * Gets a value indicating whether or not the provider is ready for use.
  351. * @memberof ArcGisMapServerImageryProvider.prototype
  352. * @type {Boolean}
  353. * @readonly
  354. */
  355. ready : {
  356. get : function() {
  357. return this._ready;
  358. }
  359. },
  360. /**
  361. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  362. * the source of the imagery. This function should not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  363. * @memberof ArcGisMapServerImageryProvider.prototype
  364. * @type {Credit}
  365. * @readonly
  366. */
  367. credit : {
  368. get : function() {
  369. return this._credit;
  370. }
  371. },
  372. /**
  373. * Gets a value indicating whether this imagery provider is using pre-cached tiles from the
  374. * ArcGIS MapServer. If the imagery provider is not yet ready ({@link ArcGisMapServerImageryProvider#ready}), this function
  375. * will return the value of `options.usePreCachedTilesIfAvailable`, even if the MapServer does
  376. * not have pre-cached tiles.
  377. * @memberof ArcGisMapServerImageryProvider.prototype
  378. *
  379. * @type {Boolean}
  380. * @readonly
  381. * @default true
  382. */
  383. usingPrecachedTiles : {
  384. get : function() {
  385. return this._useTiles;
  386. }
  387. },
  388. /**
  389. * Gets a value indicating whether or not the images provided by this imagery provider
  390. * include an alpha channel. If this property is false, an alpha channel, if present, will
  391. * be ignored. If this property is true, any images without an alpha channel will be treated
  392. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  393. * and texture upload time are reduced.
  394. * @memberof ArcGisMapServerImageryProvider.prototype
  395. *
  396. * @type {Boolean}
  397. * @readonly
  398. * @default true
  399. */
  400. hasAlphaChannel : {
  401. get : function() {
  402. return true;
  403. }
  404. }
  405. });
  406. /**
  407. * Gets the credits to be displayed when a given tile is displayed.
  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. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  413. *
  414. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  415. */
  416. ArcGisMapServerImageryProvider.prototype.getTileCredits = function(x, y, level) {
  417. return undefined;
  418. };
  419. /**
  420. * Requests the image for a given tile. This function should
  421. * not be called before {@link ArcGisMapServerImageryProvider#ready} returns true.
  422. *
  423. * @param {Number} x The tile X coordinate.
  424. * @param {Number} y The tile Y coordinate.
  425. * @param {Number} level The tile level.
  426. * @returns {Promise} A promise for the image that will resolve when the image is available, or
  427. * undefined if there are too many active requests to the server, and the request
  428. * should be retried later. The resolved image may be either an
  429. * Image or a Canvas DOM object.
  430. *
  431. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  432. */
  433. ArcGisMapServerImageryProvider.prototype.requestImage = function(x, y, level) {
  434. //>>includeStart('debug', pragmas.debug);
  435. if (!this._ready) {
  436. throw new DeveloperError('requestImage must not be called before the imagery provider is ready.');
  437. }
  438. //>>includeEnd('debug');
  439. var url = buildImageUrl(this, x, y, level);
  440. return ImageryProvider.loadImage(this, url);
  441. };
  442. /**
  443. * Picking features is not currently supported by this imagery provider, so this function simply returns
  444. * undefined.
  445. *
  446. * @param {Number} x The tile X coordinate.
  447. * @param {Number} y The tile Y coordinate.
  448. * @param {Number} level The tile level.
  449. * @param {Number} longitude The longitude at which to pick features.
  450. * @param {Number} latitude The latitude at which to pick features.
  451. * @return {Promise} A promise for the picked features that will resolve when the asynchronous
  452. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  453. * instances. The array may be empty if no features are found at the given location.
  454. * It may also be undefined if picking is not supported.
  455. */
  456. ArcGisMapServerImageryProvider.prototype.pickFeatures = function() {
  457. return undefined;
  458. };
  459. return ArcGisMapServerImageryProvider;
  460. });