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

SingleTileImageryProvider.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import Check from "../Core/Check.js";
  2. import Credit from "../Core/Credit.js";
  3. import Frozen from "../Core/Frozen.js";
  4. import defined from "../Core/defined.js";
  5. import Event from "../Core/Event.js";
  6. import GeographicTilingScheme from "../Core/GeographicTilingScheme.js";
  7. import Rectangle from "../Core/Rectangle.js";
  8. import Resource from "../Core/Resource.js";
  9. import RuntimeError from "../Core/RuntimeError.js";
  10. import TileProviderError from "../Core/TileProviderError.js";
  11. import ImageryProvider from "./ImageryProvider.js";
  12. /**
  13. * @typedef {object} SingleTileImageryProvider.ConstructorOptions
  14. *
  15. * Initialization options for the SingleTileImageryProvider constructor
  16. *
  17. * @property {Resource|string} url The url for the tile.
  18. * @property {number} [tileWidth] The width of the tile, in pixels.
  19. * @property {number} [tileHeight] The height of the tile, in pixels.
  20. * @property {Rectangle} [rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
  21. * @property {Credit|string} [credit] A credit for the data source, which is displayed on the canvas.
  22. * @property {Ellipsoid} [ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  23. */
  24. /**
  25. * Provides a single, top-level imagery tile. The single image is assumed to be in
  26. * the Geographic projection (i.e. WGS84 / EPSG:4326),
  27. * and will be rendered using a {@link GeographicTilingScheme}.
  28. *
  29. * @alias SingleTileImageryProvider
  30. * @constructor
  31. *
  32. * @param {SingleTileImageryProvider.ConstructorOptions} options Object describing initialization options
  33. *
  34. * @see ArcGisMapServerImageryProvider
  35. * @see BingMapsImageryProvider
  36. * @see GoogleEarthEnterpriseMapsProvider
  37. * @see OpenStreetMapImageryProvider
  38. * @see TileMapServiceImageryProvider
  39. * @see WebMapServiceImageryProvider
  40. * @see WebMapTileServiceImageryProvider
  41. * @see UrlTemplateImageryProvider
  42. */
  43. function SingleTileImageryProvider(options) {
  44. options = options ?? Frozen.EMPTY_OBJECT;
  45. this._defaultAlpha = undefined;
  46. this._defaultNightAlpha = undefined;
  47. this._defaultDayAlpha = undefined;
  48. this._defaultBrightness = undefined;
  49. this._defaultContrast = undefined;
  50. this._defaultHue = undefined;
  51. this._defaultSaturation = undefined;
  52. this._defaultGamma = undefined;
  53. this._defaultMinificationFilter = undefined;
  54. this._defaultMagnificationFilter = undefined;
  55. const rectangle = options.rectangle ?? Rectangle.MAX_VALUE;
  56. const tilingScheme = new GeographicTilingScheme({
  57. rectangle: rectangle,
  58. numberOfLevelZeroTilesX: 1,
  59. numberOfLevelZeroTilesY: 1,
  60. ellipsoid: options.ellipsoid,
  61. });
  62. this._tilingScheme = tilingScheme;
  63. this._image = undefined;
  64. this._texture = undefined;
  65. this._hasError = false;
  66. this._errorEvent = new Event();
  67. let credit = options.credit;
  68. if (typeof credit === "string") {
  69. credit = new Credit(credit);
  70. }
  71. this._credit = credit;
  72. //>>includeStart('debug', pragmas.debug);
  73. Check.defined("options.url", options.url);
  74. //>>includeEnd('debug');
  75. const resource = Resource.createIfNeeded(options.url);
  76. this._resource = resource;
  77. //>>includeStart('debug', pragmas.debug);
  78. Check.typeOf.number("options.tileWidth", options.tileWidth);
  79. Check.typeOf.number("options.tileHeight", options.tileHeight);
  80. //>>includeEnd('debug');
  81. this._tileWidth = options.tileWidth;
  82. this._tileHeight = options.tileHeight;
  83. }
  84. Object.defineProperties(SingleTileImageryProvider.prototype, {
  85. /**
  86. * Gets the URL of the single, top-level imagery tile.
  87. * @memberof SingleTileImageryProvider.prototype
  88. * @type {string}
  89. * @readonly
  90. */
  91. url: {
  92. get: function () {
  93. return this._resource.url;
  94. },
  95. },
  96. /**
  97. * Gets the proxy used by this provider.
  98. * @memberof SingleTileImageryProvider.prototype
  99. * @type {Proxy}
  100. * @readonly
  101. */
  102. proxy: {
  103. get: function () {
  104. return this._resource.proxy;
  105. },
  106. },
  107. /**
  108. * Gets the width of each tile, in pixels.
  109. * @memberof SingleTileImageryProvider.prototype
  110. * @type {number}
  111. * @readonly
  112. */
  113. tileWidth: {
  114. get: function () {
  115. return this._tileWidth;
  116. },
  117. },
  118. /**
  119. * Gets the height of each tile, in pixels.
  120. * @memberof SingleTileImageryProvider.prototype
  121. * @type {number}
  122. * @readonly
  123. */
  124. tileHeight: {
  125. get: function () {
  126. return this._tileHeight;
  127. },
  128. },
  129. /**
  130. * Gets the maximum level-of-detail that can be requested.
  131. * @memberof SingleTileImageryProvider.prototype
  132. * @type {number|undefined}
  133. * @readonly
  134. */
  135. maximumLevel: {
  136. get: function () {
  137. return 0;
  138. },
  139. },
  140. /**
  141. * Gets the minimum level-of-detail that can be requested.
  142. * @memberof SingleTileImageryProvider.prototype
  143. * @type {number}
  144. * @readonly
  145. */
  146. minimumLevel: {
  147. get: function () {
  148. return 0;
  149. },
  150. },
  151. /**
  152. * Gets the tiling scheme used by this provider.
  153. * @memberof SingleTileImageryProvider.prototype
  154. * @type {TilingScheme}
  155. * @readonly
  156. */
  157. tilingScheme: {
  158. get: function () {
  159. return this._tilingScheme;
  160. },
  161. },
  162. /**
  163. * Gets the rectangle, in radians, of the imagery provided by this instance.
  164. * @memberof SingleTileImageryProvider.prototype
  165. * @type {Rectangle}
  166. * @readonly
  167. */
  168. rectangle: {
  169. get: function () {
  170. return this._tilingScheme.rectangle;
  171. },
  172. },
  173. /**
  174. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  175. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  176. * returns undefined, no tiles are filtered.
  177. * @memberof SingleTileImageryProvider.prototype
  178. * @type {TileDiscardPolicy}
  179. * @readonly
  180. */
  181. tileDiscardPolicy: {
  182. get: function () {
  183. return undefined;
  184. },
  185. },
  186. /**
  187. * Gets an event that is raised when the imagery provider encounters an asynchronous error. By subscribing
  188. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  189. * are passed an instance of {@link TileProviderError}.
  190. * @memberof SingleTileImageryProvider.prototype
  191. * @type {Event}
  192. * @readonly
  193. */
  194. errorEvent: {
  195. get: function () {
  196. return this._errorEvent;
  197. },
  198. },
  199. /**
  200. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  201. * the source of the imagery.
  202. * @memberof SingleTileImageryProvider.prototype
  203. * @type {Credit}
  204. * @readonly
  205. */
  206. credit: {
  207. get: function () {
  208. return this._credit;
  209. },
  210. },
  211. /**
  212. * Gets a value indicating whether or not the images provided by this imagery provider
  213. * include an alpha channel. If this property is false, an alpha channel, if present, will
  214. * be ignored. If this property is true, any images without an alpha channel will be treated
  215. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  216. * and texture upload time are reduced.
  217. * @memberof SingleTileImageryProvider.prototype
  218. * @type {boolean}
  219. * @readonly
  220. */
  221. hasAlphaChannel: {
  222. get: function () {
  223. return true;
  224. },
  225. },
  226. });
  227. function failure(resource, error, provider, previousError) {
  228. let message = `Failed to load image ${resource.url}`;
  229. if (defined(error) && defined(error.message)) {
  230. message += `: ${error.message}`;
  231. }
  232. const reportedError = TileProviderError.reportError(
  233. previousError,
  234. provider,
  235. defined(provider) ? provider._errorEvent : undefined,
  236. message,
  237. 0,
  238. 0,
  239. 0,
  240. error,
  241. );
  242. if (reportedError.retry) {
  243. return doRequest(resource, provider, reportedError);
  244. }
  245. if (defined(provider)) {
  246. provider._hasError = true;
  247. }
  248. throw new RuntimeError(message);
  249. }
  250. async function doRequest(resource, provider, previousError) {
  251. try {
  252. const image = await ImageryProvider.loadImage(null, resource);
  253. return image;
  254. } catch (error) {
  255. return failure(resource, error, provider, previousError);
  256. }
  257. }
  258. /**
  259. * @typedef {object} SingleTileImageryProvider.fromUrlOptions
  260. *
  261. * Initialization options for the SingleTileImageryProvider constructor when using SingleTileImageryProvider.fromUrl
  262. *
  263. * @property {Rectangle} [rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
  264. * @property {Credit|string} [credit] A credit for the data source, which is displayed on the canvas.
  265. * @property {Ellipsoid} [ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  266. */
  267. /**
  268. * Creates a provider for a single, top-level imagery tile. The single image is assumed to use a
  269. * @param {Resource|string} url The url for the tile
  270. * @param {SingleTileImageryProvider.fromUrlOptions} [options] Object describing initialization options.
  271. * @returns {Promise.<SingleTileImageryProvider>} The resolved SingleTileImageryProvider.
  272. *
  273. * @example
  274. * const provider = await SingleTileImageryProvider.fromUrl("https://yoururl.com/image.png");
  275. */
  276. SingleTileImageryProvider.fromUrl = async function (url, options) {
  277. //>>includeStart('debug', pragmas.debug);
  278. Check.defined("url", url);
  279. //>>includeEnd('debug');
  280. const resource = Resource.createIfNeeded(url);
  281. const image = await doRequest(resource);
  282. options = options ?? Frozen.EMPTY_OBJECT;
  283. const provider = new SingleTileImageryProvider({
  284. ...options,
  285. url: url,
  286. tileWidth: image.width,
  287. tileHeight: image.height,
  288. });
  289. provider._image = image;
  290. return provider;
  291. };
  292. /**
  293. * Gets the credits to be displayed when a given tile is displayed.
  294. *
  295. * @param {number} x The tile X coordinate.
  296. * @param {number} y The tile Y coordinate.
  297. * @param {number} level The tile level;
  298. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  299. */
  300. SingleTileImageryProvider.prototype.getTileCredits = function (x, y, level) {
  301. return undefined;
  302. };
  303. /**
  304. * Requests the image for a given tile.
  305. *
  306. * @param {number} x The tile X coordinate.
  307. * @param {number} y The tile Y coordinate.
  308. * @param {number} level The tile level.
  309. * @param {Request} [request] The request object. Intended for internal use only.
  310. * @returns {Promise.<ImageryTypes>|undefined} The resolved image
  311. */
  312. SingleTileImageryProvider.prototype.requestImage = async function (
  313. x,
  314. y,
  315. level,
  316. request,
  317. ) {
  318. if (!this._hasError && !defined(this._image)) {
  319. const image = await doRequest(this._resource, this);
  320. this._image = image;
  321. TileProviderError.reportSuccess(this._errorEvent);
  322. return image;
  323. }
  324. return this._image;
  325. };
  326. /**
  327. * Picking features is not currently supported by this imagery provider, so this function simply returns
  328. * undefined.
  329. *
  330. * @param {number} x The tile X coordinate.
  331. * @param {number} y The tile Y coordinate.
  332. * @param {number} level The tile level.
  333. * @param {number} longitude The longitude at which to pick features.
  334. * @param {number} latitude The latitude at which to pick features.
  335. * @return {undefined} Undefined since picking is not supported.
  336. */
  337. SingleTileImageryProvider.prototype.pickFeatures = function (
  338. x,
  339. y,
  340. level,
  341. longitude,
  342. latitude,
  343. ) {
  344. return undefined;
  345. };
  346. export default SingleTileImageryProvider;