You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaiduImageryProvider.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-15 20:27:27
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import ImageryType from '../ImageryType'
  7. import BaiduMercatorTilingScheme from '../tiling-scheme/BaiduMercatorTilingScheme'
  8. const TILE_URL = {
  9. img:
  10. '//shangetu{s}.map.bdimg.com/it/u=x={x};y={y};z={z};v=009;type=sate&fm=46',
  11. vec:
  12. '//online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=sl&v=020',
  13. custom:
  14. '//api{s}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&scale=1&customid={style}',
  15. traffic:
  16. '//its.map.baidu.com:8002/traffic/TrafficTileService?time={time}&label={labelStyle}&v=016&level={z}&x={x}&y={y}&scaler=2'
  17. }
  18. class BaiduImageryProvider {
  19. constructor(options = {}) {
  20. this._url =
  21. options.url ||
  22. [
  23. options.protocol || '',
  24. options.protocol ? ':' : '',
  25. TILE_URL[options.style || 'custom']
  26. ].join('')
  27. this._labelStyle = options.labelStyle || 'web2D'
  28. this._tileWidth = 256
  29. this._tileHeight = 256
  30. this._maximumLevel = 18
  31. this._crs = options.crs || 'BD09'
  32. if (options.crs === 'WGS84') {
  33. let resolutions = []
  34. for (let i = 0; i < 19; i++) {
  35. resolutions[i] = 256 * Math.pow(2, 18 - i)
  36. }
  37. this._tilingScheme = new BaiduMercatorTilingScheme({
  38. resolutions,
  39. rectangleSouthwestInMeters: new Cesium.Cartesian2(
  40. -20037726.37,
  41. -12474104.17
  42. ),
  43. rectangleNortheastInMeters: new Cesium.Cartesian2(
  44. 20037726.37,
  45. 12474104.17
  46. )
  47. })
  48. } else {
  49. this._tilingScheme = new Cesium.WebMercatorTilingScheme({
  50. rectangleSouthwestInMeters: new Cesium.Cartesian2(-33554054, -33746824),
  51. rectangleNortheastInMeters: new Cesium.Cartesian2(33554054, 33746824)
  52. })
  53. }
  54. this._rectangle = this._tilingScheme.rectangle
  55. this._credit = undefined
  56. this._token = undefined
  57. this._style = options.style || 'normal'
  58. }
  59. get url() {
  60. return this._url
  61. }
  62. get token() {
  63. return this._token
  64. }
  65. get tileWidth() {
  66. if (!this.ready) {
  67. throw new Cesium.DeveloperError(
  68. 'tileWidth must not be called before the imagery provider is ready.'
  69. )
  70. }
  71. return this._tileWidth
  72. }
  73. get tileHeight() {
  74. if (!this.ready) {
  75. throw new Cesium.DeveloperError(
  76. 'tileHeight must not be called before the imagery provider is ready.'
  77. )
  78. }
  79. return this._tileHeight
  80. }
  81. get maximumLevel() {
  82. if (!this.ready) {
  83. throw new Cesium.DeveloperError(
  84. 'maximumLevel must not be called before the imagery provider is ready.'
  85. )
  86. }
  87. return this._maximumLevel
  88. }
  89. get minimumLevel() {
  90. if (!this.ready) {
  91. throw new Cesium.DeveloperError(
  92. 'minimumLevel must not be called before the imagery provider is ready.'
  93. )
  94. }
  95. return 0
  96. }
  97. get tilingScheme() {
  98. if (!this.ready) {
  99. throw new Cesium.DeveloperError(
  100. 'tilingScheme must not be called before the imagery provider is ready.'
  101. )
  102. }
  103. return this._tilingScheme
  104. }
  105. get rectangle() {
  106. if (!this.ready) {
  107. throw new Cesium.DeveloperError(
  108. 'rectangle must not be called before the imagery provider is ready.'
  109. )
  110. }
  111. return this._rectangle
  112. }
  113. get ready() {
  114. return !!this._url
  115. }
  116. get credit() {
  117. return this._credit
  118. }
  119. get hasAlphaChannel() {
  120. return true
  121. }
  122. getTileCredits(x, y, level) {}
  123. /**
  124. * Request Image
  125. * @param x
  126. * @param y
  127. * @param level
  128. * @returns {Promise<HTMLImageElement | HTMLCanvasElement>}
  129. */
  130. requestImage(x, y, level) {
  131. if (!this.ready) {
  132. throw new Cesium.DeveloperError(
  133. 'requestImage must not be called before the imagery provider is ready.'
  134. )
  135. }
  136. let xTiles = this._tilingScheme.getNumberOfXTilesAtLevel(level)
  137. let yTiles = this._tilingScheme.getNumberOfYTilesAtLevel(level)
  138. let url = this._url
  139. .replace('{z}', level)
  140. .replace('{s}', String(1))
  141. .replace('{style}', this._style)
  142. .replace('{labelStyle}', this._labelStyle)
  143. .replace('{time}', String(new Date().getTime()))
  144. if (this._crs === 'WGS84') {
  145. url = url.replace('{x}', String(x)).replace('{y}', String(-y))
  146. } else {
  147. url = url
  148. .replace('{x}', String(x - xTiles / 2))
  149. .replace('{y}', String(yTiles / 2 - y - 1))
  150. }
  151. return Cesium.ImageryProvider.loadImage(this, url)
  152. }
  153. }
  154. ImageryType.BAIDU = 'baidu'
  155. export default BaiduImageryProvider