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

OpenStreetMapImageryProvider.js 4.9KB

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