吉安感知网项目-前端
chenyao
72 mins ago 673c8785a130b59254a39770b83f5a724f8712eb
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
import * as Cesium from 'cesium'
import jaGeojsonRaw from '@/assets/geojson/ja.geojson?raw'
 
const DEFAULT_OPTIONS = {
    strokeColor: '#00F9EC',
    strokeWidth: 2,
    fillColor: '#FFFFFF',
    fillAlpha: 0.05,
    labelFont: '14px Source Han Sans CN',
    labelFill: '#FFFFFF',
    labelOutline: '#18305C',
    labelOutlineAlpha: 1,
    zoomTo: true,
}
 
export const loadJaAdminBoundary = async (viewer, options = {}) => {
    if (!viewer) return null
    const config = { ...DEFAULT_OPTIONS, ...options }
    const geojson = JSON.parse(jaGeojsonRaw)
 
    const boundarySource = await Cesium.GeoJsonDataSource.load(geojson, {
        stroke: Cesium.Color.fromCssColorString(config.strokeColor),
        strokeWidth: config.strokeWidth,
        fill: Cesium.Color.fromCssColorString(config.fillColor).withAlpha(config.fillAlpha),
        clampToGround: true,
    })
    boundarySource.name = 'jaBoundarySource'
    viewer.dataSources.add(boundarySource)
 
    const lineSource = new Cesium.CustomDataSource('jaBoundaryLineSource')
    boundarySource.entities.values.forEach(entity => {
        const polygon = entity.polygon
        if (!polygon) return
        const hierarchy = polygon.hierarchy?.getValue?.(viewer.clock.currentTime)
        const positions = hierarchy?.positions
        if (!positions?.length) return
        lineSource.entities.add({
            polyline: {
                positions,
                clampToGround: true,
                width: config.strokeWidth,
                material: Cesium.Color.fromCssColorString(config.strokeColor),
            },
        })
    })
    lineSource.name = 'jaBoundaryLineSource'
    viewer.dataSources.add(lineSource)
 
    const labelSource = new Cesium.CustomDataSource('jaBoundaryLabelSource')
    geojson.features?.forEach(feature => {
        const name = feature?.properties?.name
        const point = feature?.properties?.centroid || feature?.properties?.center
        if (!name || !Array.isArray(point) || point.length < 2) return
        labelSource.entities.add({
            position: Cesium.Cartesian3.fromDegrees(point[0], point[1]),
            label: {
                text: name,
                font: config.labelFont,
                fillColor: Cesium.Color.fromCssColorString(config.labelFill),
                outlineColor: Cesium.Color.fromCssColorString(config.labelOutline).withAlpha(
                    config.labelOutlineAlpha
                ),
                outlineWidth: 2,
                style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                verticalOrigin: Cesium.VerticalOrigin.CENTER,
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
        })
    })
    labelSource.name = 'jaBoundaryLabelSource'
    viewer.dataSources.add(labelSource)
 
    if (config.zoomTo) {
        await viewer.flyTo(boundarySource, {
            duration: 0,
            offset: new Cesium.HeadingPitchRange(0, Cesium.Math.toRadians(-75), 0),
        })
    }
 
    return { boundarySource, lineSource, labelSource }
}
 
export const removeJaAdminBoundary = (viewer, sources) => {
    if (!viewer || !sources) return
    const { boundarySource, lineSource, labelSource } = sources
    if (boundarySource) viewer.dataSources.remove(boundarySource)
    if (lineSource) viewer.dataSources.remove(lineSource)
    if (labelSource) viewer.dataSources.remove(labelSource)
}