BingMapsImageryProvider.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. import buildModuleUrl from "../Core/buildModuleUrl.js";
  2. import Check from "../Core/Check.js";
  3. import Credit from "../Core/Credit.js";
  4. import defaultValue from "../Core/defaultValue.js";
  5. import defer from "../Core/defer.js";
  6. import defined from "../Core/defined.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import Event from "../Core/Event.js";
  9. import CesiumMath from "../Core/Math.js";
  10. import Rectangle from "../Core/Rectangle.js";
  11. import Resource from "../Core/Resource.js";
  12. import RuntimeError from "../Core/RuntimeError.js";
  13. import TileProviderError from "../Core/TileProviderError.js";
  14. import WebMercatorTilingScheme from "../Core/WebMercatorTilingScheme.js";
  15. import BingMapsStyle from "./BingMapsStyle.js";
  16. import DiscardEmptyTilePolicy from "./DiscardEmptyTileImagePolicy.js";
  17. import ImageryProvider from "./ImageryProvider.js";
  18. /**
  19. * @typedef {Object} BingMapsImageryProvider.ConstructorOptions
  20. *
  21. * Initialization options for the BingMapsImageryProvider constructor
  22. *
  23. * @property {Resource|String} url The url of the Bing Maps server hosting the imagery.
  24. * @property {String} key The Bing Maps key for your application, which can be
  25. * created at {@link https://www.bingmapsportal.com/}.
  26. * @property {String} [tileProtocol] The protocol to use when loading tiles, e.g. 'http' or 'https'.
  27. * By default, tiles are loaded using the same protocol as the page.
  28. * @property {BingMapsStyle} [mapStyle=BingMapsStyle.AERIAL] The type of Bing Maps imagery to load.
  29. * @property {String} [culture=''] The culture to use when requesting Bing Maps imagery. Not
  30. * all cultures are supported. See {@link http://msdn.microsoft.com/en-us/library/hh441729.aspx}
  31. * for information on the supported cultures.
  32. * @property {Ellipsoid} [ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  33. * @property {TileDiscardPolicy} [tileDiscardPolicy] The policy that determines if a tile
  34. * is invalid and should be discarded. By default, a {@link DiscardEmptyTileImagePolicy}
  35. * will be used, with the expectation that the Bing Maps server will send a zero-length response for missing tiles.
  36. * To ensure that no tiles are discarded, construct and pass a {@link NeverTileDiscardPolicy} for this parameter.
  37. */
  38. /**
  39. * Provides tiled imagery using the Bing Maps Imagery REST API.
  40. *
  41. * @alias BingMapsImageryProvider
  42. * @constructor
  43. *
  44. * @param {BingMapsImageryProvider.ConstructorOptions} options Object describing initialization options
  45. *
  46. * @see ArcGisMapServerImageryProvider
  47. * @see GoogleEarthEnterpriseMapsProvider
  48. * @see OpenStreetMapImageryProvider
  49. * @see SingleTileImageryProvider
  50. * @see TileMapServiceImageryProvider
  51. * @see WebMapServiceImageryProvider
  52. * @see WebMapTileServiceImageryProvider
  53. * @see UrlTemplateImageryProvider
  54. *
  55. *
  56. * @example
  57. * const bing = new Cesium.BingMapsImageryProvider({
  58. * url : 'https://dev.virtualearth.net',
  59. * key : 'get-yours-at-https://www.bingmapsportal.com/',
  60. * mapStyle : Cesium.BingMapsStyle.AERIAL
  61. * });
  62. *
  63. * @see {@link http://msdn.microsoft.com/en-us/library/ff701713.aspx|Bing Maps REST Services}
  64. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  65. */
  66. function BingMapsImageryProvider(options) {
  67. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  68. const accessKey = options.key;
  69. //>>includeStart('debug', pragmas.debug);
  70. if (!defined(options.url)) {
  71. throw new DeveloperError("options.url is required.");
  72. }
  73. if (!defined(accessKey)) {
  74. throw new DeveloperError("options.key is required.");
  75. }
  76. //>>includeEnd('debug');
  77. /**
  78. * The default alpha blending value of this provider, with 0.0 representing fully transparent and
  79. * 1.0 representing fully opaque.
  80. *
  81. * @type {Number|undefined}
  82. * @default undefined
  83. */
  84. this.defaultAlpha = undefined;
  85. /**
  86. * The default alpha blending value on the night side of the globe of this provider, with 0.0 representing fully transparent and
  87. * 1.0 representing fully opaque.
  88. *
  89. * @type {Number|undefined}
  90. * @default undefined
  91. */
  92. this.defaultNightAlpha = undefined;
  93. /**
  94. * The default alpha blending value on the day side of the globe of this provider, with 0.0 representing fully transparent and
  95. * 1.0 representing fully opaque.
  96. *
  97. * @type {Number|undefined}
  98. * @default undefined
  99. */
  100. this.defaultDayAlpha = undefined;
  101. /**
  102. * The default brightness of this provider. 1.0 uses the unmodified imagery color. Less than 1.0
  103. * makes the imagery darker while greater than 1.0 makes it brighter.
  104. *
  105. * @type {Number|undefined}
  106. * @default undefined
  107. */
  108. this.defaultBrightness = undefined;
  109. /**
  110. * The default contrast of this provider. 1.0 uses the unmodified imagery color. Less than 1.0 reduces
  111. * the contrast while greater than 1.0 increases it.
  112. *
  113. * @type {Number|undefined}
  114. * @default undefined
  115. */
  116. this.defaultContrast = undefined;
  117. /**
  118. * The default hue of this provider in radians. 0.0 uses the unmodified imagery color.
  119. *
  120. * @type {Number|undefined}
  121. * @default undefined
  122. */
  123. this.defaultHue = undefined;
  124. /**
  125. * The default saturation of this provider. 1.0 uses the unmodified imagery color. Less than 1.0 reduces the
  126. * saturation while greater than 1.0 increases it.
  127. *
  128. * @type {Number|undefined}
  129. * @default undefined
  130. */
  131. this.defaultSaturation = undefined;
  132. /**
  133. * The default gamma correction to apply to this provider. 1.0 uses the unmodified imagery color.
  134. *
  135. * @type {Number|undefined}
  136. * @default 1.0
  137. */
  138. this.defaultGamma = 1.0;
  139. /**
  140. * The default texture minification filter to apply to this provider.
  141. *
  142. * @type {TextureMinificationFilter}
  143. * @default undefined
  144. */
  145. this.defaultMinificationFilter = undefined;
  146. /**
  147. * The default texture magnification filter to apply to this provider.
  148. *
  149. * @type {TextureMagnificationFilter}
  150. * @default undefined
  151. */
  152. this.defaultMagnificationFilter = undefined;
  153. this._key = accessKey;
  154. this._resource = Resource.createIfNeeded(options.url);
  155. this._resource.appendForwardSlash();
  156. this._tileProtocol = options.tileProtocol;
  157. this._mapStyle = defaultValue(options.mapStyle, BingMapsStyle.AERIAL);
  158. this._culture = defaultValue(options.culture, "");
  159. this._tileDiscardPolicy = options.tileDiscardPolicy;
  160. if (!defined(this._tileDiscardPolicy)) {
  161. this._tileDiscardPolicy = new DiscardEmptyTilePolicy();
  162. }
  163. this._proxy = options.proxy;
  164. this._credit = new Credit(
  165. `<a href="http://www.bing.com"><img src="${BingMapsImageryProvider.logoUrl}" title="Bing Imagery"/></a>`
  166. );
  167. this._tilingScheme = new WebMercatorTilingScheme({
  168. numberOfLevelZeroTilesX: 2,
  169. numberOfLevelZeroTilesY: 2,
  170. ellipsoid: options.ellipsoid,
  171. });
  172. this._tileWidth = undefined;
  173. this._tileHeight = undefined;
  174. this._maximumLevel = undefined;
  175. this._imageUrlTemplate = undefined;
  176. this._imageUrlSubdomains = undefined;
  177. this._errorEvent = new Event();
  178. this._ready = false;
  179. this._readyPromise = defer();
  180. let tileProtocol = this._tileProtocol;
  181. // For backward compatibility reasons, the tileProtocol may end with
  182. // a `:`. Remove it.
  183. if (defined(tileProtocol)) {
  184. if (
  185. tileProtocol.length > 0 &&
  186. tileProtocol[tileProtocol.length - 1] === ":"
  187. ) {
  188. tileProtocol = tileProtocol.substr(0, tileProtocol.length - 1);
  189. }
  190. } else {
  191. // use http if the document's protocol is http, otherwise use https
  192. const documentProtocol = document.location.protocol;
  193. tileProtocol = documentProtocol === "http:" ? "http" : "https";
  194. }
  195. const metadataResource = this._resource.getDerivedResource({
  196. url: `REST/v1/Imagery/Metadata/${this._mapStyle}`,
  197. queryParameters: {
  198. incl: "ImageryProviders",
  199. key: this._key,
  200. uriScheme: tileProtocol,
  201. },
  202. });
  203. const that = this;
  204. let metadataError;
  205. function metadataSuccess(data) {
  206. if (data.resourceSets.length !== 1) {
  207. metadataFailure();
  208. return;
  209. }
  210. const resource = data.resourceSets[0].resources[0];
  211. that._tileWidth = resource.imageWidth;
  212. that._tileHeight = resource.imageHeight;
  213. that._maximumLevel = resource.zoomMax - 1;
  214. that._imageUrlSubdomains = resource.imageUrlSubdomains;
  215. that._imageUrlTemplate = resource.imageUrl;
  216. let attributionList = (that._attributionList = resource.imageryProviders);
  217. if (!attributionList) {
  218. attributionList = that._attributionList = [];
  219. }
  220. for (
  221. let attributionIndex = 0, attributionLength = attributionList.length;
  222. attributionIndex < attributionLength;
  223. ++attributionIndex
  224. ) {
  225. const attribution = attributionList[attributionIndex];
  226. if (attribution.credit instanceof Credit) {
  227. // If attribution.credit has already been created
  228. // then we are using a cached value, which means
  229. // none of the remaining processing needs to be done.
  230. break;
  231. }
  232. attribution.credit = new Credit(attribution.attribution);
  233. const coverageAreas = attribution.coverageAreas;
  234. for (
  235. let areaIndex = 0, areaLength = attribution.coverageAreas.length;
  236. areaIndex < areaLength;
  237. ++areaIndex
  238. ) {
  239. const area = coverageAreas[areaIndex];
  240. const bbox = area.bbox;
  241. area.bbox = new Rectangle(
  242. CesiumMath.toRadians(bbox[1]),
  243. CesiumMath.toRadians(bbox[0]),
  244. CesiumMath.toRadians(bbox[3]),
  245. CesiumMath.toRadians(bbox[2])
  246. );
  247. }
  248. }
  249. that._ready = true;
  250. that._readyPromise.resolve(true);
  251. TileProviderError.handleSuccess(metadataError);
  252. }
  253. function metadataFailure(e) {
  254. const message = `An error occurred while accessing ${metadataResource.url}.`;
  255. metadataError = TileProviderError.handleError(
  256. metadataError,
  257. that,
  258. that._errorEvent,
  259. message,
  260. undefined,
  261. undefined,
  262. undefined,
  263. requestMetadata
  264. );
  265. that._readyPromise.reject(new RuntimeError(message));
  266. }
  267. const cacheKey = metadataResource.url;
  268. function requestMetadata() {
  269. const promise = metadataResource.fetchJsonp("jsonp");
  270. BingMapsImageryProvider._metadataCache[cacheKey] = promise;
  271. promise.then(metadataSuccess).catch(metadataFailure);
  272. }
  273. const promise = BingMapsImageryProvider._metadataCache[cacheKey];
  274. if (defined(promise)) {
  275. promise.then(metadataSuccess).catch(metadataFailure);
  276. } else {
  277. requestMetadata();
  278. }
  279. }
  280. Object.defineProperties(BingMapsImageryProvider.prototype, {
  281. /**
  282. * Gets the name of the BingMaps server url hosting the imagery.
  283. * @memberof BingMapsImageryProvider.prototype
  284. * @type {String}
  285. * @readonly
  286. */
  287. url: {
  288. get: function () {
  289. return this._resource.url;
  290. },
  291. },
  292. /**
  293. * Gets the proxy used by this provider.
  294. * @memberof BingMapsImageryProvider.prototype
  295. * @type {Proxy}
  296. * @readonly
  297. */
  298. proxy: {
  299. get: function () {
  300. return this._resource.proxy;
  301. },
  302. },
  303. /**
  304. * Gets the Bing Maps key.
  305. * @memberof BingMapsImageryProvider.prototype
  306. * @type {String}
  307. * @readonly
  308. */
  309. key: {
  310. get: function () {
  311. return this._key;
  312. },
  313. },
  314. /**
  315. * Gets the type of Bing Maps imagery to load.
  316. * @memberof BingMapsImageryProvider.prototype
  317. * @type {BingMapsStyle}
  318. * @readonly
  319. */
  320. mapStyle: {
  321. get: function () {
  322. return this._mapStyle;
  323. },
  324. },
  325. /**
  326. * The culture to use when requesting Bing Maps imagery. Not
  327. * all cultures are supported. See {@link http://msdn.microsoft.com/en-us/library/hh441729.aspx}
  328. * for information on the supported cultures.
  329. * @memberof BingMapsImageryProvider.prototype
  330. * @type {String}
  331. * @readonly
  332. */
  333. culture: {
  334. get: function () {
  335. return this._culture;
  336. },
  337. },
  338. /**
  339. * Gets the width of each tile, in pixels. This function should
  340. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  341. * @memberof BingMapsImageryProvider.prototype
  342. * @type {Number}
  343. * @readonly
  344. */
  345. tileWidth: {
  346. get: function () {
  347. //>>includeStart('debug', pragmas.debug);
  348. if (!this._ready) {
  349. throw new DeveloperError(
  350. "tileWidth must not be called before the imagery provider is ready."
  351. );
  352. }
  353. //>>includeEnd('debug');
  354. return this._tileWidth;
  355. },
  356. },
  357. /**
  358. * Gets the height of each tile, in pixels. This function should
  359. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  360. * @memberof BingMapsImageryProvider.prototype
  361. * @type {Number}
  362. * @readonly
  363. */
  364. tileHeight: {
  365. get: function () {
  366. //>>includeStart('debug', pragmas.debug);
  367. if (!this._ready) {
  368. throw new DeveloperError(
  369. "tileHeight must not be called before the imagery provider is ready."
  370. );
  371. }
  372. //>>includeEnd('debug');
  373. return this._tileHeight;
  374. },
  375. },
  376. /**
  377. * Gets the maximum level-of-detail that can be requested. This function should
  378. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  379. * @memberof BingMapsImageryProvider.prototype
  380. * @type {Number|undefined}
  381. * @readonly
  382. */
  383. maximumLevel: {
  384. get: function () {
  385. //>>includeStart('debug', pragmas.debug);
  386. if (!this._ready) {
  387. throw new DeveloperError(
  388. "maximumLevel must not be called before the imagery provider is ready."
  389. );
  390. }
  391. //>>includeEnd('debug');
  392. return this._maximumLevel;
  393. },
  394. },
  395. /**
  396. * Gets the minimum level-of-detail that can be requested. This function should
  397. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  398. * @memberof BingMapsImageryProvider.prototype
  399. * @type {Number}
  400. * @readonly
  401. */
  402. minimumLevel: {
  403. get: function () {
  404. //>>includeStart('debug', pragmas.debug);
  405. if (!this._ready) {
  406. throw new DeveloperError(
  407. "minimumLevel must not be called before the imagery provider is ready."
  408. );
  409. }
  410. //>>includeEnd('debug');
  411. return 0;
  412. },
  413. },
  414. /**
  415. * Gets the tiling scheme used by this provider. This function should
  416. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  417. * @memberof BingMapsImageryProvider.prototype
  418. * @type {TilingScheme}
  419. * @readonly
  420. */
  421. tilingScheme: {
  422. get: function () {
  423. //>>includeStart('debug', pragmas.debug);
  424. if (!this._ready) {
  425. throw new DeveloperError(
  426. "tilingScheme must not be called before the imagery provider is ready."
  427. );
  428. }
  429. //>>includeEnd('debug');
  430. return this._tilingScheme;
  431. },
  432. },
  433. /**
  434. * Gets the rectangle, in radians, of the imagery provided by this instance. This function should
  435. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  436. * @memberof BingMapsImageryProvider.prototype
  437. * @type {Rectangle}
  438. * @readonly
  439. */
  440. rectangle: {
  441. get: function () {
  442. //>>includeStart('debug', pragmas.debug);
  443. if (!this._ready) {
  444. throw new DeveloperError(
  445. "rectangle must not be called before the imagery provider is ready."
  446. );
  447. }
  448. //>>includeEnd('debug');
  449. return this._tilingScheme.rectangle;
  450. },
  451. },
  452. /**
  453. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  454. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  455. * returns undefined, no tiles are filtered. This function should
  456. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  457. * @memberof BingMapsImageryProvider.prototype
  458. * @type {TileDiscardPolicy}
  459. * @readonly
  460. */
  461. tileDiscardPolicy: {
  462. get: function () {
  463. //>>includeStart('debug', pragmas.debug);
  464. if (!this._ready) {
  465. throw new DeveloperError(
  466. "tileDiscardPolicy must not be called before the imagery provider is ready."
  467. );
  468. }
  469. //>>includeEnd('debug');
  470. return this._tileDiscardPolicy;
  471. },
  472. },
  473. /**
  474. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  475. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  476. * are passed an instance of {@link TileProviderError}.
  477. * @memberof BingMapsImageryProvider.prototype
  478. * @type {Event}
  479. * @readonly
  480. */
  481. errorEvent: {
  482. get: function () {
  483. return this._errorEvent;
  484. },
  485. },
  486. /**
  487. * Gets a value indicating whether or not the provider is ready for use.
  488. * @memberof BingMapsImageryProvider.prototype
  489. * @type {Boolean}
  490. * @readonly
  491. */
  492. ready: {
  493. get: function () {
  494. return this._ready;
  495. },
  496. },
  497. /**
  498. * Gets a promise that resolves to true when the provider is ready for use.
  499. * @memberof BingMapsImageryProvider.prototype
  500. * @type {Promise.<Boolean>}
  501. * @readonly
  502. */
  503. readyPromise: {
  504. get: function () {
  505. return this._readyPromise.promise;
  506. },
  507. },
  508. /**
  509. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  510. * the source of the imagery. This function should not be called before {@link BingMapsImageryProvider#ready} returns true.
  511. * @memberof BingMapsImageryProvider.prototype
  512. * @type {Credit}
  513. * @readonly
  514. */
  515. credit: {
  516. get: function () {
  517. return this._credit;
  518. },
  519. },
  520. /**
  521. * Gets a value indicating whether or not the images provided by this imagery provider
  522. * include an alpha channel. If this property is false, an alpha channel, if present, will
  523. * be ignored. If this property is true, any images without an alpha channel will be treated
  524. * as if their alpha is 1.0 everywhere. Setting this property to false reduces memory usage
  525. * and texture upload time.
  526. * @memberof BingMapsImageryProvider.prototype
  527. * @type {Boolean}
  528. * @readonly
  529. */
  530. hasAlphaChannel: {
  531. get: function () {
  532. return false;
  533. },
  534. },
  535. });
  536. const rectangleScratch = new Rectangle();
  537. /**
  538. * Gets the credits to be displayed when a given tile is displayed.
  539. *
  540. * @param {Number} x The tile X coordinate.
  541. * @param {Number} y The tile Y coordinate.
  542. * @param {Number} level The tile level;
  543. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  544. *
  545. * @exception {DeveloperError} <code>getTileCredits</code> must not be called before the imagery provider is ready.
  546. */
  547. BingMapsImageryProvider.prototype.getTileCredits = function (x, y, level) {
  548. //>>includeStart('debug', pragmas.debug);
  549. if (!this._ready) {
  550. throw new DeveloperError(
  551. "getTileCredits must not be called before the imagery provider is ready."
  552. );
  553. }
  554. //>>includeEnd('debug');
  555. const rectangle = this._tilingScheme.tileXYToRectangle(
  556. x,
  557. y,
  558. level,
  559. rectangleScratch
  560. );
  561. const result = getRectangleAttribution(
  562. this._attributionList,
  563. level,
  564. rectangle
  565. );
  566. return result;
  567. };
  568. /**
  569. * Requests the image for a given tile. This function should
  570. * not be called before {@link BingMapsImageryProvider#ready} returns true.
  571. *
  572. * @param {Number} x The tile X coordinate.
  573. * @param {Number} y The tile Y coordinate.
  574. * @param {Number} level The tile level.
  575. * @param {Request} [request] The request object. Intended for internal use only.
  576. * @returns {Promise.<ImageryTypes>|undefined} A promise for the image that will resolve when the image is available, or
  577. * undefined if there are too many active requests to the server, and the request should be retried later.
  578. *
  579. * @exception {DeveloperError} <code>requestImage</code> must not be called before the imagery provider is ready.
  580. */
  581. BingMapsImageryProvider.prototype.requestImage = function (
  582. x,
  583. y,
  584. level,
  585. request
  586. ) {
  587. //>>includeStart('debug', pragmas.debug);
  588. if (!this._ready) {
  589. throw new DeveloperError(
  590. "requestImage must not be called before the imagery provider is ready."
  591. );
  592. }
  593. //>>includeEnd('debug');
  594. const promise = ImageryProvider.loadImage(
  595. this,
  596. buildImageResource(this, x, y, level, request)
  597. );
  598. if (defined(promise)) {
  599. return promise.catch(function (error) {
  600. // One cause of an error here is that the image we tried to load was zero-length.
  601. // This isn't actually a problem, since it indicates that there is no tile.
  602. // So, in that case we return the EMPTY_IMAGE sentinel value for later discarding.
  603. if (defined(error.blob) && error.blob.size === 0) {
  604. return DiscardEmptyTilePolicy.EMPTY_IMAGE;
  605. }
  606. return Promise.reject(error);
  607. });
  608. }
  609. return undefined;
  610. };
  611. /**
  612. * Picking features is not currently supported by this imagery provider, so this function simply returns
  613. * undefined.
  614. *
  615. * @param {Number} x The tile X coordinate.
  616. * @param {Number} y The tile Y coordinate.
  617. * @param {Number} level The tile level.
  618. * @param {Number} longitude The longitude at which to pick features.
  619. * @param {Number} latitude The latitude at which to pick features.
  620. * @return {undefined} Undefined since picking is not supported.
  621. */
  622. BingMapsImageryProvider.prototype.pickFeatures = function (
  623. x,
  624. y,
  625. level,
  626. longitude,
  627. latitude
  628. ) {
  629. return undefined;
  630. };
  631. /**
  632. * Converts a tiles (x, y, level) position into a quadkey used to request an image
  633. * from a Bing Maps server.
  634. *
  635. * @param {Number} x The tile's x coordinate.
  636. * @param {Number} y The tile's y coordinate.
  637. * @param {Number} level The tile's zoom level.
  638. *
  639. * @see {@link http://msdn.microsoft.com/en-us/library/bb259689.aspx|Bing Maps Tile System}
  640. * @see BingMapsImageryProvider#quadKeyToTileXY
  641. */
  642. BingMapsImageryProvider.tileXYToQuadKey = function (x, y, level) {
  643. let quadkey = "";
  644. for (let i = level; i >= 0; --i) {
  645. const bitmask = 1 << i;
  646. let digit = 0;
  647. if ((x & bitmask) !== 0) {
  648. digit |= 1;
  649. }
  650. if ((y & bitmask) !== 0) {
  651. digit |= 2;
  652. }
  653. quadkey += digit;
  654. }
  655. return quadkey;
  656. };
  657. /**
  658. * Converts a tile's quadkey used to request an image from a Bing Maps server into the
  659. * (x, y, level) position.
  660. *
  661. * @param {String} quadkey The tile's quad key
  662. *
  663. * @see {@link http://msdn.microsoft.com/en-us/library/bb259689.aspx|Bing Maps Tile System}
  664. * @see BingMapsImageryProvider#tileXYToQuadKey
  665. */
  666. BingMapsImageryProvider.quadKeyToTileXY = function (quadkey) {
  667. let x = 0;
  668. let y = 0;
  669. const level = quadkey.length - 1;
  670. for (let i = level; i >= 0; --i) {
  671. const bitmask = 1 << i;
  672. const digit = +quadkey[level - i];
  673. if ((digit & 1) !== 0) {
  674. x |= bitmask;
  675. }
  676. if ((digit & 2) !== 0) {
  677. y |= bitmask;
  678. }
  679. }
  680. return {
  681. x: x,
  682. y: y,
  683. level: level,
  684. };
  685. };
  686. BingMapsImageryProvider._logoUrl = undefined;
  687. Object.defineProperties(BingMapsImageryProvider, {
  688. /**
  689. * Gets or sets the URL to the Bing logo for display in the credit.
  690. * @memberof BingMapsImageryProvider
  691. * @type {String}
  692. */
  693. logoUrl: {
  694. get: function () {
  695. if (!defined(BingMapsImageryProvider._logoUrl)) {
  696. BingMapsImageryProvider._logoUrl = buildModuleUrl(
  697. "Assets/Images/bing_maps_credit.png"
  698. );
  699. }
  700. return BingMapsImageryProvider._logoUrl;
  701. },
  702. set: function (value) {
  703. //>>includeStart('debug', pragmas.debug);
  704. Check.defined("value", value);
  705. //>>includeEnd('debug');
  706. BingMapsImageryProvider._logoUrl = value;
  707. },
  708. },
  709. });
  710. function buildImageResource(imageryProvider, x, y, level, request) {
  711. const imageUrl = imageryProvider._imageUrlTemplate;
  712. const subdomains = imageryProvider._imageUrlSubdomains;
  713. const subdomainIndex = (x + y + level) % subdomains.length;
  714. return imageryProvider._resource.getDerivedResource({
  715. url: imageUrl,
  716. request: request,
  717. templateValues: {
  718. quadkey: BingMapsImageryProvider.tileXYToQuadKey(x, y, level),
  719. subdomain: subdomains[subdomainIndex],
  720. culture: imageryProvider._culture,
  721. },
  722. queryParameters: {
  723. // this parameter tells the Bing servers to send a zero-length response
  724. // instead of a placeholder image for missing tiles.
  725. n: "z",
  726. },
  727. });
  728. }
  729. const intersectionScratch = new Rectangle();
  730. function getRectangleAttribution(attributionList, level, rectangle) {
  731. // Bing levels start at 1, while ours start at 0.
  732. ++level;
  733. const result = [];
  734. for (
  735. let attributionIndex = 0, attributionLength = attributionList.length;
  736. attributionIndex < attributionLength;
  737. ++attributionIndex
  738. ) {
  739. const attribution = attributionList[attributionIndex];
  740. const coverageAreas = attribution.coverageAreas;
  741. let included = false;
  742. for (
  743. let areaIndex = 0, areaLength = attribution.coverageAreas.length;
  744. !included && areaIndex < areaLength;
  745. ++areaIndex
  746. ) {
  747. const area = coverageAreas[areaIndex];
  748. if (level >= area.zoomMin && level <= area.zoomMax) {
  749. const intersection = Rectangle.intersection(
  750. rectangle,
  751. area.bbox,
  752. intersectionScratch
  753. );
  754. if (defined(intersection)) {
  755. included = true;
  756. }
  757. }
  758. }
  759. if (included) {
  760. result.push(attribution.credit);
  761. }
  762. }
  763. return result;
  764. }
  765. // Exposed for testing
  766. BingMapsImageryProvider._metadataCache = {};
  767. export default BingMapsImageryProvider;