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.

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