Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

EditPolygon.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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(
  22. this._positions.map(item => item.clone())
  23. )
  24. } else {
  25. return null
  26. }
  27. }, false)
  28. this._layer.entities.add(this._delegate)
  29. }
  30. /**
  31. *
  32. * @private
  33. */
  34. _stopedHook() {
  35. this._overlay.positions = Transform.transformCartesianArrayToWGS84Array(
  36. this._positions.filter((item, index) => index % 2 === 0)
  37. )
  38. this._overlay.show = true
  39. this._options.onEditStop && this._options.onEditStop(this._overlay)
  40. }
  41. /**
  42. *
  43. * @private
  44. */
  45. _mountAnchor() {
  46. this._positions = []
  47. let positions = this._overlay.delegate.polygon.hierarchy.getValue(
  48. Cesium.JulianDate.now()
  49. ).positions
  50. positions.push(positions[0])
  51. for (let i = 0; i < positions.length - 1; i++) {
  52. let mid = midCartesian(positions[i], positions[i + 1])
  53. this._positions.push(positions[i])
  54. this._positions.push(mid)
  55. }
  56. this._positions.forEach((item, index) => {
  57. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  58. position: item,
  59. index: index,
  60. isMid: index % 2 !== 0
  61. })
  62. })
  63. }
  64. /**
  65. *
  66. * @param pickedAnchor
  67. * @param position
  68. * @returns {boolean}
  69. * @private
  70. */
  71. _onEditAnchorStop({ pickedAnchor, position }) {
  72. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  73. let currentIndex = properties.index
  74. if (properties.isMid) {
  75. let preMidPosition
  76. let nextMidPosition
  77. let len = this._positions.length
  78. if (currentIndex === len - 1) {
  79. preMidPosition = midCartesian(
  80. this._positions[currentIndex],
  81. this._positions[currentIndex - 1]
  82. )
  83. nextMidPosition = midCartesian(
  84. this._positions[currentIndex],
  85. this._positions[0]
  86. )
  87. } else {
  88. preMidPosition = midCartesian(
  89. this._positions[currentIndex],
  90. this._positions[currentIndex - 1]
  91. )
  92. nextMidPosition = midCartesian(
  93. this._positions[currentIndex],
  94. this._positions[currentIndex + 1]
  95. )
  96. }
  97. this._positions.splice(
  98. currentIndex,
  99. 1,
  100. preMidPosition,
  101. position,
  102. nextMidPosition
  103. )
  104. this.editTool.fire(PlotEventType.CLEAR_ANCHOR)
  105. this._positions.forEach((item, index) => {
  106. this.editTool.fire(PlotEventType.CREATE_ANCHOR, {
  107. position: item,
  108. index: index,
  109. isMid: index % 2 !== 0
  110. })
  111. })
  112. }
  113. }
  114. /**
  115. *
  116. * @param pickedAnchor
  117. * @param position
  118. * @private
  119. */
  120. _onAnchorMoving({ pickedAnchor, position }) {
  121. let properties = pickedAnchor.properties.getValue(Cesium.JulianDate.now())
  122. let currentIndex = properties.index
  123. this._positions[currentIndex] = position
  124. let len = this._positions.length
  125. if (!properties.isMid) {
  126. let preAnchorIndex = -1
  127. let preMidAnchorIndex = -1
  128. let nextAnchorIndex = -1
  129. let nextMidAnchorIndex = -1
  130. if (currentIndex === 0) {
  131. preAnchorIndex = len - 2
  132. preMidAnchorIndex = len - 1
  133. nextAnchorIndex = currentIndex + 2
  134. nextMidAnchorIndex = currentIndex + 1
  135. } else if (currentIndex === len - 2) {
  136. preAnchorIndex = currentIndex - 2
  137. preMidAnchorIndex = currentIndex - 1
  138. nextAnchorIndex = 0
  139. nextMidAnchorIndex = len - 1
  140. } else {
  141. preAnchorIndex = currentIndex - 2
  142. preMidAnchorIndex = currentIndex - 1
  143. nextAnchorIndex = currentIndex + 2
  144. nextMidAnchorIndex = currentIndex + 1
  145. }
  146. let preMidPosition = midCartesian(
  147. this._positions[preAnchorIndex],
  148. this._positions[currentIndex]
  149. )
  150. let nextMidPosition = midCartesian(
  151. this._positions[nextAnchorIndex],
  152. this._positions[currentIndex]
  153. )
  154. this._positions[preMidAnchorIndex] = preMidPosition
  155. this._positions[nextMidAnchorIndex] = nextMidPosition
  156. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  157. index: preMidAnchorIndex,
  158. position: preMidPosition
  159. })
  160. this.editTool.fire(PlotEventType.UPDATE_ANCHOR, {
  161. index: nextMidAnchorIndex,
  162. position: nextMidPosition
  163. })
  164. }
  165. }
  166. }
  167. export default EditPolygon