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.

GeoJsonLayer.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-13 10:13:53
  4. */
  5. import State from '../state/State'
  6. import Layer from './Layer'
  7. import VectorLayer from './VectorLayer'
  8. import { Billboard, Polyline, Polygon, Model } from '../overlay'
  9. const { Cesium } = DC.Namespace
  10. class GeoJsonLayer extends Layer {
  11. constructor(id, url, options = {}) {
  12. if (!url) {
  13. throw new Error('GeoJsonLayer:the url invalid')
  14. }
  15. super(id)
  16. this._delegate = Cesium.GeoJsonDataSource.load(url, options)
  17. this.type = Layer.getLayerType('geojson')
  18. this._state = State.INITIALIZED
  19. }
  20. set show(show) {
  21. this._show = show
  22. this._delegate &&
  23. this._delegate.then(dataSource => {
  24. dataSource.show = this._show
  25. })
  26. }
  27. get show() {
  28. return this._show
  29. }
  30. _createBillboard(entity) {
  31. if (entity.position && entity.billboard) {
  32. return Billboard.fromEntity(entity)
  33. }
  34. }
  35. /**
  36. * Returns polyline Entity
  37. * @param entity
  38. * @returns {any}
  39. * @private
  40. */
  41. _createPolyline(entity) {
  42. if (entity.polyline) {
  43. return Polyline.fromEntity(entity)
  44. }
  45. }
  46. /**
  47. * Returns polygon Entity
  48. * @param entity
  49. * @returns {any}
  50. * @private
  51. */
  52. _createPolygon(entity) {
  53. if (entity.polygon) {
  54. return Polygon.fromEntity(entity)
  55. }
  56. }
  57. /**
  58. * Returns model Entity
  59. * @param entity
  60. * @param modelUrl
  61. * @returns {Model}
  62. * @private
  63. */
  64. _createModel(entity, modelUrl) {
  65. if (entity) {
  66. return Model.fromEntity(entity, modelUrl)
  67. }
  68. }
  69. /**
  70. *
  71. * @param method
  72. * @param context
  73. * @returns {GeoJsonLayer}
  74. */
  75. eachOverlay(method, context) {
  76. if (this._delegate) {
  77. this._delegate.then(dataSource => {
  78. let entities = dataSource.entities.values
  79. entities.forEach(item => {
  80. method.call(context, item)
  81. })
  82. })
  83. return this
  84. }
  85. }
  86. /**
  87. * Converts to VectorLayer
  88. * @returns {VectorLayer}
  89. */
  90. toVectorLayer() {
  91. let layer = new VectorLayer(this._id)
  92. this.eachOverlay(item => {
  93. if (item.billboard) {
  94. layer.addOverlay(this._createBillboard(item))
  95. } else if (item.polyline) {
  96. layer.addOverlay(this._createPolyline(item))
  97. } else if (item.polygon) {
  98. layer.addOverlay(this._createPolygon(item))
  99. }
  100. }, this)
  101. return layer
  102. }
  103. /**
  104. * Converts to VectorLayer
  105. * @param modelUrl
  106. * @returns {VectorLayer}
  107. */
  108. toModelLayer(modelUrl) {
  109. let layer = new VectorLayer(this._id)
  110. this.eachOverlay(item => {
  111. layer.addOverlay(this._createModel(item, modelUrl))
  112. }, this)
  113. return layer
  114. }
  115. }
  116. Layer.registerType('geojson')
  117. export default GeoJsonLayer