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.

EditPolygon.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-30 23:12:09
  4. */
  5. import Edit from './Edit'
  6. const { Transform } = DC
  7. const { Cesium } = DC.Namespace
  8. class EditPolygon extends Edit {
  9. constructor(overlay) {
  10. super()
  11. this._overlay = overlay
  12. this._positions = []
  13. }
  14. _mountEntity() {
  15. this._delegate = new Cesium.Entity()
  16. this._delegate.merge(this._overlay.delegate)
  17. this._overlay.show = false
  18. this._delegate.polygon.hierarchy = new Cesium.CallbackProperty(time => {
  19. if (this._positions.length > 2) {
  20. return new Cesium.PolygonHierarchy(this._positions)
  21. } else {
  22. return null
  23. }
  24. }, false)
  25. this._layer.add(this._delegate)
  26. }
  27. _mountAnchor() {
  28. let positions = [].concat(
  29. this._overlay.delegate.polygon.hierarchy.getValue(Cesium.JulianDate.now())
  30. .positions
  31. )
  32. positions.push(positions[0])
  33. for (let i = 0; i < positions.length - 1; i++) {
  34. let mid = this.computeMidPosition(positions[i], positions[i + 1])
  35. this._positions.push(positions[i])
  36. this._positions.push(mid)
  37. }
  38. this._positions.forEach((item, index) => {
  39. this.createAnchor(item, index, index % 2 !== 0)
  40. })
  41. }
  42. _onClick(e) {
  43. if (this._isMoving) {
  44. this._isMoving = false
  45. if (this._pickedAnchor && this._pickedAnchor.position) {
  46. let position = this._clampToGround ? e.surfacePosition : e.position
  47. this._pickedAnchor.position.setValue(position)
  48. let properties = this._pickedAnchor.properties.getValue(
  49. Cesium.JulianDate.now()
  50. )
  51. let currentIndex = properties.index
  52. if (properties.isMid) {
  53. let preMidPosition
  54. let nextMidPosition
  55. let len = this._positions.length
  56. if (currentIndex === len - 1) {
  57. preMidPosition = this.computeMidPosition(
  58. this._positions[currentIndex],
  59. this._positions[currentIndex - 1]
  60. )
  61. nextMidPosition = this.computeMidPosition(
  62. this._positions[currentIndex],
  63. this._positions[0]
  64. )
  65. } else {
  66. preMidPosition = this.computeMidPosition(
  67. this._positions[currentIndex],
  68. this._positions[currentIndex - 1]
  69. )
  70. nextMidPosition = this.computeMidPosition(
  71. this._positions[currentIndex],
  72. this._positions[currentIndex + 1]
  73. )
  74. }
  75. this._positions.splice(
  76. properties.index,
  77. 1,
  78. preMidPosition,
  79. position,
  80. nextMidPosition
  81. )
  82. this._anchorLayer.removeAll()
  83. this._anchors = []
  84. this._positions.forEach((item, index) => {
  85. this.createAnchor(item, index, index % 2 !== 0)
  86. })
  87. }
  88. }
  89. } else {
  90. this._isMoving = true
  91. if (!e.target.id) {
  92. return false
  93. }
  94. this._pickedAnchor = e.target.id
  95. }
  96. }
  97. _onMouseMove(e) {
  98. this._tooltip.showAt(e.windowPosition, '点击锚点移动,右击结束编辑')
  99. if (!this._isMoving) {
  100. return
  101. }
  102. if (this._pickedAnchor && this._pickedAnchor.position) {
  103. let properties = this._pickedAnchor.properties.getValue(
  104. Cesium.JulianDate.now()
  105. )
  106. let position = this._clampToGround ? e.surfacePosition : e.position
  107. let currentIndex = properties.index
  108. this._pickedAnchor.position.setValue(position)
  109. this._positions[currentIndex] = position
  110. let len = this._positions.length
  111. if (!properties.isMid) {
  112. let preAnchorIndex = -1
  113. let preMidAnchorIndex = -1
  114. let nextAnchorIndex = -1
  115. let nextMidAnchorIndex = -1
  116. if (currentIndex === 0) {
  117. preAnchorIndex = len - 2
  118. preMidAnchorIndex = len - 1
  119. nextAnchorIndex = currentIndex + 2
  120. nextMidAnchorIndex = currentIndex + 1
  121. } else if (currentIndex === len - 2) {
  122. preAnchorIndex = currentIndex - 2
  123. preMidAnchorIndex = currentIndex - 1
  124. nextAnchorIndex = 0
  125. nextMidAnchorIndex = len - 1
  126. } else {
  127. preAnchorIndex = currentIndex - 2
  128. preMidAnchorIndex = currentIndex - 1
  129. nextAnchorIndex = currentIndex + 2
  130. nextMidAnchorIndex = currentIndex + 1
  131. }
  132. let preMidPosition = this.computeMidPosition(
  133. this._positions[preAnchorIndex],
  134. this._positions[currentIndex]
  135. )
  136. let nextMidPosition = this.computeMidPosition(
  137. this._positions[nextAnchorIndex],
  138. this._positions[currentIndex]
  139. )
  140. this._positions[preMidAnchorIndex] = preMidPosition
  141. this._positions[nextMidAnchorIndex] = nextMidPosition
  142. this._anchors[preMidAnchorIndex].position.setValue(preMidPosition)
  143. this._anchors[nextMidAnchorIndex].position.setValue(nextMidPosition)
  144. }
  145. }
  146. }
  147. _onRightClick(e) {
  148. this.unbindEvent()
  149. this._overlay.positions = Transform.transformCartesianArrayToWGS84Array(
  150. this._positions
  151. )
  152. this._overlay.show = true
  153. this._plotEvent.raiseEvent(this._overlay)
  154. }
  155. }
  156. export default EditPolygon