吉安感知网项目-前端
罗广辉
2026-01-31 4cd17f4e640ae22ec64eb170468d1154d9a1eff5
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
import * as Cesium from 'cesium'
import { MapTooltip } from '@ztzf/utils'
import { ToolBase } from '../ToolBase'
import { getBoundsFromCartesians, rectanglePositionsFromBounds } 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 DrawRectangleTool extends ToolBase {
    constructor(viewer, options = {}) {
        super(viewer)
        this.tooltip = new MapTooltip(viewer)
        this.dataSource = null
        this.rectangleEntity = null
        this.outlineEntity = null
        this.startPosition = null
        this.endPosition = null
        this.isDrawing = false
        this.style = resolveStyle(options?.style)
    }
 
    start() {
        this.dataSource = new Cesium.CustomDataSource('draw-rectangle')
        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.tooltip.show('单击设置起点', { x: 0, y: 0 })
    }
 
    handleLeftClick(click) {
        const position = this.getPositionFromScreen(click.position)
        if (!position) return
 
        if (!this.startPosition) {
            this.startPosition = position
            this.endPosition = position
            this.isDrawing = true
            this.createEntities()
            this.tooltip.show('单击结束绘制', click.position)
            return
        }
 
        if (this.isDrawing) {
            this.endPosition = position
            this.isDrawing = false
            const positions = this.getRectanglePositions()
            this.notify('getPolygonPositions', positions)
            this.tooltip.hide()
            this.clearPreviewEntities()
        }
    }
 
    handleMouseMove(movement) {
        if (!this.isDrawing) {
            this.tooltip.move(movement.endPosition)
            return
        }
        const position = this.getPositionFromScreen(movement.endPosition)
        if (!position) return
        this.endPosition = position
        this.tooltip.move(movement.endPosition)
    }
 
    createEntities() {
        if (this.rectangleEntity) return
        const hierarchy = new Cesium.CallbackProperty(() => {
            const positions = this.getRectanglePositions()
            return new Cesium.PolygonHierarchy(positions)
        }, false)
 
        this.rectangleEntity = this.dataSource.entities.add({
            polygon: {
                hierarchy,
                material: this.style.fill,
                outline: false,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        })
 
        this.outlineEntity = this.dataSource.entities.add({
            polyline: {
                positions: new Cesium.CallbackProperty(() => {
                    const positions = this.getRectanglePositions()
                    return positions.length ? [...positions, positions[0]] : []
                }, false),
                clampToGround: true,
                width: 2,
                material: this.style.outline,
            },
        })
    }
 
    getRectanglePositions() {
        if (!this.startPosition || !this.endPosition) return []
        const bounds = getBoundsFromCartesians([this.startPosition, this.endPosition])
        return rectanglePositionsFromBounds(bounds)
    }
 
    clearPreviewEntities() {
        if (!this.dataSource) return
        this.dataSource.entities.removeAll()
        this.rectangleEntity = null
        this.outlineEntity = 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.rectangleEntity?.polygon) {
            this.rectangleEntity.polygon.material = this.style.fill
        }
        if (this.outlineEntity?.polyline) {
            this.outlineEntity.polyline.material = 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.startPosition = null
        this.endPosition = null
        this.isDrawing = false
    }
}