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.

пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
пре 5 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * @Author: Caven
  3. * @Date: 2020-01-09 09:10:37
  4. * @Last Modified by: Caven
  5. * @Last Modified time: 2020-02-29 18:54:09
  6. */
  7. import Overlay from '../Overlay'
  8. import Cesium from '@/namespace'
  9. DC.Polygon = class extends Overlay {
  10. constructor(positions) {
  11. if (
  12. !positions ||
  13. (typeof positions !== 'string' && !Array.isArray(positions))
  14. ) {
  15. throw new Error('the positions invalid')
  16. }
  17. super()
  18. this._positions = this._preparePositions(positions)
  19. this._holes = []
  20. this._delegate = new Cesium.Entity()
  21. this._state = DC.OverlayState.INITIALIZED
  22. this.type = DC.OverlayType.POLYGON
  23. }
  24. set positions(positions) {
  25. this._positions = this._preparePositions(positions)
  26. }
  27. get positions() {
  28. return this._positions
  29. }
  30. set holes(holes) {
  31. if (holes && holes.length) {
  32. this._holes = holes.map(item => this._preparePositions(item))
  33. }
  34. }
  35. get holes() {
  36. return this._holes
  37. }
  38. get center() {
  39. let boundingSphere = Cesium.BoundingSphere.fromPoints(
  40. DC.T.transformWSG84ArrayToCartesianArray(this._positions)
  41. )
  42. return DC.T.transformCartesianToWSG84(boundingSphere.center)
  43. }
  44. get area() {
  45. let result = 0
  46. if (this._positions) {
  47. let h = 0
  48. let ellipsoid = Cesium.Ellipsoid.WGS84
  49. let positions = [...this._positions]
  50. positions.push(positions[0])
  51. for (let i = 1; i < positions.length; i++) {
  52. let oel = ellipsoid.cartographicToCartesian(
  53. DC.T.transformWSG84ToCartographic(positions[i - 1])
  54. )
  55. let el = ellipsoid.cartographicToCartesian(
  56. DC.T.transformWSG84ToCartographic(positions[i])
  57. )
  58. h += oel.x * el.y - el.x * oel.y
  59. }
  60. result = Math.abs(h).toFixed(2)
  61. }
  62. return result
  63. }
  64. /**
  65. * prepare entity
  66. */
  67. _preparePositions(positions) {
  68. if (typeof positions === 'string') {
  69. if (positions.indexOf('#') >= 0) {
  70. throw new Error('the positions invalid')
  71. }
  72. positions = positions.split(';')
  73. }
  74. return positions.map(item => {
  75. if (Array.isArray(item)) {
  76. return DC.Position.fromCoordArray(item)
  77. } else if (item instanceof DC.Position) {
  78. return item
  79. } else {
  80. return DC.Position.fromCoordString(item)
  81. }
  82. })
  83. }
  84. _prepareHierarchy() {
  85. let result = new Cesium.PolygonHierarchy()
  86. result.positions = DC.T.transformWSG84ArrayToCartesianArray(this._positions)
  87. result.holes = this._holes.map(
  88. item =>
  89. new Cesium.PolygonHierarchy(
  90. DC.T.transformWSG84ArrayToCartesianArray(item)
  91. )
  92. )
  93. return result
  94. }
  95. /**
  96. * prepare entity
  97. */
  98. _prepareDelegate() {
  99. /**
  100. * initialize the Overlay parameter
  101. */
  102. this._delegate.polygon = {
  103. ...this._style,
  104. hierarchy: new Cesium.CallbackProperty(time => {
  105. return this._prepareHierarchy()
  106. })
  107. }
  108. this._delegate.layer = this._layer
  109. this._delegate.overlayId = this._id
  110. }
  111. /**
  112. *
  113. * @param {*} style
  114. */
  115. setStyle(style) {
  116. if (!style || Object.keys(style).length === 0) {
  117. return
  118. }
  119. this._style = style
  120. this._delegate.polygon && DC.Util.merge(this._delegate.polygon, this._style)
  121. return this
  122. }
  123. /**
  124. *
  125. * @param {*} entity
  126. */
  127. static fromEntity(entity) {
  128. let polygon = undefined
  129. if (entity.polygon) {
  130. let positions = DC.T.transformCartesianArrayToWSG84Array(
  131. item.polygon.hierarchy.getValue(Cesium.JulianDate.now()).positions
  132. )
  133. polygon = new DC.Polygon(positions)
  134. polygon.attr = {
  135. ...entity.properties
  136. }
  137. }
  138. return polygon
  139. }
  140. }
  141. DC.OverlayType.POLYGON = 'polygon'