吉安感知网项目-前端
chenyao
yesterday c567cbca3b78a7e06a827acbab56a46657e31aa1
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import * as Cesium from 'cesium'
import { MapTooltip } from '@ztzf/utils'
import { ToolBase } from '../ToolBase'
import {
    getBoundsFromLngLats,
    getCenterFromBounds,
    getMetersBetween,
    buildEllipsePositions,
    cartesianToLocal,
} from '../shapeUtils'
 
const DEFAULT_STYLE = {
    fill: Cesium.Color.fromBytes(45, 140, 240, 99),
    outline: Cesium.Color.fromBytes(45, 140, 240, 255),
}
const resolveStyle = style => ({
    fill: style?.fill || DEFAULT_STYLE.fill,
    outline: style?.outline || DEFAULT_STYLE.outline,
})
 
const normalizePoints = points =>
    (points || []).map(point => ({
        lng: point.lng ?? point.longitude,
        lat: point.lat ?? point.latitude,
    }))
 
const resolveLngLat = point => {
    if (!point) return null
    const lng = point.lng ?? point.longitude
    const lat = point.lat ?? point.latitude
    if (Number.isFinite(lng) && Number.isFinite(lat)) {
        return { lng: Number(lng), lat: Number(lat), height: Number(point.height) || 0 }
    }
    return null
}
 
const resolveCartesian = point => {
    if (!point) return null
    if (Number.isFinite(point.x) && Number.isFinite(point.y) && Number.isFinite(point.z)) {
        return new Cesium.Cartesian3(point.x, point.y, point.z)
    }
    const lngLat = resolveLngLat(point)
    if (!lngLat) return null
    return Cesium.Cartesian3.fromDegrees(lngLat.lng, lngLat.lat, lngLat.height || 0)
}
 
const resolveCartographic = point => {
    if (!point) return null
    if (Number.isFinite(point.x) && Number.isFinite(point.y) && Number.isFinite(point.z)) {
        return Cesium.Cartographic.fromCartesian(point)
    }
    const lngLat = resolveLngLat(point)
    if (!lngLat) return null
    return Cesium.Cartographic.fromDegrees(lngLat.lng, lngLat.lat, lngLat.height || 0)
}
 
export class EditEllipseTool extends ToolBase {
    constructor(viewer, options = {}) {
        super(viewer)
        this.tooltip = new MapTooltip(viewer)
        this.dataSource = null
        this.centerCartesian = null
        this.semiMajor = 0
        this.semiMinor = 0
        this.dragType = null
        this.centerEntity = null
        this.majorEntity = null
        this.minorEntity = null
        this.ellipseEntity = null
        this.style = resolveStyle(options?.style)
    }
 
    start(data = []) {
        const inputPoints = Array.isArray(data?.points) ? data.points : data
        const meta = data?.meta || null
        const metaCenter = resolveCartesian(meta?.center)
        if (metaCenter) {
            this.centerCartesian = metaCenter
            const centerCarto = Cesium.Cartographic.fromCartesian(metaCenter)
            const majorFromMeta = Number.isFinite(meta?.semiMajor) ? Number(meta.semiMajor) : 0
            const minorFromMeta = Number.isFinite(meta?.semiMinor) ? Number(meta.semiMinor) : 0
            const majorPoint = resolveCartographic(meta?.majorPoint)
            const minorPoint = resolveCartographic(meta?.minorPoint)
            const majorDistance = majorFromMeta || (majorPoint ? getMetersBetween(centerCarto, majorPoint) : 0)
            const minorDistance = minorFromMeta || (minorPoint ? getMetersBetween(centerCarto, minorPoint) : 0)
            if (majorDistance > 0) this.semiMajor = Math.max(1, majorDistance)
            if (minorDistance > 0) this.semiMinor = Math.max(1, minorDistance)
            if (this.semiMinor > this.semiMajor) this.semiMinor = this.semiMajor
        }
 
        if (!this.centerCartesian || !this.semiMajor || !this.semiMinor) {
            const normalized = normalizePoints(inputPoints)
            const bounds = getBoundsFromLngLats(normalized)
            const center = getCenterFromBounds(bounds)
            if (!center) return
            this.centerCartesian = Cesium.Cartesian3.fromRadians(center.longitude, center.latitude)
            const eastWest = getMetersBetween(
                new Cesium.Cartographic(bounds.west, center.latitude),
                new Cesium.Cartographic(bounds.east, center.latitude)
            )
            const northSouth = getMetersBetween(
                new Cesium.Cartographic(center.longitude, bounds.south),
                new Cesium.Cartographic(center.longitude, bounds.north)
            )
            this.semiMajor = Math.max(1, eastWest / 2)
            this.semiMinor = Math.max(1, northSouth / 2)
        }
 
        this.dataSource = new Cesium.CustomDataSource('edit-ellipse')
        this.viewer.dataSources.add(this.dataSource)
        this.createEntities()
        this.initHandler()
    }
 
