Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BaiduMercatorTilingScheme.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @Author: Caven
  3. * @Date: 2021-01-31 19:22:04
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { CoordTransform } from '@dc-modules/transform'
  7. import BaiduMercatorProjection from '../projection/BaiduMercatorProjection'
  8. class BaiduMercatorTilingScheme extends Cesium.WebMercatorTilingScheme {
  9. constructor(options) {
  10. super(options)
  11. let projection = new BaiduMercatorProjection()
  12. this._projection.project = function(cartographic, result) {
  13. result = result || {}
  14. result = CoordTransform.WGS84ToGCJ02(
  15. Cesium.Math.toDegrees(cartographic.longitude),
  16. Cesium.Math.toDegrees(cartographic.latitude)
  17. )
  18. result = CoordTransform.GCJ02ToBD09(result[0], result[1])
  19. result[0] = Math.min(result[0], 180)
  20. result[0] = Math.max(result[0], -180)
  21. result[1] = Math.min(result[1], 74.000022)
  22. result[1] = Math.max(result[1], -71.988531)
  23. result = projection.lngLatToPoint({
  24. lng: result[0],
  25. lat: result[1]
  26. })
  27. return new Cesium.Cartesian2(result.x, result.y)
  28. }
  29. this._projection.unproject = function(cartesian, result) {
  30. result = result || {}
  31. result = projection.mercatorToLngLat({
  32. lng: cartesian.x,
  33. lat: cartesian.y
  34. })
  35. result = CoordTransform.BD09ToGCJ02(result.lng, result.lat)
  36. result = CoordTransform.GCJ02ToWGS84(result[0], result[1])
  37. return new Cesium.Cartographic(
  38. Cesium.Math.toRadians(result[0]),
  39. Cesium.Math.toRadians(result[1])
  40. )
  41. }
  42. this.resolutions = options.resolutions || []
  43. }
  44. /**
  45. *
  46. * @param x
  47. * @param y
  48. * @param level
  49. * @param result
  50. * @returns {module:cesium.Rectangle|*}
  51. */
  52. tileXYToNativeRectangle(x, y, level, result) {
  53. const tileWidth = this.resolutions[level]
  54. const west = x * tileWidth
  55. const east = (x + 1) * tileWidth
  56. const north = ((y = -y) + 1) * tileWidth
  57. const south = y * tileWidth
  58. if (!Cesium.defined(result)) {
  59. return new Cesium.Rectangle(west, south, east, north)
  60. }
  61. result.west = west
  62. result.south = south
  63. result.east = east
  64. result.north = north
  65. return result
  66. }
  67. /**
  68. *
  69. * @param position
  70. * @param level
  71. * @param result
  72. * @returns {undefined|*}
  73. */
  74. positionToTileXY(position, level, result) {
  75. const rectangle = this._rectangle
  76. if (!Cesium.Rectangle.contains(rectangle, position)) {
  77. return undefined
  78. }
  79. const projection = this._projection
  80. const webMercatorPosition = projection.project(position)
  81. if (!Cesium.defined(webMercatorPosition)) {
  82. return undefined
  83. }
  84. const tileWidth = this.resolutions[level]
  85. const xTileCoordinate = Math.floor(webMercatorPosition.x / tileWidth)
  86. const yTileCoordinate = -Math.floor(webMercatorPosition.y / tileWidth)
  87. if (!Cesium.defined(result)) {
  88. return new Cesium.Cartesian2(xTileCoordinate, yTileCoordinate)
  89. }
  90. result.x = xTileCoordinate
  91. result.y = yTileCoordinate
  92. return result
  93. }
  94. }
  95. export default BaiduMercatorTilingScheme