suerprisePlus
2024-08-14 d4a3ca549f8755c2f87442c27217c3be39cab5cc
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
243
244
245
import mapConfig from './mapConfig';
import { zhangzitou_selectAll } from '@/api/mapView/map.js';
import WKT from 'terraformer-wkt-parser';
import store from '@/store';
// 服务加载
const mapServer = {
    serveType: null,
    layerList: [],
    addLayer(res) {
        const obj = this.getLayerChecked(res);
        if (obj) return;
        this.serveType = res.serveType;
        switch (this.serveType) {
            case 'tdMap':
                this.addTdLayer(res);
                break;
            case 'WMS':
                this.addWMSLayer(res);
                break;
            case 'Tileset':
                this.addTilesetLayer(res);
                break;
            case 'WFS':
                this.addWFSLayer(res);
                break;
        }
    },
    addWFSLayer(res) {
        zhangzitou_selectAll({
            limit: 1000000,
            page: 1,
        }).then((response) => {
            if (response.data.code != 200) return;
            const pois = response.data.result.pois;
            const cnName = res.cnName + '_' + res.id;
            const modelLayer = new Cesium.PrimitiveCollection();
            modelLayer.name = cnName;
            Viewer.scene.primitives.add(modelLayer);
            pois.map((item) => {
                var geom = WKT.parse(item.geom).coordinates;
                const terrain = config.terrain;
                if (terrain.isShow && terrain.isUrl) {
                    this.addTerrainGLB(item, geom, modelLayer);
                } else {
                    this.addGLB(item, geom, modelLayer);
                }
            });
            this.layerList.push({
                id: res.id,
                name: cnName,
                layer: modelLayer,
                serveType: this.serveType,
            });
        });
    },
    addTerrainGLB(item, geom, modelLayer) {
        var positions = [Cesium.Cartographic.fromDegrees(geom[0], geom[1])];
        var promise = Cesium.sampleTerrainMostDetailed(Viewer.terrainProvider, positions);
        promise.then((updatedPositions) => {
            var terrainHeight = updatedPositions[0].height;
            var style = {
                longitude: geom[0],
                latitude: geom[1],
                altitude: terrainHeight,
                heading: 0,
                pitch: 0,
                roll: 0,
            };
            const modelMatrix = mapConfig.getModelMatrix(style);
            const url = '/glb/' + item.type + '.glb';
            modelLayer.add(
                Cesium.Model.fromGltf({
                    id: item.id,
                    url: url,
                    scale: 1,
                    minimumPixelSize: 20,
                    maximumScale: 20,
                    modelMatrix: modelMatrix,
                    primitive: item,
                })
            );
        });
    },
    addGLB(item, geom, modelLayer) {
        var style = {
            longitude: geom[0],
            latitude: geom[1],
            altitude: 0,
            heading: 0,
            pitch: 0,
            roll: 0,
        };
        const modelMatrix = mapConfig.getModelMatrix(style);
        const url = '/glb/' + item.type + '.glb';
        modelLayer.add(
            Cesium.Model.fromGltf({
                id: item.id,
                url: url,
                scale: 1,
                minimumPixelSize: 20,
                maximumScale: 20,
                modelMatrix: modelMatrix,
                primitive: item,
            })
        );
    },
 
    addTdLayer(res) {
        const url = res.url + config.tdToken;
        Viewer.imageryLayers.addImageryProvider(
            new Cesium.UrlTemplateImageryProvider({
                url: url,
            })
        );
    },
    getLayerChecked(res) {
        const obj = this.layerList.filter((item) => {
            if (item.id == res.id) {
                return item;
            }
        });
        const boolen = obj.length > 0 ? true : false;
        return boolen;
    },
    addWMSLayer(res) {
        const serverUrl = config.geoServer;
        const that = this;
        var getFeatureInfoFormat = new Cesium.GetFeatureInfoFormat('html', null, function (html) {
            that.getFeatureInfo(html);
        });
 
        const layer = new Cesium.WebMapServiceImageryProvider({
            url: serverUrl.url + serverUrl.wms,
            layers: res.url,
            getFeatureInfoParameters: { info_format: 'text/html' },
            enablePickFeatures: true,
            getFeatureInfoFormats: [getFeatureInfoFormat],
            parameters: {
                transparent: true,
                format: 'image/png',
                srs: 'EPSG:4490',
                styles: '',
                cql_filter: '',
            },
            tileWidth: 512,
            tileHeight: 512,
        });
 
        const imageLayer = Viewer.imageryLayers.addImageryProvider(layer);
        const cnName = res.cnName + '_' + res.id;
        imageLayer.name = cnName;
        imageLayer.id = res.id;
        this.layerList.push({
            id: res.id,
            name: cnName,
            layer: imageLayer,
            serveType: this.serveType,
        });
    },
    addTilesetLayer(res) {
        let url = res.url.indexOf('{host}') > -1 ? res.url.replace('{host}', iisHost) : res.url;
        var height = 0;
        if (res.bak) {
            height = JSON.parse(res.bak).height;
        }
        var model = earthCtrl.factory.create3DTilesets({
            url: url,
            option: {
                height: height,
                id: res.id,
            },
        });
        const cnName = res.cnName + '_' + res.id;
        model.item.readyPromise.then((item) => {
            if (res.id != 'baseModel') {
                mapConfig.userSceneFlyTo(item);
            }
            this.layerList.push({
                id: res.id,
                name: cnName,
                layer: model,
                serveType: this.serveType,
            });
        });
    },
    removeLayer(res) {
        const cnName = res.cnName + '_' + res.id;
        this.layerList.map((item, index) => {
            if (cnName == item.name && res.id == item.id) {
                if (item.serveType == 'WMS') {
                    Viewer.imageryLayers.remove(item.layer);
                    this.layerList.splice(index, 1);
                } else if (item.serveType == 'Tileset') {
                    item.layer.deleteObject();
                    this.layerList.splice(index, 1);
                } else if (item.serveType == 'WFS') {
                    Viewer.scene.primitives.remove(item.layer);
                    this.layerList.splice(index, 1);
                }
            }
        });
    },
    async getFeatureInfo(html) {
        var start = html.indexOf('<caption class="featureInfo">') + '<caption class="featureInfo">'.length;
        var end = html.indexOf('</caption>');
        var tab = html.substr(start, end - start);
        var std = html
            .substr(html.indexOf('<th>'), html.lastIndexOf('</th>') - html.indexOf('<th>') + 5)
            .replaceAll(' ', '')
            .replaceAll('\n', '')
            .split('</th>');
        var str = html
            .substr(html.indexOf('<td>'), html.lastIndexOf('</td>') - html.indexOf('<td>') + 5)
            .replaceAll(' ', '')
            .replaceAll('\n', '')
            .split('</td>');
        var arr = [];
        for (var i in std) {
            var name, val;
            name = std[i];
            val = str[i];
            if (name == '') {
                continue;
            }
            if (name.indexOf('<th>') > -1) {
                name = name.replaceAll('<th>', '');
            }
            if (val.indexOf('<td>') > -1) {
                val = val.replaceAll('<td>', '');
            }
            if (name != '>') {
                arr.push({
                    name: name,
                    val: val,
                });
            }
        }
        store.dispatch('mapLayers/changeMapInfo', []);
        if (arr.length > 0) {
            // this.$store.geet.mapInfo = arr;
            store.dispatch('mapLayers/changeMapInfo', arr);
        }
    },
};
export default mapServer;