tengsx
2023-04-07 36c045a4f518f6506eab91e48ee48ce7d1aaa21d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * @Author: Caven
 * @Date: 2020-08-30 23:50:53
 */
 
import { PlotEventType } from '@/utils/event/index.js'
 
class Edit {
    constructor(overlay) {
        this._viewer = undefined
        this._layer = undefined
        this._overlay = overlay
        this._overlay.show = false
        this._delegate = new global.DC.Namespace.Cesium.Entity()
        this._delegate.merge(overlay.delegate)
        this._options = {}
        this._positions = []
    }
 
    get curEditTool () {
        return this._viewer.curEditTool
    }
 
    /**
   *
   * @private
   */
    _mountedHook () {
        this._overlay.positions = global.DC.Transform.transformCartesianArrayToWGS84Array(
            this._positions
        )
        this._overlay.show = true
        this._options.onEditStop && this._options.onEditStop(this._overlay)
    }
 
    /**
   *
   * @private
   */
    _stopedHook () {
        let position
        if (this._overlay.customType == 'polygon' || this._overlay.customType == 'polyline') {
            position = this._positions.filter((item, index) => index % 2 === 0)
        } else {
            position = this._positions
        }
 
        this._overlay.positions = global.DC.Transform.transformCartesianArrayToWGS84Array(
            position
        )
 
        this._overlay.show = true
        this._options.onEditStop && this._options.onEditStop(this._overlay)
    }
 
    /**
   *
   * @private
   */
    _mountAnchor () {
        this._positions = [].concat(
            global.DC.Transform.transformWGS84ArrayToCartesianArray(this._overlay.positions)
        )
        this._positions.forEach((item, index) => {
            this.curEditTool.fire(PlotEventType.CREATE_ANCHOR, {
                position: item,
                index: index
            })
        })
    }
 
    /**
   *
   * @param pickedAnchor
   * @param position
   * @returns {boolean}
   * @private
   */
    _onEditAnchorStop ({ pickedAnchor, position }) {
        let properties = pickedAnchor.properties.getValue(global.DC.Namespace.Cesium.JulianDate.now())
        this._positions[properties.index] = position
    }
 
    /**
   *
   * @param pickedAnchor
   * @param position
   * @private
   */
    _onAnchorMoving ({ pickedAnchor, position }) {
        let properties = pickedAnchor.properties.getValue(global.DC.Namespace.Cesium.JulianDate.now())
        this._positions[properties.index] = position
    }
 
    /**
     *
     * @param pickedAnchor
     * @param position
     * @private
     */
    _onEditStop ({ pickedAnchor, position }) {
        this._unbindEvent()
        this._viewer.curEditTool.deactivate()
        this._layer.entities.remove(this._delegate)
        this._stopedHook()
    }
 
    /**
   *
   * @returns {Edit}
   * @private
   */
    _bindEvent () {
        this.curEditTool.on(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
        this.curEditTool.on(
            PlotEventType.EDIT_ANCHOR_STOP,
            this._onEditAnchorStop,
            this
        )
        this.curEditTool.on(PlotEventType.EDIT_STOP, this._onEditStop, this)
        return this
    }
 
    /**
   *
   * @private
   */
    _unbindEvent () {
        this.curEditTool.off(PlotEventType.ANCHOR_MOVING, this._onAnchorMoving, this)
        this.curEditTool.off(
            PlotEventType.EDIT_ANCHOR_STOP,
            this._onEditAnchorStop,
            this
        )
        this.curEditTool.off(PlotEventType.EDIT_STOP, this._onEditStop, this)
    }
 
    /**
   *
   * @param measure
   * @param options
   * @returns {Edit}
   */
    start (measure, options) {
        this._viewer = measure.viewer
        this._layer = measure.layer
        this._options = options
        this._viewer.curEditTool.tooltipMess = '点击锚点移动,右击结束编辑'
        this._viewer.curEditTool.activate(options)
        this._mountedHook()
        this._mountAnchor()
        this._unbindEvent()
        this._bindEvent()
        return this
    }
}
 
export default Edit