智慧水务管理系统 - 精河县供水工程综合管理平台

Azure2DImageryProvider.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import Check from "../Core/Check.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import Credit from "../Core/Credit.js";
  4. import defined from "../Core/defined.js";
  5. import Resource from "../Core/Resource.js";
  6. import IonResource from "../Core/IonResource.js";
  7. import UrlTemplateImageryProvider from "./UrlTemplateImageryProvider.js";
  8. const trailingSlashRegex = /\/$/;
  9. /**
  10. * @typedef {object} Azure2DImageryProvider.ConstructorOptions
  11. *
  12. * Initialization options for the Azure2DImageryProvider constructor
  13. *
  14. * @property {string} subscriptionKey The public subscription key for the imagery.
  15. * @property {string} [url="https://atlas.microsoft.com/"] The Azure server url.
  16. * @property {string} [tilesetId="microsoft.imagery"] The Azure tileset ID. Valid options are {@link microsoft.imagery}, {@link microsoft.base.road}, and {@link microsoft.base.labels.road}
  17. * @property {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid. If not specified, the default ellipsoid is used.
  18. * @property {number} [minimumLevel=0] The minimum level-of-detail supported by the imagery provider. Take care when specifying
  19. * this that the number of tiles at the minimum level is small, such as four or less. A larger number is likely
  20. * to result in rendering problems.
  21. * @property {number} [maximumLevel=22] The maximum level-of-detail supported by the imagery provider.
  22. * @property {Rectangle} [rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
  23. */
  24. /**
  25. * Provides 2D image tiles from Azure.
  26. *
  27. * @alias Azure2DImageryProvider
  28. * @constructor
  29. * @param {Azure2DImageryProvider.ConstructorOptions} options Object describing initialization options
  30. *
  31. * @example
  32. * // Azure 2D imagery provider
  33. * const azureImageryProvider = new Cesium.Azure2DImageryProvider({
  34. * subscriptionKey: "subscription-key",
  35. * tilesetId: "microsoft.base.road"
  36. * });
  37. */
  38. function Azure2DImageryProvider(options) {
  39. options = options ?? {};
  40. const tilesetId = options.tilesetId ?? "microsoft.imagery";
  41. this._maximumLevel = options.maximumLevel ?? 22;
  42. this._minimumLevel = options.minimumLevel ?? 0;
  43. this._subscriptionKey =
  44. options.subscriptionKey ?? options["subscription-key"];
  45. //>>includeStart('debug', pragmas.debug);
  46. Check.defined("options.subscriptionKey", this._subscriptionKey);
  47. //>>includeEnd('debug');
  48. this._tilesetId = options.tilesetId;
  49. const resource =
  50. options.url instanceof IonResource
  51. ? options.url
  52. : Resource.createIfNeeded(options.url ?? "https://atlas.microsoft.com/");
  53. let templateUrl = resource.getUrlComponent();
  54. if (!trailingSlashRegex.test(templateUrl)) {
  55. templateUrl += "/";
  56. }
  57. const tilesUrl = `${templateUrl}map/tile`;
  58. this._viewportUrl = `${templateUrl}map/attribution`;
  59. resource.url = tilesUrl;
  60. resource.setQueryParameters({
  61. "api-version": "2024-04-01",
  62. tilesetId: tilesetId,
  63. "subscription-key": this._subscriptionKey,
  64. zoom: `{z}`,
  65. x: `{x}`,
  66. y: `{y}`,
  67. });
  68. this._resource = resource;
  69. let credit;
  70. if (defined(options.credit)) {
  71. credit = options.credit;
  72. if (typeof credit === "string") {
  73. credit = new Credit(credit);
  74. }
  75. }
  76. const provider = new UrlTemplateImageryProvider({
  77. ...options,
  78. maximumLevel: this._maximumLevel,
  79. minimumLevel: this._minimumLevel,
  80. url: resource,
  81. credit: credit,
  82. });
  83. provider._resource = resource;
  84. this._imageryProvider = provider;
  85. // This will be defined for ion resources
  86. this._tileCredits = resource.credits;
  87. this._attributionsByLevel = undefined;
  88. }
  89. Object.defineProperties(Azure2DImageryProvider.prototype, {
  90. /**
  91. * Gets the URL of the Azure 2D Imagery server.
  92. * @memberof Azure2DImageryProvider.prototype
  93. * @type {string}
  94. * @readonly
  95. */
  96. url: {
  97. get: function () {
  98. return this._imageryProvider.url;
  99. },
  100. },
  101. /**
  102. * Gets the rectangle, in radians, of the imagery provided by the instance.
  103. * @memberof Azure2DImageryProvider.prototype
  104. * @type {Rectangle}
  105. * @readonly
  106. */
  107. rectangle: {
  108. get: function () {
  109. return this._imageryProvider.rectangle;
  110. },
  111. },
  112. /**
  113. * Gets the width of each tile, in pixels.
  114. * @memberof Azure2DImageryProvider.prototype
  115. * @type {number}
  116. * @readonly
  117. */
  118. tileWidth: {
  119. get: function () {
  120. return this._imageryProvider.tileWidth;
  121. },
  122. },
  123. /**
  124. * Gets the height of each tile, in pixels.
  125. * @memberof Azure2DImageryProvider.prototype
  126. * @type {number}
  127. * @readonly
  128. */
  129. tileHeight: {
  130. get: function () {
  131. return this._imageryProvider.tileHeight;
  132. },
  133. },
  134. /**
  135. * Gets the maximum level-of-detail that can be requested.
  136. * @memberof Azure2DImageryProvider.prototype
  137. * @type {number|undefined}
  138. * @readonly
  139. */
  140. maximumLevel: {
  141. get: function () {
  142. return this._imageryProvider.maximumLevel;
  143. },
  144. },
  145. /**
  146. * Gets the minimum level-of-detail that can be requested. Generally,
  147. * a minimum level should only be used when the rectangle of the imagery is small
  148. * enough that the number of tiles at the minimum level is small. An imagery
  149. * provider with more than a few tiles at the minimum level will lead to
  150. * rendering problems.
  151. * @memberof Azure2DImageryProvider.prototype
  152. * @type {number}
  153. * @readonly
  154. */
  155. minimumLevel: {
  156. get: function () {
  157. return this._imageryProvider.minimumLevel;
  158. },
  159. },
  160. /**
  161. * Gets the tiling scheme used by the provider.
  162. * @memberof Azure2DImageryProvider.prototype
  163. * @type {TilingScheme}
  164. * @readonly
  165. */
  166. tilingScheme: {
  167. get: function () {
  168. return this._imageryProvider.tilingScheme;
  169. },
  170. },
  171. /**
  172. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  173. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  174. * returns undefined, no tiles are filtered.
  175. * @memberof Azure2DImageryProvider.prototype
  176. * @type {TileDiscardPolicy}
  177. * @readonly
  178. */
  179. tileDiscardPolicy: {
  180. get: function () {
  181. return this._imageryProvider.tileDiscardPolicy;
  182. },
  183. },
  184. /**
  185. * Gets an event that is raised when the imagery provider encounters an asynchronous error.. By subscribing
  186. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  187. * are passed an instance of {@link TileProviderError}.
  188. * @memberof Azure2DImageryProvider.prototype
  189. * @type {Event}
  190. * @readonly
  191. */
  192. errorEvent: {
  193. get: function () {
  194. return this._imageryProvider.errorEvent;
  195. },
  196. },
  197. /**
  198. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  199. * the source of the imagery.
  200. * @memberof Azure2DImageryProvider.prototype
  201. * @type {Credit}
  202. * @readonly
  203. */
  204. credit: {
  205. get: function () {
  206. return this._imageryProvider.credit;
  207. },
  208. },
  209. /**
  210. * Gets the proxy used by this provider.
  211. * @memberof Azure2DImageryProvider.prototype
  212. * @type {Proxy}
  213. * @readonly
  214. */
  215. proxy: {
  216. get: function () {
  217. return this._imageryProvider.proxy;
  218. },
  219. },
  220. /**
  221. * Gets a value indicating whether or not the images provided by this imagery provider
  222. * include an alpha channel. If this property is false, an alpha channel, if present, will
  223. * be ignored. If this property is true, any images without an alpha channel will be treated
  224. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  225. * and texture upload time are reduced.
  226. * @memberof Azure2DImageryProvider.prototype
  227. * @type {boolean}
  228. * @readonly
  229. */
  230. hasAlphaChannel: {
  231. get: function () {
  232. return this._imageryProvider.hasAlphaChannel;
  233. },
  234. },
  235. });
  236. /**
  237. * Gets the credits to be displayed when a given tile is displayed.
  238. *
  239. * @param {number} x The tile X coordinate.
  240. * @param {number} y The tile Y coordinate.
  241. * @param {number} level The tile level;
  242. * @returns {Credit[]|undefined} The credits to be displayed when the tile is displayed.
  243. */
  244. Azure2DImageryProvider.prototype.getTileCredits = function (x, y, level) {
  245. const hasAttributions = defined(this._attributionsByLevel);
  246. if (!hasAttributions || !defined(this._tileCredits)) {
  247. return undefined;
  248. }
  249. const innerCredits = this._attributionsByLevel.get(level);
  250. if (!defined(this._tileCredits)) {
  251. return innerCredits;
  252. }
  253. return this._tileCredits.concat(innerCredits);
  254. };
  255. /**
  256. * Requests the image for a given tile.
  257. *
  258. * @param {number} x The tile X coordinate.
  259. * @param {number} y The tile Y coordinate.
  260. * @param {number} level The tile level.
  261. * @param {Request} [request] The request object. Intended for internal use only.
  262. * @returns {Promise<ImageryTypes>|undefined} A promise for the image that will resolve when the image is available, or
  263. * undefined if there are too many active requests to the server, and the request should be retried later.
  264. */
  265. Azure2DImageryProvider.prototype.requestImage = function (
  266. x,
  267. y,
  268. level,
  269. request,
  270. ) {
  271. const promise = this._imageryProvider.requestImage(x, y, level, request);
  272. // If the requestImage call returns undefined, it couldn't be scheduled this frame. Make sure to return undefined so this can be handled upstream.
  273. if (!defined(promise)) {
  274. return undefined;
  275. }
  276. // Asynchronously request and populate _attributionsByLevel if it hasn't been already. We do this here so that the promise can be properly awaited.
  277. if (!defined(this._attributionsByLevel)) {
  278. return Promise.all([promise, this.getViewportCredits()]).then(
  279. (results) => results[0],
  280. );
  281. }
  282. return promise;
  283. };
  284. /**
  285. * Picking features is not currently supported by this imagery provider, so this function simply returns
  286. * undefined.
  287. *
  288. * @param {number} x The tile X coordinate.
  289. * @param {number} y The tile Y coordinate.
  290. * @param {number} level The tile level.
  291. * @param {number} longitude The longitude at which to pick features.
  292. * @param {number} latitude The latitude at which to pick features.
  293. * @return {undefined} Undefined since picking is not supported.
  294. */
  295. Azure2DImageryProvider.prototype.pickFeatures = function (
  296. x,
  297. y,
  298. level,
  299. longitude,
  300. latitude,
  301. ) {
  302. return undefined;
  303. };
  304. /**
  305. * Get attribution for imagery from Azure Maps to display in the credits
  306. * @private
  307. * @return {Promise<Map<Credit[]>>} The list of attribution sources to display in the credits.
  308. */
  309. Azure2DImageryProvider.prototype.getViewportCredits = async function () {
  310. const maximumLevel = this._maximumLevel;
  311. const promises = [];
  312. for (let level = 0; level < maximumLevel + 1; level++) {
  313. promises.push(
  314. fetchViewportAttribution(
  315. this._resource,
  316. this._viewportUrl,
  317. this._subscriptionKey,
  318. this._tilesetId,
  319. level,
  320. ),
  321. );
  322. }
  323. const results = await Promise.all(promises);
  324. const attributionsByLevel = new Map();
  325. for (let level = 0; level < maximumLevel + 1; level++) {
  326. const credits = [];
  327. const attributions = results[level].join(",");
  328. if (attributions) {
  329. const levelCredits = new Credit(attributions);
  330. credits.push(levelCredits);
  331. }
  332. attributionsByLevel.set(level, credits);
  333. }
  334. this._attributionsByLevel = attributionsByLevel;
  335. return attributionsByLevel;
  336. };
  337. async function fetchViewportAttribution(resource, url, key, tilesetId, level) {
  338. const viewportResource = resource.getDerivedResource({
  339. url,
  340. queryParameters: {
  341. zoom: level,
  342. bounds: "-180,-90,180,90",
  343. },
  344. data: JSON.stringify(Frozen.EMPTY_OBJECT),
  345. });
  346. const viewportJson = await viewportResource.fetchJson();
  347. return viewportJson.copyrights;
  348. }
  349. // Exposed for tests
  350. export default Azure2DImageryProvider;