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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. TILE_URL[options.style] || TILE_URL['custom']
  25. ].join('')
  26. this._labelStyle = options.labelStyle || 'web2D'
  27. this._tileWidth = 256
  28. this._tileHeight = 256
  29. this._maximumLevel = 18
  30. this._crs = options.crs || 'BD09'
  31. if (options.crs === 'WGS84') {
  32. let resolutions = []
  33. for (let i = 0; i < 19; i++) {
  34. resolutions[i] = 256 * Math.pow(2, 18 - i)
  35. }
  36. this._tilingScheme = new BaiduMercatorTilingScheme({
  37. resolutions,
  38. rectangleSouthwestInMeters: new Cesium.Cartesian2(
  39. -20037726.37,
  40. -12474104.17
  41. ),
  42. rectangleNortheastInMeters: new Cesium.Cartesian2(
  43. 20037726.37,
  44. 12474104.17
  45. )
  46. })
  47. } else {
  48. this._tilingScheme = new Cesium.WebMercatorTilingScheme({
  49. rectangleSouthwestInMeters: new Cesium.Cartesian2(-33554054, -33746824),
  50. rectangleNortheastInMeters: new Cesium.Cartesian2(33554054, 33746824)
  51. })
  52. }
  53. this._rectangle = this._tilingScheme.rectangle
  54. this._credit = undefined
  55. this._token = undefined
  56. this._style = options.style || 'normal'
  57. this._errorEvent = new Cesium.Event()
  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. get errorEvent() {
  123. return this._errorEvent
  124. }
  125. getTileCredits(x, y, level) {}
  126. /**
  127. * Request Image
  128. * @param x
  129. * @param y
  130. * @param level
  131. * @returns {Promise<HTMLImageElement | HTMLCanvasElement>}
  132. */
  133. requestImage(x, y, level) {
  134. if (!this.ready) {
  135. throw new Cesium.DeveloperError(
  136. 'requestImage must not be called before the imagery provider is ready.'
  137. )
  138. }
  139. let xTiles = this._tilingScheme.getNumberOfXTilesAtLevel(level)
  140. let yTiles = this._tilingScheme.getNumberOfYTilesAtLevel(level)
  141. let url = this._url
  142. .replace('{z}', level)
  143. .replace('{s}', String(1))
  144. .replace('{style}', this._style)
  145. .replace('{labelStyle}', this._labelStyle)
  146. .replace('{time}', String(new Date().getTime()))
  147. if (this._crs === 'WGS84') {
  148. url = url.replace('{x}', String(x)).replace('{y}', String(-y))
  149. } else {
  150. url = url
  151. .replace('{x}', String(x - xTiles / 2))
  152. .replace('{y}', String(yTiles / 2 - y - 1))
  153. }
  154. return Cesium.ImageryProvider.loadImage(this, url)
  155. }
  156. }
  157. ImageryType.BAIDU = 'baidu'
  158. export default BaiduImageryProvider