Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

EditPolygon.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-30 23:12:09
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { Transform } from '@dc-modules/transform'
  7. import Edit from './Edit'
  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. if (!position) {
  48. return false
  49. }
  50. this._pickedAnchor.position.setValue(position)
  51. let properties = this._pickedAnchor.properties.getValue(
  52. Cesium.JulianDate.now()
  53. )
  54. let currentIndex = properties.index
  55. if (properties.isMid) {
  56. let preMidPosition
  57. let nextMidPosition
  58. let len = this._positions.length
  59. if (currentIndex === len - 1) {
  60. preMidPosition = this.computeMidPosition(
  61. this._positions[currentIndex],
  62. this._positions[currentIndex - 1]
  63. )
  64. nextMidPosition = this.computeMidPosition(
  65. this._positions[currentIndex],
  66. this._positions[0]
  67. )
  68. } else {
  69. preMidPosition = this.computeMidPosition(
  70. this._positions[currentIndex],
  71. this._positions[currentIndex - 1]
  72. )
  73. nextMidPosition = this.computeMidPosition(
  74. this._positions[currentIndex],
  75. this._positions[currentIndex + 1]
  76. )
  77. }
  78. this._positions.splice(
  79. properties.index,
  80. 1,
  81. preMidPosition,
  82. position,
  83. nextMidPosition
  84. )
  85. this._anchorLayer.removeAll()
  86. this._anchors = []
  87. this._positions.forEach((item, index) => {
  88. this.createAnchor(item, index, index % 2 !== 0)
  89. })
  90. }
  91. }
  92. } else {
  93. this._isMoving = true
  94. if (!e.target.id) {
  95. return false
  96. }
  97. this._pickedAnchor = e.target.id
  98. }
  99. }
  100. _onMouseMove(e) {
  101. this._tooltip.showAt(e.windowPosition, '点击锚点移动,右击结束编辑')
  102. if (!this._isMoving) {
  103. return false
  104. }
  105. if (this._pickedAnchor && this._pickedAnchor.position) {
  106. let properties = this._pickedAnchor.properties.getValue(
  107. Cesium.JulianDate.now()
  108. )
  109. let position = this._clampToGround ? e.surfacePosition : e.position
  110. if (!position) {
  111. return false
  112. }
  113. let currentIndex = properties.index
  114. this._pickedAnchor.position.setValue(position)
  115. this._positions[currentIndex] = position
  116. let len = this._positions.length
  117. if (!properties.isMid) {
  118. let preAnchorIndex = -1
  119. let preMidAnchorIndex = -1
  120. let nextAnchorIndex = -1
  121. let nextMidAnchorIndex = -1
  122. if (currentIndex === 0) {
  123. preAnchorIndex = len - 2
  124. preMidAnchorIndex = len - 1
  125. nextAnchorIndex = currentIndex + 2
  126. nextMidAnchorIndex = currentIndex + 1
  127. } else if (currentIndex === len - 2) {
  128. preAnchorIndex = currentIndex - 2
  129. preMidAnchorIndex = currentIndex - 1
  130. nextAnchorIndex = 0
  131. nextMidAnchorIndex = len - 1
  132. } else {
  133. preAnchorIndex = currentIndex - 2
  134. preMidAnchorIndex = currentIndex - 1
  135. nextAnchorIndex = currentIndex + 2
  136. nextMidAnchorIndex = currentIndex + 1
  137. }
  138. let preMidPosition = this.computeMidPosition(
  139. this._positions[preAnchorIndex],
  140. this._positions[currentIndex]
  141. )
  142. let nextMidPosition = this.computeMidPosition(
  143. this._positions[nextAnchorIndex],
  144. this._positions[currentIndex]
  145. )
  146. this._positions[preMidAnchorIndex] = preMidPosition
  147. this._positions[nextMidAnchorIndex] = nextMidPosition
  148. this._anchors[preMidAnchorIndex].position.setValue(preMidPosition)
  149. this._anchors[nextMidAnchorIndex].position.setValue(nextMidPosition)
  150. }
  151. }
  152. }
  153. _onRightClick(e) {
  154. this.unbindEvent()
  155. this._overlay.positions = Transform.transformCartesianArrayToWGS84Array(
  156. this._positions
  157. )
  158. this._overlay.show = true
  159. this._plotEvent.raiseEvent(this._overlay)
  160. }
  161. }
  162. export default EditPolygon