WebMapServiceImageryProvider.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*global define*/
  2. define([
  3. '../Core/Cartesian3',
  4. '../Core/Cartographic',
  5. '../Core/combine',
  6. '../Core/Credit',
  7. '../Core/defaultValue',
  8. '../Core/defined',
  9. '../Core/defineProperties',
  10. '../Core/DeveloperError',
  11. '../Core/Event',
  12. '../Core/freezeObject',
  13. '../Core/GeographicTilingScheme',
  14. '../Core/loadJson',
  15. '../Core/loadXML',
  16. '../Core/Math',
  17. '../Core/objectToQuery',
  18. '../Core/queryToObject',
  19. '../Core/Rectangle',
  20. '../Core/WebMercatorTilingScheme',
  21. '../ThirdParty/Uri',
  22. '../ThirdParty/when',
  23. './ImageryLayerFeatureInfo',
  24. './ImageryProvider'
  25. ], function(
  26. Cartesian3,
  27. Cartographic,
  28. combine,
  29. Credit,
  30. defaultValue,
  31. defined,
  32. defineProperties,
  33. DeveloperError,
  34. Event,
  35. freezeObject,
  36. GeographicTilingScheme,
  37. loadJson,
  38. loadXML,
  39. CesiumMath,
  40. objectToQuery,
  41. queryToObject,
  42. Rectangle,
  43. WebMercatorTilingScheme,
  44. Uri,
  45. when,
  46. ImageryLayerFeatureInfo,
  47. ImageryProvider) {
  48. "use strict";
  49. function objectToLowercase(obj) {
  50. var result = {};
  51. for ( var key in obj) {
  52. if (obj.hasOwnProperty(key)) {
  53. result[key.toLowerCase()] = obj[key];
  54. }
  55. }
  56. return result;
  57. }
  58. /**
  59. * Provides tiled imagery hosted by a Web Map Service (WMS) server.
  60. *
  61. * @alias WebMapServiceImageryProvider
  62. * @constructor
  63. *
  64. * @param {Object} options Object with the following properties:
  65. * @param {String} options.url The URL of the WMS service.
  66. * @param {String} options.layers The layers to include, separated by commas.
  67. * @param {Object} [options.parameters=WebMapServiceImageryProvider.DefaultParameters] Additional parameters
  68. * to pass to the WMS server in the GetMap URL.
  69. * @param {Object} [options.getFeatureInfoParameters=WebMapServiceImageryProvider.GetFeatureInfoDefaultParameters] Additional
  70. * parameters to pass to the WMS server in the GetFeatureInfo URL.
  71. * @param {Boolean} [options.enablePickFeatures=true] If true, {@link WebMapServiceImageryProvider#pickFeatures} will invoke
  72. * the GetFeatureInfo operation on the WMS server and return the features included in the response. If false,
  73. * {@link WebMapServiceImageryProvider#pickFeatures} will immediately return undefined (indicating no pickable features)
  74. * without communicating with the server. Set this property to false if you know your WMS server does not support
  75. * GetFeatureInfo or if you don't want this provider's features to be pickable.
  76. * @param {Boolean} [options.getFeatureInfoAsGeoJson=true] true if {@link WebMapServiceImageryProvider#pickFeatures} should
  77. * try requesting feature info in GeoJSON format. If getFeatureInfoAsXml is true as well, feature information will be
  78. * requested first as GeoJSON, and then as XML if the GeoJSON request fails. If both are false, this instance will
  79. * not support feature picking at all.
  80. * @param {Boolean} [options.getFeatureInfoAsXml=true] true if {@link WebMapServiceImageryProvider#pickFeatures} should try
  81. * requesting feature info in XML format. If getFeatureInfoAsGeoJson is true as well, feature information will be
  82. * requested first as GeoJSON, and then as XML if the GeoJSON request fails. If both are false, this instance
  83. * will not support feature picking at all.
  84. * @param {Rectangle} [options.rectangle=Rectangle.MAX_VALUE] The rectangle of the layer.
  85. * @param {TilingScheme} [options.tilingScheme=new GeographicTilingScheme()] The tiling scheme to use to divide the world into tiles.
  86. * @param {Number} [options.tileWidth=256] The width of each tile in pixels.
  87. * @param {Number} [options.tileHeight=256] The height of each tile in pixels.
  88. * @param {Number} [options.minimumLevel=0] The minimum level-of-detail supported by the imagery provider. Take care when
  89. * specifying this that the number of tiles at the minimum level is small, such as four or less. A larger number is
  90. * likely to result in rendering problems.
  91. * @param {Number} [options.maximumLevel] The maximum level-of-detail supported by the imagery provider.
  92. * If not specified, there is no limit.
  93. * @param {Credit|String} [options.credit] A credit for the data source, which is displayed on the canvas.
  94. * @param {Object} [options.proxy] A proxy to use for requests. This object is
  95. * expected to have a getURL function which returns the proxied URL, if needed.
  96. *
  97. * @see ArcGisMapServerImageryProvider
  98. * @see BingMapsImageryProvider
  99. * @see GoogleEarthImageryProvider
  100. * @see SingleTileImageryProvider
  101. * @see TileMapServiceImageryProvider
  102. * @see OpenStreetMapImageryProvider
  103. *
  104. * @see {@link http://resources.esri.com/help/9.3/arcgisserver/apis/rest/|ArcGIS Server REST API}
  105. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  106. *
  107. * @example
  108. * var provider = new Cesium.WebMapServiceImageryProvider({
  109. * url: '//sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer',
  110. * layers : '0',
  111. * proxy: new Cesium.DefaultProxy('/proxy/')
  112. * });
  113. *
  114. * viewer.imageryLayers.addImageryProvider(provider);
  115. */
  116. var WebMapServiceImageryProvider = function WebMapServiceImageryProvider(options) {
  117. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  118. //>>includeStart('debug', pragmas.debug);
  119. if (!defined(options.url)) {
  120. throw new DeveloperError('options.url is required.');
  121. }
  122. if (!defined(options.layers)) {
  123. throw new DeveloperError('options.layers is required.');
  124. }
  125. //>>includeEnd('debug');
  126. this._url = options.url;
  127. this._tileDiscardPolicy = options.tileDiscardPolicy;
  128. this._proxy = options.proxy;
  129. this._layers = options.layers;
  130. this._enablePickFeatures = defaultValue(options.enablePickFeatures, true);
  131. this._getFeatureInfoAsGeoJson = defaultValue(options.getFeatureInfoAsGeoJson, true);
  132. this._getFeatureInfoAsXml = defaultValue(options.getFeatureInfoAsXml, true);
  133. // Merge the parameters with the defaults, and make all parameter names lowercase
  134. this._parameters = combine(objectToLowercase(defaultValue(options.parameters, defaultValue.EMPTY_OBJECT)), WebMapServiceImageryProvider.DefaultParameters);
  135. this._getFeatureInfoParameters = combine(objectToLowercase(defaultValue(options.getFeatureInfoParameters, defaultValue.EMPTY_OBJECT)), WebMapServiceImageryProvider.GetFeatureInfoDefaultParameters);
  136. this._tileWidth = defaultValue(options.tileWidth, 256);
  137. this._tileHeight = defaultValue(options.tileHeight, 256);
  138. this._minimumLevel = defaultValue(options.minimumLevel, 0);
  139. this._maximumLevel = options.maximumLevel; // undefined means no limit
  140. this._rectangle = defaultValue(options.rectangle, Rectangle.MAX_VALUE);
  141. this._tilingScheme = defined(options.tilingScheme) ? options.tilingScheme : new GeographicTilingScheme();
  142. this._rectangle = Rectangle.intersection(this._rectangle, this._tilingScheme.rectangle);
  143. var credit = options.credit;
  144. if (typeof credit === 'string') {
  145. credit = new Credit(credit);
  146. }
  147. this._credit = credit;
  148. this._errorEvent = new Event();
  149. this._ready = true;
  150. };
  151. defineProperties(WebMapServiceImageryProvider.prototype, {
  152. /**
  153. * Gets the URL of the WMS server.
  154. * @memberof WebMapServiceImageryProvider.prototype
  155. * @type {String}
  156. * @readonly
  157. */
  158. url : {
  159. get : function() {
  160. return this._url;
  161. }
  162. },
  163. /**
  164. * Gets the proxy used by this provider.
  165. * @memberof WebMapServiceImageryProvider.prototype
  166. * @type {Proxy}
  167. * @readonly
  168. */
  169. proxy : {
  170. get : function() {
  171. return this._proxy;
  172. }
  173. },
  174. /**
  175. * Gets the names of the WMS layers, separated by commas.
  176. * @memberof WebMapServiceImageryProvider.prototype
  177. * @type {String}
  178. * @readonly
  179. */
  180. layers : {
  181. get : function() {
  182. return this._layers;
  183. }
  184. },
  185. /**
  186. * Gets the width of each tile, in pixels. This function should
  187. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  188. * @memberof WebMapServiceImageryProvider.prototype
  189. * @type {Number}
  190. * @readonly
  191. */
  192. tileWidth : {
  193. get : function() {
  194. //>>includeStart('debug', pragmas.debug);
  195. if (!this._ready) {
  196. throw new DeveloperError('tileWidth must not be called before the imagery provider is ready.');
  197. }
  198. //>>includeEnd('debug');
  199. return this._tileWidth;
  200. }
  201. },
  202. /**
  203. * Gets the height of each tile, in pixels. This function should
  204. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  205. * @memberof WebMapServiceImageryProvider.prototype
  206. * @type {Number}
  207. * @readonly
  208. */
  209. tileHeight : {
  210. get : function() {
  211. //>>includeStart('debug', pragmas.debug);
  212. if (!this._ready) {
  213. throw new DeveloperError('tileHeight must not be called before the imagery provider is ready.');
  214. }
  215. //>>includeEnd('debug');
  216. return this._tileHeight;
  217. }
  218. },
  219. /**
  220. * Gets the maximum level-of-detail that can be requested. This function should
  221. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  222. * @memberof WebMapServiceImageryProvider.prototype
  223. * @type {Number}
  224. * @readonly
  225. */
  226. maximumLevel : {
  227. get : function() {
  228. //>>includeStart('debug', pragmas.debug);
  229. if (!this._ready) {
  230. throw new DeveloperError('maximumLevel must not be called before the imagery provider is ready.');
  231. }
  232. //>>includeEnd('debug');
  233. return this._maximumLevel;
  234. }
  235. },
  236. /**
  237. * Gets the minimum level-of-detail that can be requested. This function should
  238. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  239. * @memberof WebMapServiceImageryProvider.prototype
  240. * @type {Number}
  241. * @readonly
  242. */
  243. minimumLevel : {
  244. get : function() {
  245. //>>includeStart('debug', pragmas.debug);
  246. if (!this._ready) {
  247. throw new DeveloperError('minimumLevel must not be called before the imagery provider is ready.');
  248. }
  249. //>>includeEnd('debug');
  250. return this._minimumLevel;
  251. }
  252. },
  253. /**
  254. * Gets the tiling scheme used by this provider. This function should
  255. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  256. * @memberof WebMapServiceImageryProvider.prototype
  257. * @type {TilingScheme}
  258. * @readonly
  259. */
  260. tilingScheme : {
  261. get : function() {
  262. //>>includeStart('debug', pragmas.debug);
  263. if (!this._ready) {
  264. throw new DeveloperError('tilingScheme must not be called before the imagery provider is ready.');
  265. }
  266. //>>includeEnd('debug');
  267. return this._tilingScheme;
  268. }
  269. },
  270. /**
  271. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  272. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  273. * @memberof WebMapServiceImageryProvider.prototype
  274. * @type {Rectangle}
  275. * @readonly
  276. */
  277. rectangle : {
  278. get : function() {
  279. //>>includeStart('debug', pragmas.debug);
  280. if (!this._ready) {
  281. throw new DeveloperError('rectangle must not be called before the imagery provider is ready.');
  282. }
  283. //>>includeEnd('debug');
  284. return this._rectangle;
  285. }
  286. },
  287. /**
  288. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  289. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  290. * returns undefined, no tiles are filtered. This function should
  291. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  292. * @memberof WebMapServiceImageryProvider.prototype
  293. * @type {TileDiscardPolicy}
  294. * @readonly
  295. */
  296. tileDiscardPolicy : {
  297. get : function() {
  298. //>>includeStart('debug', pragmas.debug);
  299. if (!this._ready) {
  300. throw new DeveloperError('tileDiscardPolicy must not be called before the imagery provider is ready.');
  301. }
  302. //>>includeEnd('debug');
  303. return this._tileDiscardPolicy;
  304. }
  305. },
  306. /**
  307. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  308. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  309. * are passed an instance of {@link TileProviderError}.
  310. * @memberof WebMapServiceImageryProvider.prototype
  311. * @type {Event}
  312. * @readonly
  313. */
  314. errorEvent : {
  315. get : function() {
  316. return this._errorEvent;
  317. }
  318. },
  319. /**
  320. * Gets a value indicating whether or not the provider is ready for use.
  321. * @memberof WebMapServiceImageryProvider.prototype
  322. * @type {Boolean}
  323. * @readonly
  324. */
  325. ready : {
  326. get : function() {
  327. return this._ready;
  328. }
  329. },
  330. /**
  331. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  332. * the source of the imagery. This function should not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  333. * @memberof WebMapServiceImageryProvider.prototype
  334. * @type {Credit}
  335. * @readonly
  336. */
  337. credit : {
  338. get : function() {
  339. return this._credit;
  340. }
  341. },
  342. /**
  343. * Gets a value indicating whether or not the images provided by this imagery provider
  344. * include an alpha channel. If this property is false, an alpha channel, if present, will
  345. * be ignored. If this property is true, any images without an alpha channel will be treated
  346. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  347. * and texture upload time are reduced.
  348. * @memberof WebMapServiceImageryProvider.prototype
  349. * @type {Boolean}
  350. * @readonly
  351. */
  352. hasAlphaChannel : {
  353. get : function() {
  354. return true;
  355. }
  356. }
  357. });
  358. /**
  359. * Gets the credits to be displayed when a given tile is displayed.
  360. *
  361. * @param {Number} x The tile X coordinate.
  362. * @param {Number} y The tile Y coordinate.
  363. * @param {Number} level The tile level;
  364. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  365. *
  366. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  367. */
  368. WebMapServiceImageryProvider.prototype.getTileCredits = function(x, y, level) {
  369. return undefined;
  370. };
  371. /**
  372. * Requests the image for a given tile. This function should
  373. * not be called before {@link WebMapServiceImageryProvider#ready} returns true.
  374. *
  375. * @param {Number} x The tile X coordinate.
  376. * @param {Number} y The tile Y coordinate.
  377. * @param {Number} level The tile level.
  378. * @returns {Promise} A promise for the image that will resolve when the image is available, or
  379. * undefined if there are too many active requests to the server, and the request
  380. * should be retried later. The resolved image may be either an
  381. * Image or a Canvas DOM object.
  382. *
  383. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  384. */
  385. WebMapServiceImageryProvider.prototype.requestImage = function(x, y, level) {
  386. //>>includeStart('debug', pragmas.debug);
  387. if (!this._ready) {
  388. throw new DeveloperError('requestImage must not be called before the imagery provider is ready.');
  389. }
  390. //>>includeEnd('debug');
  391. var url = buildImageUrl(this, x, y, level);
  392. return ImageryProvider.loadImage(this, url);
  393. };
  394. var cartographicScratch = new Cartographic();
  395. var cartesian3Scratch = new Cartesian3();
  396. /**
  397. * Asynchronously determines what features, if any, are located at a given longitude and latitude within
  398. * a tile. This function should not be called before {@link ImageryProvider#ready} returns true.
  399. *
  400. * @param {Number} x The tile X coordinate.
  401. * @param {Number} y The tile Y coordinate.
  402. * @param {Number} level The tile level.
  403. * @param {Number} longitude The longitude at which to pick features.
  404. * @param {Number} latitude The latitude at which to pick features.
  405. * @return {Promise} A promise for the picked features that will resolve when the asynchronous
  406. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  407. * instances. The array may be empty if no features are found at the given location.
  408. *
  409. * @exception {DeveloperError} <code>pickFeatures</code> must not be called before the imagery provider is ready.
  410. */
  411. WebMapServiceImageryProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
  412. //>>includeStart('debug', pragmas.debug);
  413. if (!this._ready) {
  414. throw new DeveloperError('pickFeatures must not be called before the imagery provider is ready.');
  415. }
  416. //>>includeEnd('debug');
  417. if (!this._enablePickFeatures) {
  418. return undefined;
  419. }
  420. var rectangle = this._tilingScheme.tileXYToNativeRectangle(x, y, level);
  421. var projected;
  422. if (this._tilingScheme instanceof GeographicTilingScheme) {
  423. projected = cartesian3Scratch;
  424. projected.x = CesiumMath.toDegrees(longitude);
  425. projected.y = CesiumMath.toDegrees(latitude);
  426. } else {
  427. var cartographic = cartographicScratch;
  428. cartographic.longitude = longitude;
  429. cartographic.latitude = latitude;
  430. projected = this._tilingScheme.projection.project(cartographic, cartesian3Scratch);
  431. }
  432. var i = (this._tileWidth * (projected.x - rectangle.west) / rectangle.width) | 0;
  433. var j = (this._tileHeight * (rectangle.north - projected.y) / rectangle.height) | 0;
  434. var url;
  435. if (this._getFeatureInfoAsGeoJson) {
  436. url = buildGetFeatureInfoUrl(this, 'application/json', x, y, level, i, j);
  437. var that = this;
  438. return when(loadJson(url), function(json) {
  439. return geoJsonToFeatureInfo(json);
  440. }, function(e) {
  441. // GeoJSON failed, try XML.
  442. if (!that._getFeatureInfoAsXml) {
  443. return when.reject(e);
  444. }
  445. url = buildGetFeatureInfoUrl(that, 'text/xml', x, y, level, i, j);
  446. return when(loadXML(url), function(xml) {
  447. return xmlToFeatureInfo(xml);
  448. });
  449. });
  450. } else if (this._getFeatureInfoAsXml) {
  451. url = buildGetFeatureInfoUrl(this, 'text/xml', x, y, level, i, j);
  452. return when(loadXML(url), function(xml) {
  453. return xmlToFeatureInfo(xml);
  454. });
  455. } else {
  456. return undefined;
  457. }
  458. };
  459. /**
  460. * The default parameters to include in the WMS URL to obtain images. The values are as follows:
  461. * service=WMS
  462. * version=1.1.1
  463. * request=GetMap
  464. * styles=
  465. * format=image/jpeg
  466. *
  467. * @constant
  468. */
  469. WebMapServiceImageryProvider.DefaultParameters = freezeObject({
  470. service : 'WMS',
  471. version : '1.1.1',
  472. request : 'GetMap',
  473. styles : '',
  474. format : 'image/jpeg'
  475. });
  476. /**
  477. * The default parameters to include in the WMS URL to get feature information. The values are as follows:
  478. * service=WMS
  479. * version=1.1.1
  480. * request=GetFeatureInfo
  481. *
  482. * @constant
  483. */
  484. WebMapServiceImageryProvider.GetFeatureInfoDefaultParameters = freezeObject({
  485. service : 'WMS',
  486. version : '1.1.1',
  487. request : 'GetFeatureInfo'
  488. });
  489. function buildImageUrl(imageryProvider, x, y, level) {
  490. var uri = new Uri(imageryProvider._url);
  491. var queryOptions = queryToObject(defaultValue(uri.query, ''));
  492. queryOptions = combine(imageryProvider._parameters, queryOptions);
  493. if (!defined(queryOptions.layers)) {
  494. queryOptions.layers = imageryProvider._layers;
  495. }
  496. if (!defined(queryOptions.srs)) {
  497. queryOptions.srs = imageryProvider._tilingScheme instanceof WebMercatorTilingScheme ? 'EPSG:3857' : 'EPSG:4326';
  498. }
  499. if (!defined(queryOptions.bbox)) {
  500. var nativeRectangle = imageryProvider._tilingScheme.tileXYToNativeRectangle(x, y, level);
  501. queryOptions.bbox = nativeRectangle.west + ',' + nativeRectangle.south + ',' + nativeRectangle.east + ',' + nativeRectangle.north;
  502. }
  503. if (!defined(queryOptions.width)) {
  504. queryOptions.width = imageryProvider._tileWidth;
  505. }
  506. if (!defined(queryOptions.height)) {
  507. queryOptions.height = imageryProvider._tileHeight;
  508. }
  509. uri.query = objectToQuery(queryOptions);
  510. var url = uri.toString();
  511. var proxy = imageryProvider._proxy;
  512. if (defined(proxy)) {
  513. url = proxy.getURL(url);
  514. }
  515. return url;
  516. }
  517. function buildGetFeatureInfoUrl(imageryProvider, infoFormat, x, y, level, i, j) {
  518. var uri = new Uri(imageryProvider._url);
  519. var queryOptions = queryToObject(defaultValue(uri.query, ''));
  520. queryOptions = combine(imageryProvider._getFeatureInfoParameters, queryOptions);
  521. if (!defined(queryOptions.layers)) {
  522. queryOptions.layers = imageryProvider._layers;
  523. }
  524. if (!defined(queryOptions.query_layers)) {
  525. queryOptions.query_layers = imageryProvider._layers;
  526. }
  527. if (!defined(queryOptions.srs)) {
  528. queryOptions.srs = imageryProvider._tilingScheme instanceof WebMercatorTilingScheme ? 'EPSG:3857' : 'EPSG:4326';
  529. }
  530. if (!defined(queryOptions.bbox)) {
  531. var nativeRectangle = imageryProvider._tilingScheme.tileXYToNativeRectangle(x, y, level);
  532. queryOptions.bbox = nativeRectangle.west + ',' + nativeRectangle.south + ',' + nativeRectangle.east + ',' + nativeRectangle.north;
  533. }
  534. if (!defined(queryOptions.x)) {
  535. queryOptions.x = i;
  536. }
  537. if (!defined(queryOptions.y)) {
  538. queryOptions.y = j;
  539. }
  540. if (!defined(queryOptions.width)) {
  541. queryOptions.width = imageryProvider._tileWidth;
  542. }
  543. if (!defined(queryOptions.height)) {
  544. queryOptions.height = imageryProvider._tileHeight;
  545. }
  546. if (!defined(queryOptions.info_format)) {
  547. queryOptions.info_format = infoFormat;
  548. }
  549. uri.query = objectToQuery(queryOptions);
  550. var url = uri.toString();
  551. var proxy = imageryProvider._proxy;
  552. if (defined(proxy)) {
  553. url = proxy.getURL(url);
  554. }
  555. return url;
  556. }
  557. function geoJsonToFeatureInfo(json) {
  558. var result = [];
  559. var features = json.features;
  560. for (var i = 0; i < features.length; ++i) {
  561. var feature = features[i];
  562. var featureInfo = new ImageryLayerFeatureInfo();
  563. featureInfo.data = feature;
  564. featureInfo.configureNameFromProperties(feature.properties);
  565. featureInfo.configureDescriptionFromProperties(feature.properties);
  566. // If this is a point feature, use the coordinates of the point.
  567. if (feature.geometry.type === 'Point') {
  568. var longitude = feature.geometry.coordinates[0];
  569. var latitude = feature.geometry.coordinates[1];
  570. featureInfo.position = Cartographic.fromDegrees(longitude, latitude);
  571. }
  572. result.push(featureInfo);
  573. }
  574. return result;
  575. }
  576. var mapInfoMxpNamespace = 'http://www.mapinfo.com/mxp';
  577. var esriWmsNamespace = 'http://www.esri.com/wms';
  578. function xmlToFeatureInfo(xml) {
  579. var documentElement = xml.documentElement;
  580. if (documentElement.localName === 'MultiFeatureCollection' && documentElement.namespaceURI === mapInfoMxpNamespace) {
  581. // This looks like a MapInfo MXP response
  582. return mapInfoXmlToFeatureInfo(xml);
  583. } else if (documentElement.localName === 'FeatureInfoResponse' && documentElement.namespaceURI === esriWmsNamespace) {
  584. // This looks like an Esri WMS response
  585. return esriXmlToFeatureInfo(xml);
  586. } else if (documentElement.localName === 'ServiceExceptionReport') {
  587. // This looks like a WMS server error, so no features picked.
  588. return undefined;
  589. } else {
  590. // Unknown response type, so just dump the XML itself into the description.
  591. return unknownXmlToFeatureInfo(xml);
  592. }
  593. }
  594. function mapInfoXmlToFeatureInfo(xml) {
  595. var result = [];
  596. var multiFeatureCollection = xml.documentElement;
  597. var features = multiFeatureCollection.getElementsByTagNameNS(mapInfoMxpNamespace, 'Feature');
  598. for (var featureIndex = 0; featureIndex < features.length; ++featureIndex) {
  599. var feature = features[featureIndex];
  600. var properties = {};
  601. var propertyElements = feature.getElementsByTagNameNS(mapInfoMxpNamespace, 'Val');
  602. for (var propertyIndex = 0; propertyIndex < propertyElements.length; ++propertyIndex) {
  603. var propertyElement = propertyElements[propertyIndex];
  604. if (propertyElement.hasAttribute('ref')) {
  605. var name = propertyElement.getAttribute('ref');
  606. var value = propertyElement.textContent.trim();
  607. properties[name] = value;
  608. }
  609. }
  610. var featureInfo = new ImageryLayerFeatureInfo();
  611. featureInfo.data = feature;
  612. featureInfo.configureNameFromProperties(properties);
  613. featureInfo.configureDescriptionFromProperties(properties);
  614. result.push(featureInfo);
  615. }
  616. return result;
  617. }
  618. function esriXmlToFeatureInfo(xml) {
  619. var result = [];
  620. var featureInfoResponse = xml.documentElement;
  621. var features = featureInfoResponse.getElementsByTagNameNS(esriWmsNamespace, 'FIELDS');
  622. for (var featureIndex = 0; featureIndex < features.length; ++featureIndex) {
  623. var feature = features[featureIndex];
  624. var properties = {};
  625. var propertyAttributes = feature.attributes;
  626. for (var attributeIndex = 0; attributeIndex < propertyAttributes.length; ++attributeIndex) {
  627. var attribute = propertyAttributes[attributeIndex];
  628. properties[attribute.name] = attribute.value;
  629. }
  630. var featureInfo = new ImageryLayerFeatureInfo();
  631. featureInfo.data = feature;
  632. featureInfo.configureNameFromProperties(properties);
  633. featureInfo.configureDescriptionFromProperties(properties);
  634. result.push(featureInfo);
  635. }
  636. return result;
  637. }
  638. function unknownXmlToFeatureInfo(xml) {
  639. var xmlText = new XMLSerializer().serializeToString(xml);
  640. var element = document.createElement('div');
  641. var pre = document.createElement('pre');
  642. pre.textContent = xmlText;
  643. element.appendChild(pre);
  644. var featureInfo = new ImageryLayerFeatureInfo();
  645. featureInfo.data = xml;
  646. featureInfo.description = element.innerHTML;
  647. return [featureInfo];
  648. }
  649. return WebMapServiceImageryProvider;
  650. });