guonan
2025-06-17 be9c1145fc79165142fbe29aacb04dd8e34dd23f
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
let areaPrimitives = []
let areaDataSource = []
 
/**
 * 将避险场所数据转为 GeoJSON FeatureCollection
 */
export function convertToGeoJson(data, offset = 0.0001) {
    const features = data.map((item) => {
        const lon = parseFloat(item.lon);
        const lat = parseFloat(item.lat);
 
        // 构造一个小正方形区域(5米左右)
        const coordinates = [
            [
                [lon, lat],
                [lon + offset, lat],
                [lon + offset, lat - offset],
                [lon, lat - offset],
                [lon, lat],
            ],
        ];
 
        return {
            type: "Feature",
            properties: {
                id: item.safeHavenId,
                name: item.safeHavenName,
                locationInfo: item.locationInfo,
                area: item.area,
                safeHavenType: item.safeHavenType,
                accommodatePersonNum: item.accommodatePersonNum,
                planResettlementPersonNum: item.planResettlementPersonNum,
                materialReserve: item.materialReserve,
                remark: item.remark,
            },
            geometry: {
                type: "MultiPolygon",
                coordinates: [coordinates],
            },
        };
    });
 
    return {
        type: "FeatureCollection",
        name: "emergency_area",
        crs: {
            type: "name",
            properties: {
                name: "urn:ogc:def:crs:OGC:1.3:CRS84",
            },
        },
        features,
    };
}
 
 
export function loadAreaPolygon(geoJsonData, isPolluted = false) {
    let color = new Cesium.Color(1.0, 0.0, 0.0, 0.1);
    if (isPolluted) {
        color = Cesium.Color.GREEN;
    }
 
    // 使用 GeoJsonDataSource 加载 GeoJSON 数据
    const dataSourcePromise = Cesium.GeoJsonDataSource.load(geoJsonData, {
        clampToGround: true,
    });
 
    // 获取 GeoJSON 中的第一个 Polygon feature
    return dataSourcePromise.then(function (dataSource) {
        viewer.dataSources.add(dataSource);
        areaDataSource.push(dataSource);
 
        const polygonEntity = dataSource.entities.values;
        const distanceDisplayCondition = new Cesium.DistanceDisplayCondition(1000, 50000000);
        polygonEntity.forEach(entity => {
            entity.polygon.material = new Cesium.ColorMaterialProperty(color);
 
            const properties = entity.properties;
            const fullname = properties.name.getValue();
 
            const positions = entity.polygon.hierarchy._value.positions;
 
            entity.label = {
                text: fullname || "默认标签",
                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,
                scale: 1.0,
                scaleByDistance: new Cesium.NearFarScalar(1.5e2, 1.0, 1.5e7, 0.5),
                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                verticalOrigin: Cesium.VerticalOrigin.CENTER,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                pixelOffset: new Cesium.Cartesian2(0, 0),
                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),
                clampToGround: true,
            };
 
            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() {
    // 1. 清除通过 viewer.entities 添加的实体
    if (Array.isArray(areaDataSource)) {
        areaDataSource.forEach(dataSource => {
            // 移除数据源中的所有实体
            dataSource.entities.values.forEach(entity => {
                viewer.entities.remove(entity);
            });
            // 移除数据源本身
            viewer.dataSources.remove(dataSource);
        });
        areaDataSource = [];
    }
 
    // 2. 清除通过 viewer.scene.primitives 添加的图元
    if (Array.isArray(areaPrimitives)) {
        areaPrimitives.forEach(primitive => {
            viewer.scene.primitives.remove(primitive);
        });
        areaPrimitives = [];
    }
 
    // 3. 强制场景重绘
    viewer.scene.requestRender();
}
 
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)
}