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

MapboxStyleImageryProvider.js 11KB

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