吉安感知网项目-前端
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import * as Cesium from 'cesium'
import { MapTooltip } from '@ztzf/utils'
import { ToolBase } from '../ToolBase'
import {
    buildEllipsePositions,
    cartesianToLocal,
    formatBufferRadius,
    getBufferRadiusLabelStyle,
    getBufferRadiusPoint,
} from '../shapeUtils'
 
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)
}
 
export class EditBufferCircleTool extends ToolBase {
    constructor(viewer) {
        super(viewer)
        this.tooltip = new MapTooltip(viewer)
        this.dataSource = null
        this.centerCartesian = null
        this.radiusLevel1 = 0
        this.radiusLevel2 = 0
        this.radiusLevel3 = 0
        this.dragType = null
        this.circleLevel1Entity = null
        this.circleLevel2Entity = null
        this.circleLevel3Entity = null
        this.circleLevel1OutlineEntity = null
        this.circleLevel2OutlineEntity = null
        this.circleLevel3OutlineEntity = null
        this.centerEntity = null
        this.radiusLevel1Entity = null
        this.radiusLevel2Entity = null
        this.radiusLevel3Entity = null
        this.radiusLevel1LabelEntity = null
        this.radiusLevel2LabelEntity = null
        this.radiusLevel3LabelEntity = null
    }
 
    start(data = []) {
        const inputPoints = Array.isArray(data?.points) ? data.points : data
        const normalized = normalizePoints(inputPoints)
        const metaCenter = resolveCartesian(data?.meta?.center)
        if (metaCenter) {
            this.centerCartesian = metaCenter
        } else {
            if (!normalized.length) return
            const centerLng = normalized.reduce((sum, p) => sum + p.lng, 0) / normalized.length
            const centerLat = normalized.reduce((sum, p) => sum + p.lat, 0) / normalized.length
            this.centerCartesian = Cesium.Cartesian3.fromDegrees(centerLng, centerLat)
        }
        const radii = Array.isArray(data?.meta?.bufferRadii) ? data.meta.bufferRadii : null
        if (radii?.length) {
            const [r1, r2, r3] = radii
            this.radiusLevel1 = Number.isFinite(r1) && r1 > 0 ? r1 : 1
            this.radiusLevel2 = Number.isFinite(r2) && r2 > 0 ? Math.max(r2, this.radiusLevel1) : this.radiusLevel1
            this.radiusLevel3 = Number.isFinite(r3) && r3 > 0 ? Math.max(r3, this.radiusLevel2) : this.radiusLevel2
        } else {
            this.radiusLevel3 = this.estimateRadius(normalized)
            this.radiusLevel2 = this.radiusLevel3
            this.radiusLevel1 = this.radiusLevel3
        }
 
        this.dataSource = new Cesium.CustomDataSource('edit-buffer-circle')
        this.viewer.dataSources.add(this.dataSource)
        this.createEntities()
        this.initHandler()
    }
 
