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

MapboxImageryProvider.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import Credit from "../Core/Credit.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import Resource from "../Core/Resource.js";
  6. import UrlTemplateImageryProvider from "./UrlTemplateImageryProvider.js";
  7. const trailingSlashRegex = /\/$/;
  8. const defaultCredit = new Credit(
  9. '&copy; <a href="https://www.mapbox.com/about/maps/">Mapbox</a> &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback/">Improve this map</a></strong>',
  10. );
  11. /**
  12. * @typedef {object} MapboxImageryProvider.ConstructorOptions
  13. *
  14. * Initialization options for the MapboxImageryProvider constructor
  15. *
  16. * @property {string} [url='https://api.mapbox.com/v4/'] The Mapbox server url.
  17. * @property {string} mapId The Mapbox Map ID.
  18. * @property {string} accessToken The public access token for the imagery.
  19. * @property {string} [format='png'] The format of the image request.
  20. * @property {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid. If not specified, the default ellipsoid is used.
  21. * @property {number} [minimumLevel=0] The minimum level-of-detail supported by the imagery provider. Take care when specifying
  22. * this that the number of tiles at the minimum level is small, such as four or less. A larger number is likely
  23. * to result in rendering problems.
  24. * @property {number} [maximumLevel] The maximum level-of-detail supported by the imagery provider, or undefined if there is no limit.
  25. * @property {Rectangle} [rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
  26. * @property {Credit|string} [credit] A credit for the data source, which is displayed on the canvas.
  27. */
  28. /**
  29. * Provides tiled imagery hosted by Mapbox.
  30. *
  31. * @alias MapboxImageryProvider
  32. * @constructor
  33. *
  34. * @param {MapboxImageryProvider.ConstructorOptions} options Object describing initialization options
  35. *
  36. * @example
  37. * // Mapbox tile provider
  38. * const mapbox = new Cesium.MapboxImageryProvider({
  39. * mapId: 'mapbox.mapbox-terrain-v2',
  40. * accessToken: 'thisIsMyAccessToken'
  41. * });
  42. *
  43. * @see {@link https://docs.mapbox.com/api/maps/raster-tiles/}
  44. * @see {@link https://docs.mapbox.com/api/accounts/tokens/}
  45. */
  46. function MapboxImageryProvider(options) {
  47. options = options ?? Frozen.EMPTY_OBJECT;
  48. const mapId = options.mapId;
  49. //>>includeStart('debug', pragmas.debug);
  50. if (!defined(mapId)) {
  51. throw new DeveloperError("options.mapId is required.");
  52. }
  53. //>>includeEnd('debug');
  54. const accessToken = options.accessToken;
  55. //>>includeStart('debug', pragmas.debug);
  56. if (!defined(accessToken)) {
  57. throw new DeveloperError("options.accessToken is required.");
  58. }
  59. //>>includeEnd('debug');
  60. this._defaultAlpha = undefined;
  61. this._defaultNightAlpha = undefined;
  62. this._defaultDayAlpha = undefined;
  63. this._defaultBrightness = undefined;
  64. this._defaultContrast = undefined;
  65. this._defaultHue = undefined;
  66. this._defaultSaturation = undefined;
  67. this._defaultGamma = undefined;
  68. this._defaultMinificationFilter = undefined;
  69. this._defaultMagnificationFilter = undefined;
  70. const resource = Resource.createIfNeeded(
  71. options.url ?? "https://{s}.tiles.mapbox.com/v4/",
  72. );
  73. this._mapId = mapId;
  74. this._accessToken = accessToken;
  75. let format = options.format ?? "png";
  76. if (!/\./.test(format)) {
  77. format = `.${format}`;
  78. }
  79. this._format = format;
  80. let templateUrl = resource.getUrlComponent();
  81. if (!trailingSlashRegex.test(templateUrl)) {
  82. templateUrl += "/";
  83. }
  84. templateUrl += `${mapId}/{z}/{x}/{y}${this._format}`;
  85. resource.url = templateUrl;
  86. resource.setQueryParameters({
  87. access_token: accessToken,
  88. });
  89. let credit;
  90. if (defined(options.credit)) {
  91. credit = options.credit;
  92. if (typeof credit === "string") {
  93. credit = new Credit(credit);
  94. }
  95. } else {
  96. credit = defaultCredit;
  97. }
  98. this._resource = resource;
  99. this._imageryProvider = new UrlTemplateImageryProvider({
  100. url: resource,
  101. credit: credit,
  102. ellipsoid: options.ellipsoid,
  103. minimumLevel: options.minimumLevel,
  104. maximumLevel: options.maximumLevel,
  105. rectangle: options.rectangle,
  106. });
  107. }
  108. Object.defineProperties(MapboxImageryProvider.prototype, {
  109. /**
  110. * Gets the URL of the Mapbox server.
  111. * @memberof MapboxImageryProvider.prototype
  112. * @type {string}
  113. * @readonly
  114. */
  115. url: {
  116. get: function () {
  117. return this._imageryProvider.url;
  118. },
  119. },
  120. /**
  121. * Gets the rectangle, in radians, of the imagery provided by the instance.
  122. * @memberof MapboxImageryProvider.prototype
  123. * @type {Rectangle}
  124. * @readonly
  125. */
  126. rectangle: {
  127. get: function () {
  128. return this._imageryProvider.rectangle;
  129. },
  130. },
  131. /**
  132. * Gets the width of each tile, in pixels.
  133. * @memberof MapboxImageryProvider.prototype
  134. * @type {number}
  135. * @readonly
  136. */
  137. tileWidth: {
  138. get: function () {
  139. return this._imageryProvider.tileWidth;
  140. },
  141. },
  142. /**
  143. * Gets the height of each tile, in pixels.
  144. * @memberof MapboxImageryProvider.prototype
  145. * @type {number}
  146. * @readonly
  147. */
  148. tileHeight: {
  149. get: function () {
  150. return this._imageryProvider.tileHeight;
  151. },
  152. },
  153. /**
  154. * Gets the maximum level-of-detail that can be requested.
  155. * @memberof MapboxImageryProvider.prototype
  156. * @type {number|undefined}
  157. * @readonly
  158. */
  159. maximumLevel: {
  160. get: function () {
  161. return this._imageryProvider.maximumLevel;
  162. },
  163. },
  164. /**
  165. * Gets the minimum level-of-detail that can be requested. Generally,
  166. * a minimum level should only be used when the rectangle of the imagery is small
  167. * enough that the number of tiles at the minimum level is small. An imagery
  168. * provider with more than a few tiles at the minimum level will lead to
  169. * rendering problems.
  170. * @memberof MapboxImageryProvider.prototype
  171. * @type {number}
  172. * @readonly
  173. */
  174. minimumLevel: {
  175. get: function () {
  176. return this._imageryProvider.minimumLevel;
  177. },
  178. },
  179. /**
  180. * Gets the tiling scheme used by the provider.
  181. * @memberof MapboxImageryProvider.prototype
  182. * @type {TilingScheme}
  183. * @readonly
  184. */
  185. tilingScheme: {
  186. get: function () {
  187. return this._imageryProvider.tilingScheme;
  188. },
  189. },
  190. /**
  191. * Gets the tile discard policy. If not undefined, the discard policy is responsible
  192. * for filtering out "missing" tiles via its shouldDiscardImage function. If this function
  193. * returns undefined, no tiles are filtered.
  194. * @memberof MapboxImageryProvider.prototype
  195. * @type {TileDiscardPolicy}
  196. * @readonly
  197. */
  198. tileDiscardPolicy: {
  199. get: function () {
  200. return this._imageryProvider.tileDiscardPolicy;
  201. },
  202. },
  203. /**
  204. * Gets an event that is raised when the imagery provider encounters an asynchronous error.. By subscribing
  205. * to the event, you will be notified of the error and can potentially recover from it. Event listeners
  206. * are passed an instance of {@link TileProviderError}.
  207. * @memberof MapboxImageryProvider.prototype
  208. * @type {Event}
  209. * @readonly
  210. */
  211. errorEvent: {
  212. get: function () {
  213. return this._imageryProvider.errorEvent;
  214. },
  215. },
  216. /**
  217. * Gets the credit to display when this imagery provider is active. Typically this is used to credit
  218. * the source of the imagery.
  219. * @memberof MapboxImageryProvider.prototype
  220. * @type {Credit}
  221. * @readonly
  222. */
  223. credit: {
  224. get: function () {
  225. return this._imageryProvider.credit;
  226. },
  227. },
  228. /**
  229. * Gets the proxy used by this provider.
  230. * @memberof MapboxImageryProvider.prototype
  231. * @type {Proxy}
  232. * @readonly
  233. */
  234. proxy: {
  235. get: function () {
  236. return this._imageryProvider.proxy;
  237. },
  238. },
  239. /**
  240. * Gets a value indicating whether or not the images provided by this imagery provider
  241. * include an alpha channel. If this property is false, an alpha channel, if present, will
  242. * be ignored. If this property is true, any images without an alpha channel will be treated
  243. * as if their alpha is 1.0 everywhere. When this property is false, memory usage
  244. * and texture upload time are reduced.
  245. * @memberof MapboxImageryProvider.prototype
  246. * @type {boolean}
  247. * @readonly
  248. */
  249. hasAlphaChannel: {
  250. get: function () {
  251. return this._imageryProvider.hasAlphaChannel;
  252. },
  253. },
  254. });
  255. /**
  256. * Gets the credits to be displayed when a given tile is displayed.
  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. * @returns {Credit[]} The credits to be displayed when the tile is displayed.
  262. */
  263. MapboxImageryProvider.prototype.getTileCredits = function (x, y, level) {
  264. return undefined;
  265. };
  266. /**
  267. * Requests the image for a given tile.
  268. *
  269. * @param {number} x The tile X coordinate.
  270. * @param {number} y The tile Y coordinate.
  271. * @param {number} level The tile level.
  272. * @param {Request} [request] The request object. Intended for internal use only.
  273. * @returns {Promise<ImageryTypes>|undefined} A promise for the image that will resolve when the image is available, or
  274. * undefined if there are too many active requests to the server, and the request should be retried later.
  275. */
  276. MapboxImageryProvider.prototype.requestImage = function (x, y, level, request) {
  277. return this._imageryProvider.requestImage(x, y, level, request);
  278. };
  279. /**
  280. * Asynchronously determines what features, if any, are located at a given longitude and latitude within
  281. * a tile. This function is optional, so it may not exist on all ImageryProviders.
  282. *
  283. * @param {number} x The tile X coordinate.
  284. * @param {number} y The tile Y coordinate.
  285. * @param {number} level The tile level.
  286. * @param {number} longitude The longitude at which to pick features.
  287. * @param {number} latitude The latitude at which to pick features.
  288. * @return {Promise<ImageryLayerFeatureInfo[]>|undefined} A promise for the picked features that will resolve when the asynchronous
  289. * picking completes. The resolved value is an array of {@link ImageryLayerFeatureInfo}
  290. * instances. The array may be empty if no features are found at the given location.
  291. * It may also be undefined if picking is not supported.
  292. */
  293. MapboxImageryProvider.prototype.pickFeatures = function (
  294. x,
  295. y,
  296. level,
  297. longitude,
  298. latitude,
  299. ) {
  300. return this._imageryProvider.pickFeatures(x, y, level, longitude, latitude);
  301. };
  302. // Exposed for tests
  303. MapboxImageryProvider._defaultCredit = defaultCredit;
  304. export default MapboxImageryProvider;