liuyg
2021-07-02 25ce610f6ecca7325e7a743dc032c4a76559c63d
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
define([
    'dojo/_base/declare',
    'dojo/_base/lang',
    'dojo/_base/array',
    'dojo/_base/html',
    'dojo/query',
    'dojo/topic',
    'dojo/on',
    'dojo/aspect',
    'dojo/keys',
    'dojo/i18n',
    'dojo/_base/config',
    'require',
    './utils',
    'jimu/dijit/Message'
], function (declare, lang, array, html, query, topic, on, aspect, keys, i18n, dojoConfig,
    require, jimuUtils,
    Message) {
    var instance = null,
        clazz = declare(null, {
            appConfig: null,
            mapDivId: '',
            map: null,
 
            constructor: function (options, mapDivId) {
                this.appConfig = options.appConfig;
                this.urlParams = options.urlParams;
                this.mapDivId = mapDivId;
                this.id = mapDivId;
                this.nls = window.jimuNls;
                topic.subscribe("appConfigChanged", lang.hitch(this, this.onAppConfigChanged));
                topic.subscribe("mapContentModified", lang.hitch(this, this.onMapContentModified));
 
                on(window, 'resize', lang.hitch(this, this.onWindowResize));
                on(window, 'beforeunload', lang.hitch(this, this.onBeforeUnload));
 
            },
 
            showMap: function () {
                this._showMap(this.appConfig);
            },
 
            _showMap: function (appConfig) {
                var mode = Cesium.SceneMode.SCENE3D;
                if (appConfig.map['3D']) {
                    mode = Cesium.SceneMode.SCENE3D;
                } else if (appConfig.map['2D']) {
                    mode = Cesium.SceneMode.SCENE2D;
                } else {
                    mode = Cesium.SceneMode.COLUMBUS_VIEW;
                }
 
                this._createMap(mode, appConfig);
            },
            _visitConfigMapLayers: function (appConfig, cb) {
                array.forEach(appConfig.map.basemaps, function (layerConfig, i) {
                    cb(layerConfig, i);
                }, this);
 
                array.forEach(appConfig.map.swzwLayers, function (layerConfig, i) {
                    cb(layerConfig, i);
                }, this);
 
                array.forEach(appConfig.map.shLayers, function (layerConfig, i) {
                    cb(layerConfig, i);
                }, this);
 
                array.forEach(appConfig.map.djyLayers, function (layerConfig, i) {
                    cb(layerConfig, i);
                }, this);
 
            },
 
            _createMap: function (mode, appConfig) { //创建地图js
 
                Cesium.Ion.defaultAccessToken = appConfig.cesiumToken;
 
                appConfig.map.mapOptions["imageryProviderViewModels"] = this._getImageryProviderArr(appConfig);
                appConfig.map.mapOptions["terrainProviderViewModels"] = this._getTerrainProviderViewModelsArr(appConfig);
                this.map = new Cesium.Viewer(this.mapDivId, appConfig.map.mapOptions);
 
                this.map.scene.highDynamicRange = false;
                this.map._cesiumWidget._creditContainer.style.display = "none";
                // console.log(this.map.scene)
                this.map.scene.globe.depthTestAgainstTerrain = true;
                this.map.scene.logarithmicDepthBuffer = false;
                this.map.scene.globe.baseColor = Cesium.Color.BLACK;
                window.viewer = this.map;
                var imageryLayers = this.map.imageryLayers;
                imageryLayers.removeAll();
 
                this.map.baseLayerPicker.viewModel.selectedImagery = this.map.baseLayerPicker.viewModel.imageryProviderViewModels[1];
                this.map.baseLayerPicker.viewModel.selectedTerrain = this.map.baseLayerPicker.viewModel.terrainProviderViewModels[1];
                $(".cesium-baseLayerPicker-sectionTitle").eq(0).html("底图");
                $(".cesium-baseLayerPicker-sectionTitle").eq(1).html("地形");
 
                topic.subscribe("gis/map/setCenter", lang.hitch(this, this.centerAt));
                topic.subscribe("gis/map/flyTo", lang.hitch(this, this.flyTo));
                this._processMapOptions(appConfig.map.mapOptions);
                this._publishMapEvent(this.map);
 
                // window.viewer.scene.screenSpaceCameraController.maximumZoomDistance = 180000;
 
                this.setMapHeight(this.map);
            },
            setMapHeight: function (map) {
                // 相机最低高度
                const minimumHeight = 100;
                // 在渲染阶段前添加事件监听器
                map.scene.preRender.addEventListener(function () {
                    var eye = map.camera.positionCartographic;
                    // 判断相机坐标是否小于阈值,若小于阈值,则保持视点方位,修改相机高度
                    if (eye.height < minimumHeight) {
                        map.camera.setView({
                            destination: Cesium.Cartesian3.fromRadians(eye.longitude, eye.latitude, minimumHeight),
                            orientation: {
                                direction: map.camera.direction,
                                up: map.camera.up
                            }
                        });
                    }
                });
            },
            _getImageryProviderArr: function (mapconfig) {
                var imageryProviderArr = [];
                var imageryProviderViewModels = mapconfig.map.imageryProviderViewModels;
                for (var i = 0; i < imageryProviderViewModels.length; i++) {
 
                    let layerArr = [];
                    for (var j = 0; j < imageryProviderViewModels[i].layers.length; j++) {
                        let layerConfig = imageryProviderViewModels[i].layers[j];
                        let layer;
                        if (layerConfig.type == "url") {
                            layer = new Cesium.UrlTemplateImageryProvider(layerConfig);
                        } else if (layerConfig.type == "wmts") {
                            layer = new Cesium.WebMapTileServiceImageryProvider(layerConfig);
                        } else if (layerConfig.type == "SuperMap") {
                            layer = new Cesium.SuperMapImageryProvider(layerConfig);
                        } else if (layerConfig.type == "arcgis") {
                            layer = new Cesium.ArcGisMapServerImageryProvider(layerConfig);
                        }
 
 
 
 
                        layerArr.push(layer);
                    }
 
                    var pvm = new Cesium.ProviderViewModel({
                        name: imageryProviderViewModels[i].name,
                        tooltip: imageryProviderViewModels[i].tooltip,
                        iconUrl: imageryProviderViewModels[i].iconUrl,
                        creationFunction: function () {
                            return layerArr;
                        }
                    });
                    imageryProviderArr.push(pvm);
                }
                return imageryProviderArr;
            },
 
 
            _getTerrainProviderViewModelsArr: function (mapconfig) {
 
                var terrainProviderViewModelsArr = [new Cesium.ProviderViewModel({
                    name: "无地形",
                    tooltip: "WGS84标准球体",
                    iconUrl: "images/basemaps/TerrainEllipsoid.png",
                    creationFunction: function () {
                        return new Cesium.EllipsoidTerrainProvider({
                            ellipsoid: Cesium.Ellipsoid.WGS84
                        })
                    }
                }), new Cesium.ProviderViewModel({
                    name: "鄱阳湖地形",
                    tooltip: "由 普适科技 提供的鄱阳湖地区地形",
                    iconUrl: "images/basemaps/TerrainSTK.png",
                    creationFunction: function () {
                        return new Cesium.CesiumTerrainProvider({
                            url: "./dx/",
                            requestWaterMask: !0,
                            requestVertexNormals: !0,
                            // isSct: true,//因坡度坡向添加
                        })
                    }
                })];
                //              var terrainProviderViewModels = mapconfig.map.terrainProviderViewModels;
                //              for (var i = 0; i < terrainProviderViewModels.length; i++) {
                //                  var tp = null;
                //                  if(terrainProviderViewModels[i].url==""){
                //                        tp = new Cesium.EllipsoidTerrainProvider({
                //                            ellipsoid: Cesium.Ellipsoid.WGS84
                //                        })
                //                    }
                //                    else{
                //                        tp = new Cesium.CesiumTerrainProvider({url:terrainProviderViewModels[i].url,requestVertexNormals:true});
                //                    }
                //                  var pvm = new Cesium.ProviderViewModel({
                //                        name: terrainProviderViewModels[i].name,
                //                        tooltip: terrainProviderViewModels[i].tooltip,
                //                        iconUrl: terrainProviderViewModels[i].iconUrl,
                //                        creationFunction: function() {
                //                            return tp;
                //                        }
                //                    });
                //                    terrainProviderViewModelsArr.push(pvm);
                //              }
 
                return terrainProviderViewModelsArr;
 
            },
 
            flyTo: function (item) {
                window.viewer.camera.flyTo({
                    destination: Cesium.Cartesian3.fromDegrees(Number(item.longitude), Number(item.latitude), Number(item.height)),
                    orientation: {
                        heading: Cesium.Math.toRadians(Number(item.heading)),
                        pitch: Cesium.Math.toRadians(Number(item.pitch)),
                        roll: Cesium.Math.toRadians(Number(item.roll))
                    },
                    duration: 5,
                    pitchAdjustHeight: 10000
                });
            },
            centerAt: function (item) {
                if (item.lgtd) {
                    window.viewer.camera.flyTo({
                        destination: Cesium.Cartesian3.fromDegrees(Number(item.lgtd), Number(item.lttd), 800.0),
                        duration: 5
                    });
                }
 
            },
            createLayer: function (map, maptype, layerConfig) {
                var layMap = {
                    'arcgis': 'ArcGisMapServerImageryProvider',
                    'wmts': 'WebMapTileServiceImageryProvider',
                    'ctms': 'SingleTileImageryProvider',
                    'wms': 'WebMapServiceImageryProvider',
                    'url': 'UrlTemplateImageryProvider',
                    'mapbox': 'MapboxImageryProvider'
                };
 
                var layer;
                if (false) {
 
                } else {
                    var layer = new Cesium[layMap[layerConfig.type]](layerConfig);
                    var imageryLayers = this.map.imageryLayers;
                    var olayer = imageryLayers.addImageryProvider(layer);
                    olayer.show = layerConfig.show;
                    olayer.label = layerConfig.label;
                }
 
            },
            _publishMapEvent: function (map) {
                window._viewerMap = map;
                if (this.loading) {
                    this.loading.destroy();
                }
 
                if (this.map) {
                    this.map = map;
                    topic.publish('mapChanged', this.map);
                } else {
                    this.map = map;
                    topic.publish('mapLoaded', this.map);
                }
            },
            _processMapOptions: function (mapOptions) {
                if (!mapOptions) {
                    return;
                }
                if (mapOptions.positionInfo) {
                    var west = mapOptions.positionInfo.xmin;
 
                    var south = mapOptions.positionInfo.ymin;
 
                    var east = mapOptions.positionInfo.xmax;
 
                    var north = mapOptions.positionInfo.ymax;
                    var heading = mapOptions.positionInfo.heading;
                    var pitch = mapOptions.positionInfo.pitch;
                    var roll = mapOptions.positionInfo.roll;
 
                    setTimeout(lang.hitch(this, function () {
                        this.map.camera.setView({
                            destination: Cesium.Rectangle.fromDegrees(west, south, east, north)
                        });
                    }), 100);
 
 
                    Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(west, south, east, north);
                    Cesium.Camera.DEFAULT_VIEW_FACTOR = 0;
                }
            },
 
            onBeforeUnload: function () {
 
            },
 
            onWindowResize: function () {
                if (this.map && this.map.resize) {
                    this.map.resize();
                }
            },
 
 
            onAppConfigChanged: function (appConfig, reason, changedJson) {
 
            },
 
            onMapContentModified: function () {
                this._recreateMap(this.appConfig);
            },
 
            _recreateMap: function (appConfig) {
                if (this.map) {
                    topic.publish('beforeMapDestory', this.map);
                    this.map.destroy();
                }
                this._showMap(appConfig);
            },
 
        });
 
 
    clazz.getInstance = function (options, mapDivId) {
        if (instance === null) {
            instance = new clazz(options, mapDivId);
        }
        return instance;
    };
 
    return clazz;
});