    estimateRadius(points) {
        const center = Cesium.Cartesian3.clone(this.centerCartesian)
        let max = 1
        points.forEach(point => {
            const cartesian = Cesium.Cartesian3.fromDegrees(point.lng, point.lat)
            const distance = Cesium.Cartesian3.distance(center, cartesian)
            max = Math.max(max, distance)
        })
        return max
    }
 
    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 === 'circle-center') this.dragType = 'center'
        if (picked.customType === 'circle-radius-1') this.dragType = 'radius-1'
        if (picked.customType === 'circle-radius-2') this.dragType = 'radius-2'
        if (picked.customType === 'circle-radius-3') this.dragType = 'radius-3'
        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.updateRadiusPoints()
            return
        }
 
        const local = cartesianToLocal(this.centerCartesian, position)
        const radius = Math.max(1, Math.sqrt(local.x * local.x + local.y * local.y))
        if (this.dragType === 'radius-1') {
            this.radiusLevel1 = radius
            if (this.radiusLevel2 < this.radiusLevel1) this.radiusLevel2 = this.radiusLevel1
            if (this.radiusLevel3 < this.radiusLevel2) this.radiusLevel3 = this.radiusLevel2
        }
        if (this.dragType === 'radius-2') {
            this.radiusLevel2 = Math.max(radius, this.radiusLevel1)
            if (this.radiusLevel3 < this.radiusLevel2) this.radiusLevel3 = this.radiusLevel2
        }
        if (this.dragType === 'radius-3') {
            this.radiusLevel3 = Math.max(radius, this.radiusLevel2)
        }
        this.updateRadiusPoints()
    }
 
    handleLeftUp() {
        if (!this.dragType) return
        this.dragType = null
        this.enableMapControl()
        const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel3, this.radiusLevel3)
        this.notify('getPolygonPositions', {
            positions,
            meta: {
                center: this.centerCartesian,
                bufferRadii: [this.radiusLevel1, this.radiusLevel2, this.radiusLevel3],
                controlPoints: [
                    this.centerCartesian,
                    this.getRadiusPoint(this.radiusLevel1),
                    this.getRadiusPoint(this.radiusLevel2),
                    this.getRadiusPoint(this.radiusLevel3),
                ],
            },
        })
    }
 
    createEntities() {
        this.circleLevel1Entity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
            ellipse: {
                semiMajorAxis: new Cesium.CallbackProperty(() => this.radiusLevel1, false),
                semiMinorAxis: new Cesium.CallbackProperty(() => this.radiusLevel1, false),
                material: Cesium.Color.fromBytes(247, 20, 20, 128),
                outline: false,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        })
        this.circleLevel1OutlineEntity = this.dataSource.entities.add({
            polyline: {
                positions: new Cesium.CallbackProperty(() => {
                    if (!this.centerCartesian || this.radiusLevel1 <= 0) return []
                    const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel1, this.radiusLevel1)
                    return positions.length ? [...positions, positions[0]] : positions
                }, false),
                clampToGround: true,
                width: 2,
                material: Cesium.Color.fromBytes(255, 0, 0, 255),
            },
        })
 
        this.circleLevel2Entity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
            ellipse: {
                semiMajorAxis: new Cesium.CallbackProperty(() => this.radiusLevel2, false),
                semiMinorAxis: new Cesium.CallbackProperty(() => this.radiusLevel2, false),
                material: Cesium.Color.fromBytes(255, 235, 59, 128),
                outline: false,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        })
        this.circleLevel2OutlineEntity = this.dataSource.entities.add({
            polyline: {
                positions: new Cesium.CallbackProperty(() => {
                    if (!this.centerCartesian || this.radiusLevel2 <= 0) return []
                    const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel2, this.radiusLevel2)
                    return positions.length ? [...positions, positions[0]] : positions
                }, false),
                clampToGround: true,
                width: 2,
                material: Cesium.Color.fromBytes(255, 235, 59, 255),
            },
        })
 
        this.circleLevel3Entity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.centerCartesian, false),
            ellipse: {
                semiMajorAxis: new Cesium.CallbackProperty(() => this.radiusLevel3, false),
                semiMinorAxis: new Cesium.CallbackProperty(() => this.radiusLevel3, false),
                material: Cesium.Color.fromBytes(25, 178, 230, 128),
                outline: false,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        })
        this.circleLevel3OutlineEntity = this.dataSource.entities.add({
            polyline: {
                positions: new Cesium.CallbackProperty(() => {
                    if (!this.centerCartesian || this.radiusLevel3 <= 0) return []
                    const positions = buildEllipsePositions(this.centerCartesian, this.radiusLevel3, this.radiusLevel3)
                    return positions.length ? [...positions, positions[0]] : positions
                }, false),
                clampToGround: true,
                width: 2,
                material: Cesium.Color.fromBytes(0, 251, 255, 255),
            },
        })
 
        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: 'circle-center',
        })
 
        this.radiusLevel1Entity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.getRadiusPoint(this.radiusLevel1), false),
            point: {
                pixelSize: 10,
                color: Cesium.Color.fromBytes(255, 255, 255, 200),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
            customType: 'circle-radius-1',
        })
 
        this.radiusLevel2Entity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.getRadiusPoint(this.radiusLevel2), false),
            point: {
                pixelSize: 10,
                color: Cesium.Color.fromBytes(255, 255, 255, 200),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
            customType: 'circle-radius-2',
        })
 
        this.radiusLevel3Entity = this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(() => this.getRadiusPoint(this.radiusLevel3), false),
            point: {
                pixelSize: 10,
                color: Cesium.Color.fromBytes(255, 255, 255, 200),
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
            customType: 'circle-radius-3',
        })
 
        this.radiusLevel1LabelEntity = this.createRadiusLabelEntity(() => this.radiusLevel1)
        this.radiusLevel2LabelEntity = this.createRadiusLabelEntity(() => this.radiusLevel2)
        this.radiusLevel3LabelEntity = this.createRadiusLabelEntity(() => this.radiusLevel3)
    }
 
    updateRadiusPoints() {
        if (this.radiusLevel1Entity) this.radiusLevel1Entity.position = this.getRadiusPoint(this.radiusLevel1)
        if (this.radiusLevel2Entity) this.radiusLevel2Entity.position = this.getRadiusPoint(this.radiusLevel2)
        if (this.radiusLevel3Entity) this.radiusLevel3Entity.position = this.getRadiusPoint(this.radiusLevel3)
    }
 
    getPositions() {
        if (!this.centerCartesian) return []
        return {
            positions: buildEllipsePositions(this.centerCartesian, this.radiusLevel3, this.radiusLevel3),
            meta: {
                center: this.centerCartesian,
                bufferRadii: [this.radiusLevel1, this.radiusLevel2, this.radiusLevel3],
                controlPoints: [
                    this.centerCartesian,
                    this.getRadiusPoint(this.radiusLevel1),
                    this.getRadiusPoint(this.radiusLevel2),
                    this.getRadiusPoint(this.radiusLevel3),
                ],
            },
        }
    }
 
    getRadiusPoint(radius) {
        return getBufferRadiusPoint(this.centerCartesian, radius)
    }
 
    createRadiusLabelEntity(getRadius) {
        return this.dataSource.entities.add({
            position: new Cesium.CallbackProperty(
                () => getBufferRadiusPoint(this.centerCartesian, getRadius()),
                false
            ),
            label: {
                ...getBufferRadiusLabelStyle(),
                text: new Cesium.CallbackProperty(() => formatBufferRadius(getRadius()), false),
            },
        })
    }
 
    getPositionFromScreen(screenPosition) {
        const scene = this.viewer.scene
        const cartesian = scene.pickPosition(screenPosition)
        if (cartesian) return cartesian
        return scene.camera.pickEllipsoid(screenPosition, scene.globe.ellipsoid)
    }
 
    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.radiusLevel1 = 0
        this.radiusLevel2 = 0
        this.radiusLevel3 = 0
        this.circleLevel1OutlineEntity = null
        this.circleLevel2OutlineEntity = null
        this.circleLevel3OutlineEntity = null
        this.radiusLevel1LabelEntity = null
        this.radiusLevel2LabelEntity = null
        this.radiusLevel3LabelEntity = null
        this.dragType = null
    }
}