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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-08-30 23:12:09
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { PlotEventType } from '@dc-modules/event'
  7. import { Transform } from '@dc-modules/transform'
  8. import { midCartesian } from '@dc-modules/math'
  9. import Edit from './Edit'
  10. class EditPolygon extends Edit {
  11. constructor(overlay) {
  12. super(overlay)
  13. }
  14. /**
  15. *
  16. * @private
  17. */
  18. _mountedHook() {
  19. this._delegate.polygon.hierarchy = new Cesium.CallbackProperty(time => {
  20. if (this._positions.length > 2) {
  21. return new Cesium.PolygonHierarchy(this._positions)
  22. } else {
  23. return null
  24. }
  25. }, false)
  26. this._layer.entities.add(this._delegate)
  27. }
  28. /**
  29. *
  30. * @private
  31. */
  32. _mountAnchor() {
  33. this._positions = []
  34. let positions = this._overlay.delegate.polygon.hierarchy.getValue(Cesium.JulianDate.now())
  35. .positions
  36. positions.push(positions[0])
  37. for (let i = 0; i < positions.length - 1; i++) {
  38. let mid = midCartesian(positions[i], positions[i + 1])
  39. this._positions.push(positions[i])
  40. this._positions.push(mid)
  41. }
  42. this._positions.forEach((item, index) => {
  43. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  44. position: item,
  45. index: index,
  46. isMid: index % 2 !== 0
  47. })
  48. })
  49. }
  50. /**
  51. *
  52. * @param pickedAnchor
  53. * @param position
  54. * @returns {boolean}
  55. * @private
  56. */
  57. _onEditAnchorStop({ pickedAnchor, position }) {
  58. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  59. let currentIndex = properties.index
  60. if (properties.isMid) {
  61. let preMidPosition
  62. let nextMidPosition
  63. let len = this._positions.length
  64. if (currentIndex === len - 1) {
  65. preMidPosition = midCartesian(
  66. this._positions[currentIndex],
  67. this._positions[currentIndex - 1]
  68. )
  69. nextMidPosition = midCartesian(
  70. this._positions[currentIndex],
  71. this._positions[0]
  72. )
  73. } else {
  74. preMidPosition = midCartesian(
  75. this._positions[currentIndex],
  76. this._positions[currentIndex - 1]
  77. )
  78. nextMidPosition = midCartesian(
  79. this._positions[currentIndex],
  80. this._positions[currentIndex + 1]
  81. )
  82. }
  83. this._positions.splice(
  84. currentIndex,
  85. 1,
  86. preMidPosition,
  87. position,
  88. nextMidPosition
  89. )
  90. this.editTool.fire(PlotEventType.CLEAR_ANCHOR)
  91. this._positions.forEach((item, index) => {
  92. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  93. position: item,
  94. index: index,
  95. isMid: index % 2 !== 0
  96. })
  97. })
  98. }
  99. }
  100. /**
  101. *
  102. * @param pickedAnchor
  103. * @param position
  104. * @private
  105. */
  106. _onAnchorMoving({ pickedAnchor, position }) {
  107. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  108. let currentIndex = properties.index
  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 = midCartesian(
  133. this._positions[preAnchorIndex],
  134. this._positions[currentIndex]
  135. )
  136. let nextMidPosition = midCartesian(
  137. this._positions[nextAnchorIndex],
  138. this._positions[currentIndex]
  139. )
  140. this._positions[preMidAnchorIndex] = preMidPosition
  141. this._positions[nextMidAnchorIndex] = nextMidPosition
  142. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  143. index: preMidAnchorIndex,
  144. position: preMidPosition
  145. })
  146. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  147. index: nextMidAnchorIndex,
  148. position: nextMidPosition
  149. })
  150. }
  151. }
  152. }
  153. export default EditPolygon