MapboxStyleImageryProvider.js 13 KB

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