surprise
2024-01-08 7e6b37afd1295c71bca1de595426330aff88420d
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
 
 
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import Feature from "ol/Feature";
import Point from "ol/geom/Point";
import LineString from "ol/geom/LineString";
import {fromExtent } from "ol/geom/Polygon";
import { Polygon} from "ol/geom";
import { fromLonLat } from "ol/proj";
import { Circle, Fill, Icon, Stroke, Style, Text } from 'ol/style.js';
 
const mapOL = {
    removeLayerData(res){
        var layers =  window.map.getAllLayers();
        for(var i=0;i<layers.length;i++){
            if(res.id === layers[i].values_.id){
                window.map.removeLayer(layers[i])
            }
        }
    },
    addLayerData(res) {
        var type = res.style.type;
        if (!type) return
        this.removeLayerData(res)
        switch (type) {
            case 'label':
                this.addLabellayer(res);
                break;
            case 'billboard':
                this.addBillboardlayer(res);
                break;
            case 'polyline':
                this.addPolylinelayer(res);
                break;
            case 'rectangle':
                this.addRectanglelayer(res);
                break;
            case 'polygon':
                this.addPolygonlayer(res);
                break;
        }
    },
    CesiumTo84(x, y, z) {
        var ellipsoid = Viewer.scene.globe.ellipsoid;
       
        var cartographic = ellipsoid.cartesianToCartographic({ x: x, y: y, z: z });
        var lat = Cesium.Math.toDegrees(cartographic.latitude);
        var lng = Cesium.Math.toDegrees(cartographic.longitude);
        var alt = cartographic.height;
        return [lng, lat, alt ]
    },
    addLabellayer(res) {
        var style = res.style;
        var val = this.CesiumTo84(style.position.x, style.position.y, style.position.z)
        const labelFeature = new Feature({
            geometry: new Point([val[0], val[1]]),
        });
        const labelStyle = new Style({
            text: new Text({
                text: style.text,
                font: style.font,
                fill: new Fill({
                    color: style.fillColor,
                }),
                stroke: new Stroke({
                    color: style.outlineColor,
                    width: style.outlineWidth,
                }),
            }),
        });
        labelFeature.setStyle([labelStyle])
        const vectorSource = new VectorSource({
            features: [labelFeature],
        });
        var vectorLayer = new VectorLayer({
            id: res.id,
            source: vectorSource,
        });
        window.map.addLayer(vectorLayer)
    },
    addBillboardlayer(res) {
        var style = res.style;
        var val = this.CesiumTo84(style.position.x, style.position.y, style.position.z)
        const imgFeature = new Feature({
            geometry: new Point([val[0], val[1]]),
        });
        const imgStyle = new Style({
            image: new Icon({
                scale: style.scale,
                src: style.image,
            }),
        });
        imgFeature.setStyle([imgStyle])
        const vectorSource = new VectorSource({
            features: [imgFeature],
        });
        var vectorLayer = new VectorLayer({
            id: res.id,
            source: vectorSource,
        });
        window.map.addLayer(vectorLayer)
    },
    addPolylinelayer(res) {
        var style = res.style;
        var positon = style.positions;
        var geom = [];
        for (var i in positon) {
            var lonlat = positon[i];
            var val = this.CesiumTo84(lonlat.x, lonlat.y, lonlat.z);
            geom.push([val[0], val[1]])
        }
        const lineFeature = new Feature({
            geometry: new LineString(geom),
            name: "Line"
        });
        const lineStyle = new Style({
 
            stroke: new Stroke({
                color: style.color,
                width: style.outlineWidth == 0 ? 2 : style.outlineWidth,
            }),
 
        });
        lineFeature.setStyle([lineStyle])
        const vectorSource = new VectorSource({
            features: [lineFeature],
        });
        var vectorLayer = new VectorLayer({
            id: res.id,
            source: vectorSource,
        });
        window.map.addLayer(vectorLayer)
    },
    addRectanglelayer(res) {
        var style = res.style;
       
        var east = Cesium.Math.toDegrees(
            style.coordinates.east
        );
        var north = Cesium.Math.toDegrees(
            style.coordinates.north
        );
        var west = Cesium.Math.toDegrees(
            style.coordinates.west
        );
        var south = Cesium.Math.toDegrees(
            style.coordinates.south
        );
 
        var rectFeature = new Feature({
            geometry: new fromExtent([east, south, west, north])
        });
        const rectStyle = new Style({
            //填充色
            fill: new Fill({
                color:  style.color,
            }),
            //边线颜色
            stroke: new Stroke({
                color:  style.outlineColor,
                width: 1
            }),
        });
        rectFeature.setStyle(rectStyle)
        const vectorSource = new VectorSource({
            features: [rectFeature],
        });
        var vectorLayer = new VectorLayer({
            id: res.id,
            source: vectorSource,
        });
        window.map.addLayer(vectorLayer)
    },
    addPolygonlayer(res) {
        var style = res.style;
        console.log(style)
        var positon= style.positions;
        var geom = [];
        for (var i in positon) {
            var lonlat = positon[i];
            var val = this.CesiumTo84(lonlat.x, lonlat.y, lonlat.z);
            geom.push([val[0], val[1]])
        }
        if(geom[0]!= geom[geom.length-1]){
            geom.push(geom[0])
        }
           var std =[];
           std.push(geom) 
        var rectFeature = new Feature({
            geometry: new Polygon(std)
        });
 
        const rectStyle = new Style({
            //填充色
            fill: new Fill({
                color:  style.color,
            }),
            //边线颜色
            stroke: new Stroke({
                color:  style.outlineColor,
                width: 1
            }),
        });
        rectFeature.setStyle(rectStyle)
        const vectorSource = new VectorSource({
            features: [rectFeature],
        });
        var vectorLayer = new VectorLayer({
            id: res.id,
            source: vectorSource,
        });
        window.map.addLayer(vectorLayer)
 
    },
}
export default mapOL;