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) }