    initHandler() {
        this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
        this.handler.setInputAction(evt => this.handleLeftDown(evt), Cesium.ScreenSpaceEventType.LEFT_DOWN)
        this.handler.setInputAction(evt => this.handleMouseMove(evt), Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        this.handler.setInputAction(() => this.handleLeftUp(), Cesium.ScreenSpaceEventType.LEFT_UP)
        this.tooltip.hide()
    }
 
    handleLeftDown(evt) {
        const picked = this.viewer.scene.pick(evt.position)?.id
        if (!picked) return
        if (picked.customType === 'ellipse-center') this.dragType = 'center'
        if (picked.customType === 'ellipse-major') this.dragType = 'major'
        if (picked.customType === 'ellipse-minor') this.dragType = 'minor'
        if (this.dragType) this.disableMapControl()
    }
 
    handleMouseMove(evt) {
        if (!this.tooltip?.isVisible) {
            this.tooltip.show('拖动控制点编辑', evt.endPosition)
        } else {
            this.tooltip.show('拖动控制点编辑')
            this.tooltip.move(evt.endPosition)
        }
        if (!this.dragType) return
        const position = this.getPositionFromScreen(evt.endPosition)
        if (!position) return
 
        if (this.dragType === 'center') {
            this.centerCartesian = position
            this.updateAxisPoints()
            return
        }
 
        const local = cartesianToLocal(this.centerCartesian, position)
        if (this.dragType === 'major') {
            this.semiMajor = Math.max(1, Math.abs(local.x))
            if (this.semiMajor < this.semiMinor) {
                this.semiMajor = this.semiMinor
            }
        }
        if (this.dragType === 'minor') {
            this.semiMinor = Math.max(1, Math.abs(local.y))
            if (this.semiMinor > this.semiMajor) {
                this.semiMinor = this.semiMajor
            }
        }
        this.updateAxisPoints()
    }
 
    handleLeftUp() {
        if (!this.dragType) return
        this.dragType = null
        this.enableMapControl()
        const positions = buildEllipsePositions(this.centerCartesian, this.semiMajor, this.semiMinor)
        this.notify('getPolygonPositions', {
            positions,
            meta: {
                center: this.centerCartesian,
                majorPoint: this.getAxisPoint(this.semiMajor, 0),
                minorPoint: this.getAxisPoint(0, this.semiMinor),
                semiMajor: this.semiMajor,
                semiMinor: this.semiMinor,
            },
        })
    }
 
    createEntities() {
        this.ellipseEntity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
            ellipse: {
                semiMajorAxis: new Cesium.CallbackProperty(() => this.semiMajor, false),
                semiMinorAxis: new Cesium.CallbackProperty(() => this.semiMinor, false),
                material: this.style.fill,
                outline: true,
                outlineColor: this.style.outline,
                outlineWidth: 2,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        })
 
        this.centerEntity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
            point: {
                pixelSize: 12,
                color: Cesium.Color.WHITE,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
            customType: 'ellipse-center',
        })
 
        this.majorEntity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.getAxisPoint(this.semiMajor, 0), false),
            point: {
                pixelSize: 10,
                color: Cesium.Color.fromBytes(255, 255, 255, 200),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
            customType: 'ellipse-major',
        })
 
        this.minorEntity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.getAxisPoint(0, this.semiMinor), false),
            point: {
                pixelSize: 10,
                color: Cesium.Color.fromBytes(255, 255, 255, 200),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
            customType: 'ellipse-minor',
        })
    }
 
    updateAxisPoints() {
        if (!this.majorEntity || !this.minorEntity) return
        this.majorEntity.position = this.getAxisPoint(this.semiMajor, 0)
        this.minorEntity.position = this.getAxisPoint(0, this.semiMinor)
    }
 
    getPositions() {
        if (!this.centerCartesian) return []
        return {
            positions: buildEllipsePositions(this.centerCartesian, this.semiMajor, this.semiMinor),
            meta: {
                center: this.centerCartesian,
                majorPoint: this.getAxisPoint(this.semiMajor, 0),
                minorPoint: this.getAxisPoint(0, this.semiMinor),
                semiMajor: this.semiMajor,
                semiMinor: this.semiMinor,
            },
        }
    }
 
    getAxisPoint(eastMeters, northMeters) {
        const frame = Cesium.Transforms.eastNorthUpToFixedFrame(this.centerCartesian)
        const local = new Cesium.Cartesian3(eastMeters, northMeters, 0)
        return Cesium.Matrix4.multiplyByPoint(frame, local, new Cesium.Cartesian3())
    }
 
    getPositionFromScreen(screenPosition) {
        const scene = this.viewer.scene
        const cartesian = scene.pickPosition(screenPosition)
        if (cartesian) return cartesian
        return scene.camera.pickEllipsoid(screenPosition, scene.globe.ellipsoid)
    }
 
    setStyle(style) {
        this.style = resolveStyle(style)
        if (this.ellipseEntity?.ellipse) {
            this.ellipseEntity.ellipse.material = this.style.fill
            this.ellipseEntity.ellipse.outlineColor = this.style.outline
        }
    }
 
    disableMapControl() {
        const controller = this.viewer.scene.screenSpaceCameraController
        controller.enableRotate = false
        controller.enableTranslate = false
        controller.enableZoom = false
    }
 
    enableMapControl() {
        const controller = this.viewer.scene.screenSpaceCameraController
        controller.enableRotate = true
        controller.enableTranslate = true
        controller.enableZoom = true
    }
 
    destroy() {
        if (this.dataSource) {
            this.dataSource.entities.removeAll()
            this.viewer.dataSources.remove(this.dataSource)
            this.dataSource = null
        }
        if (this.handler) {
            this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOWN)
            this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_UP)
            this.handler.destroy()
            this.handler = null
        }
        this.tooltip?.destroy()
        this.tooltip = null
        this.centerCartesian = null
        this.semiMajor = 0
        this.semiMinor = 0
        this.dragType = null
    }
}