您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

EditPolygon.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 { midCartesian } from '@dc-modules/math'
  8. import { Transform } from '@dc-modules/transform'
  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. _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.polygon.hierarchy.getValue(
  46. Cesium.JulianDate.now()
  47. ).positions
  48. positions.push(positions[0])
  49. for (let i = 0; i < positions.length - 1; i++) {
  50. let mid = midCartesian(positions[i], positions[i + 1])
  51. this._positions.push(positions[i])
  52. this._positions.push(mid)
  53. }
  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
  74. let nextMidPosition
  75. let len = this._positions.length
  76. if (currentIndex === len - 1) {
  77. preMidPosition = midCartesian(
  78. this._positions[currentIndex],
  79. this._positions[currentIndex - 1]
  80. )
  81. nextMidPosition = midCartesian(
  82. this._positions[currentIndex],
  83. this._positions[0]
  84. )
  85. } else {
  86. preMidPosition = midCartesian(
  87. this._positions[currentIndex],
  88. this._positions[currentIndex - 1]
  89. )
  90. nextMidPosition = midCartesian(
  91. this._positions[currentIndex],
  92. this._positions[currentIndex + 1]
  93. )
  94. }
  95. this._positions.splice(
  96. currentIndex,
  97. 1,
  98. preMidPosition,
  99. position,
  100. nextMidPosition
  101. )
  102. this.editTool.fire(PlotEventType.CLEAR_ANCHOR)
  103. this._positions.forEach((item, index) => {
  104. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  105. position: item,
  106. index: index,
  107. isMid: index % 2 !== 0
  108. })
  109. })
  110. }
  111. }
  112. /**
  113. *
  114. * @param pickedAnchor
  115. * @param position
  116. * @private
  117. */
  118. _onAnchorMoving({ pickedAnchor, position }) {
  119. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  120. let currentIndex = properties.index
  121. this._positions[currentIndex] = position
  122. let len = this._positions.length
  123. if (!properties.isMid) {
  124. let preAnchorIndex = -1
  125. let preMidAnchorIndex = -1
  126. let nextAnchorIndex = -1
  127. let nextMidAnchorIndex = -1
  128. if (currentIndex === 0) {
  129. preAnchorIndex = len - 2
  130. preMidAnchorIndex = len - 1
  131. nextAnchorIndex = currentIndex + 2
  132. nextMidAnchorIndex = currentIndex + 1
  133. } else if (currentIndex === len - 2) {
  134. preAnchorIndex = currentIndex - 2
  135. preMidAnchorIndex = currentIndex - 1
  136. nextAnchorIndex = 0
  137. nextMidAnchorIndex = len - 1
  138. } else {
  139. preAnchorIndex = currentIndex - 2
  140. preMidAnchorIndex = currentIndex - 1
  141. nextAnchorIndex = currentIndex + 2
  142. nextMidAnchorIndex = currentIndex + 1
  143. }
  144. let preMidPosition = midCartesian(
  145. this._positions[preAnchorIndex],
  146. this._positions[currentIndex]
  147. )
  148. let nextMidPosition = midCartesian(
  149. this._positions[nextAnchorIndex],
  150. this._positions[currentIndex]
  151. )
  152. this._positions[preMidAnchorIndex] = preMidPosition
  153. this._positions[nextMidAnchorIndex] = nextMidPosition
  154. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  155. index: preMidAnchorIndex,
  156. position: preMidPosition
  157. })
  158. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  159. index: nextMidAnchorIndex,
  160. position: nextMidPosition
  161. })
  162. }
  163. }
  164. }
  165. export default EditPolygon