guonan
2025-04-14 9e860a560c5a4b81abe2042b8d8698e253730502
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
let areaPrimitives = []
let areaDataSource = []
 
export function loadAreaPolygon(url, isPolluted = false) {
    let color = new Cesium.Color(1.0, 0.0, 0.0, 0.1)
    if (isPolluted) {
        color = Cesium.Color.SKYBLUE
    }
    // 使用 GeoJsonDataSource 加载 GeoJSON 数据
    const dataSourcePromise = Cesium.GeoJsonDataSource.load(url, {
        clampToGround: true,
    })
 
    // 获取 GeoJSON 中的第一个 Polygon feature
    return dataSourcePromise.then(function (dataSource) {
        viewer.dataSources.add(dataSource)
        console.log("dataSource", dataSource)
        // 获取 GeoJSON 中的第一个 Polygon feature
        areaDataSource.push(dataSource)
 
        // const polygonEntity = dataSource.entities.values[0]
        // let areaPrimitive = createAreaPolygon(polygonEntity, color)
        // areaPrimitives.push(areaPrimitive)
 
        // return areaPrimitive
 
        const polygonEntity = dataSource.entities.values
        console.log("polygonEntity", polygonEntity)
        const distanceDisplayCondition = new Cesium.DistanceDisplayCondition(1000, 50000000)
        polygonEntity.forEach(entity => {
            // console.log("entity", entity)
 
            entity.polygon.material = new Cesium.ColorMaterialProperty(
                // Cesium.Color.LIGHTSTEELBLUE.withAlpha(0.2)
                color
                // new Cesium.Color.fromCssColorString("#0f2636b3")
                // Cesium.Color.fromRandom({
                //     alpha: 0.5,
                //     minimumAlpha: 0.2,
                //     maximumAlpha: 0.9,
                // })
            )
 
            // entity.polygon.distanceDisplayCondition = distanceDisplayCondition
 
            const properties = entity.properties
            const fullname = properties.name.getValue()
 
            const positions = entity.polygon.hierarchy._value.positions
 
            // entity.position = positions
            entity.label = {
                // 文本。支持显式换行符“ \ n”
                text: fullname || "默认标签",
                // 字体样式,以CSS语法指定字体
                font: "20pt Source Han Sans CN",
                // 字体颜色
 
                fillColor: Cesium.Color.WHITE,
                // 背景颜色
                backgroundColor: Cesium.Color.BLACK.withAlpha(0.8),
                // 是否显示背景颜色
                showBackground: false,
                // 字体边框
                outline: true,
                // 字体边框颜色
                outlineColor: Cesium.Color.WHITE,
                // 字体边框尺寸
                outlineWidth: 0,
                // 应用于图像的统一比例。比例大于会1.0放大标签,而比例小于会1.0缩小标签。
                scale: 1.0,
                scaleByDistance: new Cesium.NearFarScalar(1.5e2, 1.0, 1.5e7, 0.5),
                // 设置样式:FILL:填写标签的文本,但不要勾勒轮廓;OUTLINE:概述标签的文本,但不要填写;FILL_AND_OUTLINE:填写并概述标签文本。
                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                // 相对于坐标的水平位置
                verticalOrigin: Cesium.VerticalOrigin.CENTER,
                // 相对于坐标的水平位置
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                // 该属性指定标签在屏幕空间中距此标签原点的像素偏移量
                pixelOffset: new Cesium.Cartesian2(0, 0),
                // pixelOffset: new Cesium.Cartesian2(0, 0),
                // 显示在距相机的距离处的属性,多少区间内是可以显示的
                // distanceDisplayCondition: distanceDisplayCondition,
                // : new Cesium.DistanceDisplayCondition(0, 50000),
                heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
                // 是否显示
                show: true,
            }
            entity.polyline = {
                positions: positions,
                width: 2,
                material: new Cesium.ColorMaterialProperty(
                    Cesium.Color.YELLOW
                    // Cesium.Color.fromRandom({
                    //     alpha: 0.5,
                    //     minimumAlpha: 0.2,
                    //     maximumAlpha: 0.9,
                    // })
                ),
                clampToGround: true,
                // distanceDisplayCondition: distanceDisplayCondition,
            }
 
            viewer.entities.add(entity)
        })
 
        return polygonEntity
    })
}
function createAreaPolygon(polygonEntity, color, height) {
    if (!polygonEntity) return
    let areaPrimitive = new Cesium.GroundPrimitive({
        classificationType: Cesium.ClassificationType.BOTH,
        geometryInstances: new Cesium.GeometryInstance({
            geometry: new Cesium.PolygonGeometry({
                polygonHierarchy: polygonEntity.polygon.hierarchy.getValue(),
            }),
        }),
        appearance: new Cesium.EllipsoidSurfaceAppearance({
            material: new Cesium.Material({
                fabric: {
                    type: "Area",
                    uniforms: {
                        baseAreaColor: color || new Cesium.Color(64 / 255.0, 157 / 255.0, 253 / 255.0, 0.5),
                        normalMap: "/areaNormals.jpg",
                        frequency: 1000.0,
                        animationSpeed: 0.05,
                        amplitude: 10,
                        specularIntensity: 10,
                    },
                },
            }),
        }),
    })
    // 流动水面效果
    viewer.scene.primitives.add(areaPrimitive)
 
    return areaPrimitive
}
 
