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

IonImageryProvider.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import Check from "../Core/Check.js";
  2. import clone from "../Core/clone.js";
  3. import Frozen from "../Core/Frozen.js";
  4. import defined from "../Core/defined.js";
  5. import Event from "../Core/Event.js";
  6. import IonResource from "../Core/IonResource.js";
  7. import RuntimeError from "../Core/RuntimeError.js";
  8. import IonImageryProviderFactory from "./IonImageryProviderFactory.js";
  9. /**
  10. * @typedef {object} IonImageryProvider.ConstructorOptions
  11. *
  12. * Initialization options for the TileMapServiceImageryProvider constructor
  13. *
  14. * @property {string} [accessToken=Ion.defaultAccessToken] The access token to use.
  15. * @property {string|Resource} [server=Ion.defaultServer] The resource to the Cesium ion API server.
  16. */
  17. /**
  18. * <div class="notice">
  19. * To construct a IonImageryProvider, call {@link IonImageryProvider.fromAssetId}. Do not call the constructor directly.
  20. * </div>
  21. *
  22. * Provides tiled imagery using the Cesium ion REST API.
  23. *
  24. * @alias IonImageryProvider
  25. * @constructor
  26. *
  27. * @param {IonImageryProvider.ConstructorOptions} [options] Object describing initialization options
  28. *
  29. * @example
  30. * const imageryLayer = Cesium.ImageryLayer.fromProviderAsync(Cesium.IonImageryProvider.fromAssetId(3812));
  31. * viewer.imageryLayers.add(imageryLayer);
  32. *
  33. * @see IonImageryProvider.fromAssetId
  34. */
  35. function IonImageryProvider(options) {
  36. options = options ?? Frozen.EMPTY_OBJECT;
  37. this._defaultAlpha = undefined;
  38. this._defaultNightAlpha = undefined;
  39. this._defaultDayAlpha = undefined;
  40. this._defaultBrightness = undefined;
  41. this._defaultContrast = undefined;
  42. this._defaultHue = undefined;
  43. this._defaultSaturation = undefined;
  44. this._defaultGamma = undefined;
  45. this._defaultMinificationFilter = undefined;
  46. this._defaultMagnificationFilter = undefined;
  47. this._tileCredits = undefined;
  48. this._errorEvent = new Event();
  49. }
  50. Object.defineProperties(IonImageryProvider.prototype, {
  51. /**
  52. * Gets the rectangle, in radians, of the imagery provided by the instance.
  53. * @memberof IonImageryProvider.prototype
  54. * @type {Rectangle}
  55. * @readonly
  56. */
  57. rectangle: {
  58. get: function () {
  59. return this._imageryProvider.rectangle;
  60. },
  61. },
  62. /**
  63. * Gets the width of each tile, in pixels.
  64. * @memberof IonImageryProvider.prototype
  65. * @type {number}
  66. * @readonly
  67. */
  68. tileWidth: {
  69. get: function () {
  70. return this._imageryProvider.tileWidth;
  71. },
  72. },
  73. /**
  74. * Gets the height of each tile, in pixels.
  75. * @memberof IonImageryProvider.prototype
  76. * @type {number}
  77. * @readonly
  78. */
  79. tileHeight: {
  80. get: function () {
  81. return this._imageryProvider.tileHeight;
  82. },
  83. },
  84. /**
  85. * Gets the maximum level-of-detail that can be requested.
  86. * @memberof IonImageryProvider.prototype
  87. * @type {number|undefined}
  88. * @readonly
  89. */
  90. maximumLevel: {
  91. get: function () {
  92. return this._imageryProvider.maximumLevel;
  93. },
  94. },
  95. /**
  96. * Gets the minimum level-of-detail that can be requested. Generally,
  97. * a minimum level should only be used when the rectangle of the imagery is small
  98. * enough that the number of tiles at the minimum level is small. An imagery
  99. * provider with more than a few tiles at the minimum level will lead to
  100. * rendering problems.
  101. * @memberof IonImageryProvider.prototype
  102. * @type {number}
  103. * @readonly
  104. */
  105. minimumLevel: {
  106. get: function () {
  107. return this._imageryProvider.minimumLevel;
  108. },
  109. },
  110. /**
  111. * Gets the tiling scheme used by the provider.
  112. * @memberof IonImageryProvider.prototype
  113. * @type {TilingScheme}
  114. * @readonly
  115. */
  116. tilingScheme: {
  117. get: function () {
  118. return this._imageryProvider.tilingScheme;
  119. },
  120. },
  121. /**
  122. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  123. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  124. * returns undefined, no tiles are filtered.
  125. * @memberof IonImageryProvider.prototype
  126. * @type {TileDiscardPolicy}
  127. * @readonly
  128. */
  129. tileDiscardPolicy: {
  130. get: function () {
  131. return this._imageryProvider.tileDiscardPolicy;
  132. },
  133. },
  134. /**
  135. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  136. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  137. * are passed an instance of {@link TileProviderError}.
  138. * @memberof IonImageryProvider.prototype
  139. * @type {Event}
  140. * @readonly
  141. */
  142. errorEvent: {
  143. get: function () {
  144. return this._errorEvent;
  145. },
  146. },
  147. /**
  148. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  149. * the source of the imagery.
  150. * @memberof IonImageryProvider.prototype
  151. * @type {Credit}
  152. * @readonly
  153. */
  154. credit: {
  155. get: function () {
  156. return this._imageryProvider.credit;
  157. },
  158. },
  159. /**
  160. * Gets a value indicating whether or not the images provided by this imagery provider
  161. * include an alpha channel. If this property is false, an alpha channel, if present, will
  162. * be ignored. If this property is true, any images without an alpha channel will be treated
  163. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  164. * and texture upload time are reduced.
  165. * @memberof IonImageryProvider.prototype
  166. * @type {boolean}
  167. * @readonly
  168. */
  169. hasAlphaChannel: {
  170. get: function () {
  171. return this._imageryProvider.hasAlphaChannel;
  172. },
  173. },
  174. /**
  175. * Gets the proxy used by this provider.
  176. * @memberof IonImageryProvider.prototype
  177. * @type {Proxy}
  178. * @readonly
  179. * @default undefined
  180. */
  181. proxy: {
  182. get: function () {
  183. return undefined;
  184. },
  185. },
  186. });
  187. /**
  188. * Creates a provider for tiled imagery using the Cesium ion REST API.
  189. *
  190. * @param {number} assetId An ion imagery asset ID.
  191. * @param {IonImageryProvider.ConstructorOptions} [options] Object describing initialization options.
  192. * @returns {Promise<IonImageryProvider>} A promise which resolves to the created IonImageryProvider.
  193. *
  194. * @example
  195. * const imageryLayer = Cesium.ImageryLayer.fromProviderAsync(Cesium.IonImageryProvider.fromAssetId(3812));
  196. * viewer.imageryLayers.add(imageryLayer);
  197. *
  198. * @exception {RuntimeError} Cesium ion assetId is not an imagery asset
  199. * @exception {RuntimeError} Unrecognized Cesium ion imagery type
  200. */
  201. IonImageryProvider.fromAssetId = async function (assetId, options) {
  202. //>>includeStart('debug', pragmas.debug);
  203. Check.typeOf.number("assetId", assetId);
  204. //>>includeEnd('debug');
  205. options = options ?? Frozen.EMPTY_OBJECT;
  206. const endpointResource = IonResource._createEndpointResource(
  207. assetId,
  208. options,
  209. );
  210. // A simple cache to avoid making repeated requests to ion for endpoints we've
  211. // already retrieved. This exists mainly to support Bing caching to reduce
  212. // world imagery sessions, but provides a small boost of performance in general
  213. // if constantly reloading assets
  214. const cacheKey = assetId.toString() + options.accessToken + options.server;
  215. let promise = IonImageryProvider._endpointCache[cacheKey];
  216. if (!defined(promise)) {
  217. promise = endpointResource.fetchJson();
  218. IonImageryProvider._endpointCache[cacheKey] = promise;
  219. }
  220. let endpoint = await promise;
  221. if (endpoint.type !== "IMAGERY") {
  222. throw new RuntimeError(
  223. `Cesium ion asset ${assetId} is not an imagery asset.`,
  224. );
  225. }
  226. const externalType = endpoint.externalType;
  227. let factory = IonImageryProviderFactory.defaultFactoryCallback;
  228. // Make a copy before editing since this object reference is cached;
  229. endpoint = clone(endpoint, true);
  230. endpoint.options = endpoint.options ?? {};
  231. const url = endpoint.options?.url;
  232. delete options.url;
  233. if (defined(externalType)) {
  234. factory = IonImageryProviderFactory[externalType];
  235. if (!defined(factory)) {
  236. throw new RuntimeError(
  237. `Unrecognized Cesium ion imagery type: ${externalType}`,
  238. );
  239. }
  240. }
  241. const imageryProvider = await factory(url, endpoint, endpointResource);
  242. const provider = new IonImageryProvider(options);
  243. imageryProvider.errorEvent.addEventListener(function (tileProviderError) {
  244. //Propagate the errorEvent but set the provider to this instance instead
  245. //of the inner instance.
  246. tileProviderError.provider = provider;
  247. provider._errorEvent.raiseEvent(tileProviderError);
  248. });
  249. provider._tileCredits = IonResource.getCreditsFromEndpoint(
  250. endpoint,
  251. endpointResource,
  252. );
  253. provider._imageryProvider = imageryProvider;
  254. return provider;
  255. };
  256. /**
  257. * Gets the credits to be displayed when a given tile is displayed.
  258. * @function
  259. *
  260. * @param {number} x The tile X coordinate.
  261. * @param {number} y The tile Y coordinate.
  262. * @param {number} level The tile level;
  263. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  264. */
  265. IonImageryProvider.prototype.getTileCredits = function (x, y, level) {
  266. const innerCredits = this._imageryProvider.getTileCredits(x, y, level);
  267. if (!defined(innerCredits)) {
  268. return this._tileCredits;
  269. }
  270. return this._tileCredits.concat(innerCredits);
  271. };
  272. /**
  273. * Requests the image for a given tile.
  274. * @function
  275. *
  276. * @param {number} x The tile X coordinate.
  277. * @param {number} y The tile Y coordinate.
  278. * @param {number} level The tile level.
  279. * @param {Request} [request] The request object. Intended for internal use only.
  280. * @returns {Promise<ImageryTypes>|undefined} A promise for the image that will resolve when the image is available, or
  281. * undefined if there are too many active requests to the server, and the request should be retried later.
  282. */
  283. IonImageryProvider.prototype.requestImage = function (x, y, level, request) {
  284. return this._imageryProvider.requestImage(x, y, level, request);
  285. };
  286. /**
  287. * Asynchronously determines what features, if any, are located at a given longitude and latitude within
  288. * a tile. This function is optional, so it may not exist on all ImageryProviders.
  289. *
  290. * @function
  291. *
  292. * @param {number} x The tile X coordinate.
  293. * @param {number} y The tile Y coordinate.
  294. * @param {number} level The tile level.
  295. * @param {number} longitude The longitude at which to pick features.
  296. * @param {number} latitude The latitude at which to pick features.
  297. * @return {Promise<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  298. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  299. * instances. The array may be empty if no features are found at the given location.
  300. * It may also be undefined if picking is not supported.
  301. */
  302. IonImageryProvider.prototype.pickFeatures = function (
  303. x,
  304. y,
  305. level,
  306. longitude,
  307. latitude,
  308. ) {
  309. return this._imageryProvider.pickFeatures(x, y, level, longitude, latitude);
  310. };
  311. //exposed for testing
  312. IonImageryProvider._endpointCache = {};
  313. export default IonImageryProvider;