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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /**
  2. * @Author: Caven
  3. * @Date: 2019-12-27 17:13:24
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import Parse from '@dc-modules/parse/Parse'
  7. import {
  8. LayerGroupEventType,
  9. LayerEventType,
  10. MouseEvent,
  11. ViewerEvent,
  12. SceneEvent
  13. } from '@dc-modules/event'
  14. import { ViewerOption, CameraOption } from '@dc-modules/option'
  15. import { Util, DomUtil } from '@dc-modules/utils'
  16. import { Transform } from '@dc-modules/transform'
  17. import createWidgets from '@dc-modules/widget'
  18. const DEF_OPTS = {
  19. animation: false, //Whether to create animated widgets, lower left corner of the meter
  20. baseLayerPicker: false, //Whether to display the layer selector
  21. imageryProvider: false, // Whether to display the default imagery
  22. fullscreenButton: false, //Whether to display the full-screen button
  23. geocoder: false, //To display the geocoder widget, query the button in the upper right corner
  24. homeButton: false, //Whether to display the Home button
  25. infoBox: false, //Whether to display the information box
  26. sceneModePicker: false, //Whether to display 3D/2D selector
  27. selectionIndicator: false, //Whether to display the selection indicator component
  28. timeline: false, //Whether to display the timeline
  29. navigationHelpButton: false, //Whether to display the help button in the upper right corner
  30. navigationInstructionsInitiallyVisible: false,
  31. creditContainer: undefined,
  32. shouldAnimate: true
  33. }
  34. class Viewer {
  35. constructor(id, options = {}) {
  36. if (!id || !document.getElementById(id)) {
  37. throw new Error('Viewer:the id is empty')
  38. }
  39. this._delegate = new Cesium.Viewer(id, {
  40. ...DEF_OPTS,
  41. ...options
  42. }) // Initialize the viewer
  43. /**
  44. * Registers events
  45. */
  46. new MouseEvent(this) // Register global mouse events
  47. this._viewerEvent = new ViewerEvent() // Register viewer events
  48. this._sceneEvent = new SceneEvent(this) // Register scene events
  49. this._viewerOption = new ViewerOption(this) // Initialize the viewer option
  50. this._cameraOption = new CameraOption(this) // Initialize the camera option
  51. this._dcContainer = DomUtil.create(
  52. 'div',
  53. 'dc-container',
  54. document.getElementById(id)
  55. ) //Register the custom container
  56. this._baseLayerPicker = new Cesium.BaseLayerPickerViewModel({
  57. globe: this._delegate.scene.globe
  58. }) //Initialize the baseLayer picker
  59. this._layerGroupCache = {}
  60. this._layerCache = {}
  61. /**
  62. * Registers default widgets
  63. */
  64. let widgets = createWidgets()
  65. Object.keys(widgets).forEach(key => {
  66. this.use(widgets[key])
  67. })
  68. }
  69. get delegate() {
  70. return this._delegate
  71. }
  72. get dcContainer() {
  73. return this._dcContainer
  74. }
  75. get scene() {
  76. return this._delegate.scene
  77. }
  78. get camera() {
  79. return this._delegate.camera
  80. }
  81. get canvas() {
  82. return this._delegate.scene.canvas
  83. }
  84. get dataSources() {
  85. return this._delegate.dataSources
  86. }
  87. get imageryLayers() {
  88. return this._delegate.imageryLayers
  89. }
  90. get terrainProvider() {
  91. return this._delegate.terrainProvider
  92. }
  93. get entities() {
  94. return this._delegate.entities
  95. }
  96. get postProcessStages() {
  97. return this._delegate.postProcessStages
  98. }
  99. get clock() {
  100. return this._delegate.clock
  101. }
  102. get viewerEvent() {
  103. return this._viewerEvent
  104. }
  105. get cameraPosition() {
  106. let position = Transform.transformCartesianToWGS84(this.camera.positionWC)
  107. if (position) {
  108. position.heading = Cesium.Math.toDegrees(this.camera.heading)
  109. position.pitch = Cesium.Math.toDegrees(this.camera.pitch)
  110. position.roll = Cesium.Math.toDegrees(this.camera.roll)
  111. }
  112. return position
  113. }
  114. /***
  115. *
  116. * @param layerGroup
  117. * @private
  118. */
  119. _addLayerGroup(layerGroup) {
  120. if (
  121. layerGroup &&
  122. layerGroup.layerGroupEvent &&
  123. !Object(this._layerGroupCache).hasOwnProperty(layerGroup.id)
  124. ) {
  125. layerGroup.layerGroupEvent.fire(LayerGroupEventType.ADD, this)
  126. this._layerGroupCache[layerGroup.id] = layerGroup
  127. }
  128. }
  129. /**
  130. *
  131. * @param layerGroup
  132. * @private
  133. */
  134. _removeLayerGroup(layerGroup) {
  135. if (
  136. layerGroup &&
  137. layerGroup.layerGroupEvent &&
  138. Object(this._layerGroupCache).hasOwnProperty(layerGroup.id)
  139. ) {
  140. layerGroup.layerGroupEvent.fire(LayerGroupEventType.REMOVE, this)
  141. delete this._layerGroupCache[layerGroup.id]
  142. }
  143. }
  144. /**
  145. * @param layer
  146. * @private
  147. */
  148. _addLayer(layer) {
  149. if (layer && layer.layerEvent) {
  150. !this._layerCache[layer.type] && (this._layerCache[layer.type] = {})
  151. if (!Object(this._layerCache[layer.type]).hasOwnProperty(layer.id)) {
  152. layer.layerEvent.fire(LayerEventType.ADD, this)
  153. this._layerCache[layer.type][layer.id] = layer
  154. }
  155. }
  156. }
  157. /**
  158. * @param layer
  159. * @private
  160. */
  161. _removeLayer(layer) {
  162. if (
  163. layer &&
  164. layer.layerEvent &&
  165. Object(this._layerCache[layer.type]).hasOwnProperty(layer.id)
  166. ) {
  167. layer.layerEvent.fire(LayerEventType.REMOVE, this)
  168. delete this._layerCache[layer.type][layer.id]
  169. }
  170. }
  171. /**
  172. * Sets viewer options
  173. * @param options
  174. * @returns {Viewer}
  175. */
  176. setOptions(options) {
  177. this._viewerOption.setOptions(options)
  178. return this
  179. }
  180. /**
  181. * Sets camera pitch range
  182. * @param min
  183. * @param max
  184. * @returns {Viewer}
  185. */
  186. setPitchRange(min = -90, max = -20) {
  187. this._cameraOption.setPitchRange(min, max)
  188. return this
  189. }
  190. /**
  191. * @param west
  192. * @param south
  193. * @param east
  194. * @param north
  195. * @returns {Viewer}
  196. */
  197. setBounds(west, south, east, north) {
  198. this._cameraOption.setBounds(west, south, east, north)
  199. return this
  200. }
  201. /**
  202. * Changes Scene Mode,2:2D,2.5:2.5D,3:3D
  203. * @param sceneMode
  204. * @param duration
  205. * @returns {Viewer}
  206. */
  207. changeSceneMode(sceneMode, duration = 0) {
  208. if (sceneMode === 2) {
  209. this._delegate.scene.morphTo2D(duration)
  210. } else if (sceneMode === 3) {
  211. this._delegate.scene.morphTo3D(duration)
  212. } else if (sceneMode === 2.5) {
  213. this._delegate.scene.morphToColumbusView(duration)
  214. }
  215. return this
  216. }
  217. /**
  218. * Changes Mouse Mode,0:Default,1: Change the tiltEventTypes to CameraEventType.RIGHT_DRAG
  219. * @param mouseMode
  220. * @returns {Viewer}
  221. */
  222. changeMouseMode(mouseMode) {
  223. this._cameraOption.changeMouseMode(mouseMode)
  224. return this
  225. }
  226. /**
  227. * Adds the baseLayer .
  228. * The baseLayer can be a single or an array,
  229. * and when the baseLayer is an array, the baseLayer will be loaded together
  230. * @param baseLayers
  231. * @param options
  232. * @returns {Viewer}
  233. */
  234. addBaseLayer(baseLayers, options = {}) {
  235. if (!baseLayers) {
  236. return this
  237. }
  238. this._baseLayerPicker.imageryProviderViewModels.push(
  239. new Cesium.ProviderViewModel({
  240. name: options.name || '地图',
  241. creationFunction: () => {
  242. return baseLayers
  243. }
  244. })
  245. )
  246. if (!this._baseLayerPicker.selectedImagery) {
  247. this._baseLayerPicker.selectedImagery = this._baseLayerPicker.imageryProviderViewModels[0]
  248. }
  249. this.mapSwitch && this.mapSwitch.addMap(options)
  250. return this
  251. }
  252. /**
  253. * Changes the current globe display of the baseLayer
  254. * @param index
  255. * @returns {Viewer}
  256. */
  257. changeBaseLayer(index) {
  258. if (this._baseLayerPicker && index >= 0) {
  259. this._baseLayerPicker.selectedImagery = this._baseLayerPicker.imageryProviderViewModels[
  260. index
  261. ]
  262. }
  263. return this
  264. }
  265. /**
  266. * Adds the terrain
  267. * @param terrain
  268. * @returns {Viewer}
  269. */
  270. addTerrain(terrain) {
  271. if (!terrain) {
  272. return this
  273. }
  274. this._baseLayerPicker.terrainProviderViewModels.push(
  275. new Cesium.ProviderViewModel({
  276. name: '地形',
  277. creationFunction: () => {
  278. return terrain
  279. }
  280. })
  281. )
  282. if (!this._baseLayerPicker.selectedTerrain) {
  283. this._baseLayerPicker.selectedTerrain = this._baseLayerPicker.terrainProviderViewModels[0]
  284. }
  285. return this
  286. }
  287. /**
  288. * Changes the current globe display of the terrain
  289. * @param index
  290. * @returns {Viewer}
  291. */
  292. changeTerrain(index) {
  293. if (this._baseLayerPicker && index >= 0) {
  294. this._baseLayerPicker.selectedTerrain = this._baseLayerPicker.terrainProviderViewModels[
  295. index
  296. ]
  297. }
  298. return this
  299. }
  300. /**
  301. * Removes terrain
  302. * @returns {Viewer}
  303. */
  304. removeTerrain() {
  305. this._baseLayerPicker.terrainProviderViewModels = []
  306. this._baseLayerPicker.selectedTerrain = undefined
  307. this._delegate.terrainProvider = new Cesium.EllipsoidTerrainProvider()
  308. return this
  309. }
  310. /**
  311. *
  312. * @param layerGroup
  313. * @returns {Viewer}
  314. */
  315. addLayerGroup(layerGroup) {
  316. this._addLayerGroup(layerGroup)
  317. return this
  318. }
  319. /**
  320. *
  321. * @param layerGroup
  322. * @returns {Viewer}
  323. */
  324. removeLayerGroup(layerGroup) {
  325. this._removeLayerGroup(layerGroup)
  326. return this
  327. }
  328. /**
  329. * add a layer
  330. * @param layer
  331. * @returns {Viewer}
  332. */
  333. addLayer(layer) {
  334. this._addLayer(layer)
  335. return this
  336. }
  337. /**
  338. * Removes a layer
  339. * @param layer
  340. * @returns {Viewer}
  341. */
  342. removeLayer(layer) {
  343. this._removeLayer(layer)
  344. return this
  345. }
  346. /**
  347. * Checks to see if the layer is included
  348. * @param layer
  349. * @returns {boolean}
  350. */
  351. hasLayer(layer) {
  352. return (
  353. layer &&
  354. layer.layerEvent &&
  355. Object(this._layerCache[layer.type]).hasOwnProperty(layer.id)
  356. )
  357. }
  358. /**
  359. * Returns a layer by id
  360. * @param id
  361. * @returns {*|undefined}
  362. */
  363. getLayer(id) {
  364. let filters = this.getLayers().filter(item => item.id === id)
  365. return filters && filters.length ? filters[0] : undefined
  366. }
  367. /**
  368. * Returns all layers
  369. * @returns {[]}
  370. */
  371. getLayers() {
  372. let result = []
  373. Object.keys(this._layerCache).forEach(type => {
  374. let cache = this._layerCache[type]
  375. Object.keys(cache).forEach(layerId => {
  376. result.push(cache[layerId])
  377. })
  378. })
  379. return result
  380. }
  381. /**
  382. * Iterate through each layer and pass it as an argument to the callback function
  383. * @param method
  384. * @param context
  385. * @returns {Viewer}
  386. */
  387. eachLayer(method, context) {
  388. Object.keys(this._layerCache).forEach(type => {
  389. let cache = this._layerCache[type]
  390. Object.keys(cache).forEach(layerId => {
  391. method.call(context, cache[layerId])
  392. })
  393. })
  394. return this
  395. }
  396. /**
  397. * @param target
  398. * @param duration
  399. * @returns {Viewer}
  400. */
  401. flyTo(target, duration) {
  402. this._delegate.flyTo(target?.delegate || target, {
  403. duration
  404. })
  405. return this
  406. }
  407. /**
  408. * @param target
  409. * @returns {Viewer}
  410. */
  411. zoomTo(target) {
  412. this._delegate.zoomTo(target?.delegate || target)
  413. return this
  414. }
  415. /**
  416. * Camera fly to a position
  417. * @param position
  418. * @param completeCallback
  419. * @param duration
  420. * @returns {Viewer}
  421. */
  422. flyToPosition(position, completeCallback, duration) {
  423. position = Parse.parsePosition(position)
  424. this.camera.flyTo({
  425. destination: Transform.transformWGS84ToCartesian(position),
  426. orientation: {
  427. heading: Cesium.Math.toRadians(position.heading),
  428. pitch: Cesium.Math.toRadians(position.pitch),
  429. roll: Cesium.Math.toRadians(position.roll)
  430. },
  431. complete: completeCallback,
  432. duration: duration
  433. })
  434. return this
  435. }
  436. /**
  437. * Camera zoom to a position
  438. * @param position
  439. * @param completeCallback
  440. * @returns {Viewer}
  441. */
  442. zoomToPosition(position, completeCallback) {
  443. this.flyToPosition(position, completeCallback, 0)
  444. return this
  445. }
  446. /**
  447. *
  448. * @param type
  449. * @param callback
  450. * @param context
  451. * @returns {Viewer}
  452. */
  453. on(type, callback, context) {
  454. this._viewerEvent.on(type, callback, context || this)
  455. this._sceneEvent.on(type, callback, context || this)
  456. return this
  457. }
  458. /**
  459. *
  460. * @param type
  461. * @param callback
  462. * @param context
  463. * @returns {Viewer}
  464. */
  465. once(type, callback, context) {
  466. this._viewerEvent.once(type, callback, context || this)
  467. return this
  468. }
  469. /**
  470. *
  471. * @param type
  472. * @param callback
  473. * @param context
  474. * @returns {Viewer}
  475. */
  476. off(type, callback, context) {
  477. this._viewerEvent.off(type, callback, context || this)
  478. this._sceneEvent.off(type, callback, context || this)
  479. return this
  480. }
  481. /**
  482. * Destroys the viewer.
  483. */
  484. destroy() {
  485. this._delegate.destroy()
  486. this._delegate = undefined
  487. return this
  488. }
  489. /**
  490. * Export scene to image
  491. * @param name
  492. * @returns {Viewer}
  493. */
  494. exportScene(name) {
  495. let canvas = this.canvas
  496. let image = canvas
  497. .toDataURL('image/png')
  498. .replace('image/png', 'image/octet-stream')
  499. let link = document.createElement('a')
  500. let blob = Util.dataURLtoBlob(image)
  501. let objUrl = URL.createObjectURL(blob)
  502. link.download = `${name || 'scene'}.png`
  503. link.href = objUrl
  504. link.click()
  505. return this
  506. }
  507. /**
  508. * Adds a plugin
  509. * @param plugin
  510. * @returns {Viewer}
  511. */
  512. use(plugin) {
  513. if (plugin && plugin.install) {
  514. plugin.install(this)
  515. }
  516. return this
  517. }
  518. }
  519. export default Viewer