export function clearAreaPolygon() {
    if (Array.isArray(areaPrimitives)) {
        areaPrimitives.forEach(item => {
            viewer.scene.primitives.remove(item)
        })
        areaPrimitives = []
    }
    if (Array.isArray(areaDataSource)) {
        areaDataSource.forEach(dataSource => {
            dataSource.show = false
        })
    }
}
 
export function initAreaLine() {
    // AQUA  SKYBLUE GOLDENROD
 
    const baseColor = Cesium.Color.SKYBLUE
    const activeColor = Cesium.Color.GOLDENROD
    // pointList.forEach(point => {
    //     if (point.type == '河流') {
    //         loadAreaLine(`${MapDataUrl}/河流/${point.name}.geojson`, point.name, baseColor)
    //     }
    // })
    // loadAreaLine(`${MapDataUrl}/河流/donghe.geojson`, Cesium.Color.SKYBLUE)
    // loadAreaLine(`${MapDataUrl}/河流/nanhe.geojson`, Cesium.Color.SKYBLUE)
    // loadAreaLine(`${MapDataUrl}/河流/qingnihe.geojson`, Cesium.Color.SKYBLUE)
 
    return areaLineEntities
}
export function changeAreaLine(name, color) {
    const baseColor = Cesium.Color.SKYBLUE
    const activeColor = Cesium.Color.GOLDENROD
    areaLineEntities.forEach(item => {
        if (item.polyline) {
            if (item.type === name) {
                item.polyline.material = activeColor
            } else {
                item.polyline.material = baseColor
            }
        }
    })
    return
    const dataSources = viewer.dataSources.getByName(name)
    if (dataSources && dataSources.length > 0) {
        const dataSource = dataSources[0]
        console.log("dataSource", dataSource)
        const polygonEntity = dataSource.entities.values
        polygonEntity.forEach(item => {
            if (item.polyline) {
                item.polyline.material = activeColor
            } else {
                item.polyline.material = baseColor
            }
        })
    }
}
 
export const areaLineEntities = []
function loadAreaLine(url, name, color) {
    // 使用 GeoJsonDataSource 加载 GeoJSON 数据
    // const GeoJsonDataSource = new Cesium.GeoJsonDataSource()
    const dataSourcePromise = Cesium.GeoJsonDataSource.load(url, {
        clampToGround: true,
        fill: color || Cesium.Color.SKYBLUE,
        stroke: color || Cesium.Color.SKYBLUE,
        strokeWidth: 5,
    })
    dataSourcePromise.then(function (dataSource) {
        dataSource.name = name
        // viewer.dataSources.add(dataSource)
        // 获取 GeoJSON 中的第一个 Polygon feature
        console.log("dataSource", dataSource)
 
        const polygonEntity = dataSource.entities.values
        areaLineEntities.push(...polygonEntity)
        polygonEntity.forEach(item => {
            item.type = name
            if (item.polyline) {
                item.polyline.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(0, 5000000)
            }
        })
    })
    viewer.dataSources.add(dataSourcePromise)
}