OpenStreetMapImageryProvider.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Credit from "../Core/Credit.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import Rectangle from "../Core/Rectangle.js";
  6. import Resource from "../Core/Resource.js";
  7. import WebMercatorTilingScheme from "../Core/WebMercatorTilingScheme.js";
  8. import UrlTemplateImageryProvider from "./UrlTemplateImageryProvider.js";
  9. const defaultCredit = new Credit(
  10. "MapQuest, Open Street Map and contributors, CC-BY-SA"
  11. );
  12. /**
  13. * @typedef {Object} OpenStreetMapImageryProvider.ConstructorOptions
  14. *
  15. * Initialization options for the OpenStreetMapImageryProvider constructor
  16. *
  17. * @property {String} [url='https://a.tile.openstreetmap.org'] The OpenStreetMap server url.
  18. * @property {String} [fileExtension='png'] The file extension for images on the server.
  19. * @property {Rectangle} [rectangle=Rectangle.MAX_VALUE] The rectangle of the layer.
  20. * @property {Number} [minimumLevel=0] The minimum level-of-detail supported by the imagery provider.
  21. * @property {Number} [maximumLevel] The maximum level-of-detail supported by the imagery provider, or undefined if there is no limit.
  22. * @property {Ellipsoid} [ellipsoid] The ellipsoid. If not specified, the WGS84 ellipsoid is used.
  23. * @property {Credit|String} [credit='MapQuest, Open Street Map and contributors, CC-BY-SA'] A credit for the data source, which is displayed on the canvas.
  24. */
  25. /**
  26. * An imagery provider that provides tiled imagery hosted by OpenStreetMap
  27. * or another provider of Slippy tiles. The default url connects to OpenStreetMap's volunteer-run
  28. * servers, so you must conform to their
  29. * {@link http://wiki.openstreetmap.org/wiki/Tile_usage_policy|Tile Usage Policy}.
  30. *
  31. * @alias OpenStreetMapImageryProvider
  32. * @constructor
  33. * @extends UrlTemplateImageryProvider
  34. *
  35. * @param {OpenStreetMapImageryProvider.ConstructorOptions} options Object describing initialization options
  36. * @exception {DeveloperError} The rectangle and minimumLevel indicate that there are more than four tiles at the minimum level. Imagery providers with more than four tiles at the minimum level are not supported.
  37. *
  38. * @see ArcGisMapServerImageryProvider
  39. * @see BingMapsImageryProvider
  40. * @see GoogleEarthEnterpriseMapsProvider
  41. * @see SingleTileImageryProvider
  42. * @see TileMapServiceImageryProvider
  43. * @see WebMapServiceImageryProvider
  44. * @see WebMapTileServiceImageryProvider
  45. * @see UrlTemplateImageryProvider
  46. *
  47. * @example
  48. * const osm = new Cesium.OpenStreetMapImageryProvider({
  49. * url : 'https://a.tile.openstreetmap.org/'
  50. * });
  51. *
  52. * @see {@link http://wiki.openstreetmap.org/wiki/Main_Page|OpenStreetMap Wiki}
  53. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  54. */
  55. function OpenStreetMapImageryProvider(options) {
  56. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  57. const resource = Resource.createIfNeeded(
  58. defaultValue(options.url, "https://a.tile.openstreetmap.org/")
  59. );
  60. resource.appendForwardSlash();
  61. resource.url += `{z}/{x}/{y}.${defaultValue(options.fileExtension, "png")}`;
  62. const tilingScheme = new WebMercatorTilingScheme({
  63. ellipsoid: options.ellipsoid,
  64. });
  65. const tileWidth = 256;
  66. const tileHeight = 256;
  67. const minimumLevel = defaultValue(options.minimumLevel, 0);
  68. const maximumLevel = options.maximumLevel;
  69. const rectangle = defaultValue(options.rectangle, tilingScheme.rectangle);
  70. // Check the number of tiles at the minimum level. If it's more than four,
  71. // throw an exception, because starting at the higher minimum
  72. // level will cause too many tiles to be downloaded and rendered.
  73. const swTile = tilingScheme.positionToTileXY(
  74. Rectangle.southwest(rectangle),
  75. minimumLevel
  76. );
  77. const neTile = tilingScheme.positionToTileXY(
  78. Rectangle.northeast(rectangle),
  79. minimumLevel
  80. );
  81. const tileCount =
  82. (Math.abs(neTile.x - swTile.x) + 1) * (Math.abs(neTile.y - swTile.y) + 1);
  83. //>>includeStart('debug', pragmas.debug);
  84. if (tileCount > 4) {
  85. throw new DeveloperError(
  86. `The rectangle and minimumLevel indicate that there are ${tileCount} tiles at the minimum level. Imagery providers with more than four tiles at the minimum level are not supported.`
  87. );
  88. }
  89. //>>includeEnd('debug');
  90. let credit = defaultValue(options.credit, defaultCredit);
  91. if (typeof credit === "string") {
  92. credit = new Credit(credit);
  93. }
  94. UrlTemplateImageryProvider.call(this, {
  95. url: resource,
  96. credit: credit,
  97. tilingScheme: tilingScheme,
  98. tileWidth: tileWidth,
  99. tileHeight: tileHeight,
  100. minimumLevel: minimumLevel,
  101. maximumLevel: maximumLevel,
  102. rectangle: rectangle,
  103. });
  104. }
  105. if (defined(Object.create)) {
  106. OpenStreetMapImageryProvider.prototype = Object.create(
  107. UrlTemplateImageryProvider.prototype
  108. );
  109. OpenStreetMapImageryProvider.prototype.constructor = OpenStreetMapImageryProvider;
  110. }
  111. export default OpenStreetMapImageryProvider;