吉安感知网项目-前端
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
import * as Cesium from 'cesium'
import { MapTooltip } from '@ztzf/utils'
import { ToolBase } from '../ToolBase'
import { buildEllipsePositions } 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,
})
 
export class DrawEllipseTool extends ToolBase {
    constructor(viewer, options = {}) {
        super(viewer)
        this.tooltip = new MapTooltip(viewer)
        this.dataSource = null
        this.ellipseEntity = null
        this.centerCartesian = null
        this.majorPoint = null
        this.minorPoint = null
        this.semiMajor = 0
        this.semiMinor = 0
        this.isDrawing = false
        this.drawStep = 0
        this.isCompleted = false
        this.style = resolveStyle(options?.style)
    }
 
    start() {
        this.dataSource = new Cesium.CustomDataSource('draw-ellipse')
        this.viewer.dataSources.add(this.dataSource)
        this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
        this.handler.setInputAction(click => this.handleLeftClick(click), Cesium.ScreenSpaceEventType.LEFT_CLICK)
        this.handler.setInputAction(movement => this.handleMouseMove(movement), Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        this.drawStep = 0
        this.isCompleted = false
        this.tooltip.hide()
    }
 
    handleLeftClick(click) {
        if (this.isCompleted) return
        const position = this.getPositionFromScreen(click.position)
        if (!position) return
        if (this.drawStep === 0) {
            this.centerCartesian = position
            this.semiMajor = 1
            this.semiMinor = 1
            this.isDrawing = true
            this.drawStep = 1
            this.createEntities()
            this.tooltip.show('单击设置长轴', click.position)
            return
        }
        if (this.drawStep === 1) {
            this.majorPoint = position
            this.semiMajor = Math.max(1, this.getSurfaceDistance(this.centerCartesian, this.majorPoint))
            this.semiMinor = this.semiMajor
            this.drawStep = 2
            this.tooltip.show('单击设置短轴', click.position)
            return
        }
        if (this.drawStep === 2) {
            this.minorPoint = position
            const minorRadius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, this.minorPoint))
            this.semiMinor = Math.min(minorRadius, this.semiMajor)
            this.isDrawing = false
            this.drawStep = 0
            this.isCompleted = true
            const positions = buildEllipsePositions(this.centerCartesian, this.semiMajor, this.semiMinor)
            this.notify('getPolygonPositions', {
                positions,
                meta: {
                    center: this.centerCartesian,
                    majorPoint: this.majorPoint,
                    minorPoint: this.minorPoint,
                    semiMajor: this.semiMajor,
                    semiMinor: this.semiMinor,
                },
            })
            this.tooltip.hide()
            this.clearPreviewEntities()
        }
    }
 
    handleMouseMove(movement) {
        if (this.isCompleted) return
        const tipText = this.drawStep === 0
            ? '单击设置中心点'
            : this.drawStep === 1
                ? '单击设置长轴'
                : '单击设置短轴'
        if (!this.tooltip?.isVisible) {
            this.tooltip.show(tipText, movement.endPosition)
        } else {
            this.tooltip.show(tipText)
            this.tooltip.move(movement.endPosition)
        }
        if (!this.isDrawing) return
        const position = this.getPositionFromScreen(movement.endPosition)
        if (!position) return
        if (this.drawStep === 1) {
            const radius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
            this.semiMajor = radius
            this.semiMinor = radius
            return
        }
        if (this.drawStep === 2) {
            const minorRadius = Math.max(1, this.getSurfaceDistance(this.centerCartesian, position))
            this.semiMinor = Math.min(minorRadius, this.semiMajor)
        }
    }
 
    createEntities() {
        if (this.ellipseEntity) return
        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,
            },
        })
    }
 
    getSurfaceDistance(startCartesian, endCartesian) {
        const start = Cesium.Cartographic.fromCartesian(startCartesian)
        const end = Cesium.Cartographic.fromCartesian(endCartesian)
        const geodesic = new Cesium.EllipsoidGeodesic(start, end)
        return geodesic.surfaceDistance
    }
 
    clearPreviewEntities() {
        if (!this.dataSource) return
        this.dataSource.entities.removeAll()
        this.ellipseEntity = null
    }
 
    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
        }
    }
 
    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_CLICK)
            this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
            this.handler.destroy()
            this.handler = null
        }
        this.tooltip?.destroy()
        this.tooltip = null
        this.centerCartesian = null
        this.majorPoint = null
        this.minorPoint = null
        this.isDrawing = false
        this.drawStep = 0
        this.isCompleted = false
    }
}