赣州市洪水风险预警系统三维版本
guoshilong
2023-02-27 4d8c6dd77427e8e581fda17b6b65ba86bfb7a815
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
define([], function () {
    var thisoption = {};
 
    function Flyobj() {
 
        function ctsn3ToDegree(cartesian3) {
            let cartographic = AppScope.Viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian3);
            return {
                lng: Cesium.Math.toDegrees(cartographic.longitude),
                lat: Cesium.Math.toDegrees(cartographic.latitude),
                alt: cartographic.height
            };
        }
 
        function getHeadingRadian(point1, point2) {
            return Math.atan2(point2.lng - point1.lng, point2.lat - point1.lat);
        }
 
        /**
         * 空间两点距离计算函数 单位 米
         **/
        function getSpaceDistance(positions) {
            var distance = 0;
            var deg;
            for (var i = 0; i < positions.length - 1; i++) {
                deg = positions[i];
                var point1cartographic = Cesium.Cartographic.fromDegrees(deg.longitude, deg.latitude, deg.height);
                deg = positions[i + 1];
                var point2cartographic = Cesium.Cartographic.fromDegrees(deg.longitude, deg.latitude, deg.height);
                //根据经纬度计算出距离
                var geodesic = new Cesium.EllipsoidGeodesic();
                geodesic.setEndPoints(point1cartographic, point2cartographic);
                var s = geodesic.surfaceDistance;
                //console.log(Math.sqrt(Math.pow(distance, 2) + Math.pow(endheight, 2)));
                //返回两点之间的距离
                s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
                distance = distance + s;
            }
            return distance;
        }
 
        var frame_obj={};//上层变量 防止内存溢出
        /**
         * 帧处理者
         **/
        var frame_handler = function (clock) {
            frame_obj.thistime = new Date().getTime();
            frame_obj.timespan = 0;
            frame_obj.nexttime = 0.001;
            frame_obj.timespan = (frame_obj.thistime - AppScope.FlyRoute.timeclock) / 1000;
 
            frame_obj.lasttime = frame_obj.lasttime + frame_obj.timespan;
            if (frame_obj.lasttime > frame_obj.timesum) {
                frame_obj.lasttime = 0;
            }
            frame_obj.nexttime = frame_obj.lasttime + frame_obj.timespan * 2;
            if (frame_obj.nexttime > frame_obj.timesum) {
                frame_obj.nexttime = frame_obj.timesum;
            }
            frame_obj.point = frame_obj.spline.evaluate(frame_obj.lasttime);
            frame_obj.point2 = frame_obj.spline.evaluate(frame_obj.nexttime);
 
            frame_obj.heading = getHeadingRadian(ctsn3ToDegree(frame_obj.point), ctsn3ToDegree(frame_obj.point2));
            frame_obj.pitch = Cesium.Cartesian3.angleBetween(frame_obj.point, frame_obj.point2)-Math.PI/12;
            AppScope.Viewer.camera.lookAt(frame_obj.point, new Cesium.HeadingPitchRange(frame_obj.heading, frame_obj.pitch, 100.0));
            AppScope.FlyRoute.timeclock = frame_obj.thistime;
        }
 
        /**
         * 创建路线
         **/
        function CreateFlyRoute(option) {
            option.timeline = [];
            option.timeline.push(0);
            option.pointline = [];
            option.timesum = 0;
            option.pointline.push(Cesium.Cartesian3.fromDegrees(option.points[0], option.points[1], option.points[2]+option.add_height));
            var len = option.points.length - 3;
            for (var index = 0; index < len; index += 3) {
                let length = getSpaceDistance([{
                    longitude: option.points[index],
                    latitude: option.points[index + 1],
                    height: option.points[index + 2]+option.add_height
                },
                {
                    longitude: option.points[index + 3],
                    latitude: option.points[index + 4],
                    height: option.points[index + 5]+option.add_height
                }]);
                let t = length / option.speed;//计算速度
                option.timesum += t;
                option.timeline.push(option.timesum);
 
                option.pointline.push(Cesium.Cartesian3.fromDegrees(option.points[index + 3], option.points[index + 4], option.points[index + 5]+option.add_height));
            }
            option.lasttime = option.timesum;
            option.spline = new Cesium.CatmullRomSpline({
                times: option.timeline,
                points: option.pointline
            });
            //航向
            option.hpRange = new Cesium.HeadingPitchRange();
            option.fixedFrameTransforms = Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west');
        }
 
 
        var fly_obj = {};
        fly_obj.routes = [];
        fly_obj.stop = function () {
            if(AppScope.FlyRoute.thisRoute!=undefined&&AppScope.FlyRoute.thisRoute.flyline!=undefined){
                AppScope.Viewer.entities.remove(AppScope.FlyRoute.thisRoute.flyline);
                AppScope.FlyRoute.thisRoute.flyline = undefined;
            }
            AppScope.Viewer.clock.onTick.removeEventListener(frame_handler);
            AppScope.Viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
        };
        fly_obj.ispause = false;
        fly_obj.pause = function () {
            if(fly_obj.ispause){
               fly_obj.ispause = false;
               AppScope.Viewer.clock.onTick.addEventListener(frame_handler);
            }else{
                fly_obj.ispause = true;
                AppScope.Viewer.clock.onTick.removeEventListener(frame_handler);
            }
        };
        fly_obj.load = function (arr) {
            $.ajax({
                url: "data/flyroute.json",
                type: "GET",
                dataType: "json",
                success: function (data) {
                    if (data.length == undefined) return;
                    AppScope.FlyRoute.routes=[];
                    data.forEach(item => {
                        CreateFlyRoute(item);
                        AppScope.FlyRoute.routes.push(item);
                    });
                }
            });
        };
        fly_obj.start = function () {
            this.stop();
            fly_obj.ispause = false;
            AppScope.FlyRoute.thisRoute = AppScope.FlyRoute.routes[0];
            var points = [];
            for(var i=0;i<AppScope.FlyRoute.thisRoute.timesum;i+=0.1){//航线线条的平滑度(与飞行无关)
                points.push(AppScope.FlyRoute.thisRoute.spline.evaluate(i));
            }
            AppScope.FlyRoute.thisRoute.flyline = AppScope.Viewer.entities.add({
                name : '航线',
                polyline : {
                    positions : points,
                    width : 0.4,
                    material : Cesium.Color.DARKSLATEBLUE,
                    //clampToGround : true
                }
            });
            AppScope.FlyRoute.thisRoute.lasttime = AppScope.FlyRoute.thisRoute.timesum;
            AppScope.FlyRoute.timeclock = new Date().getTime();
            frame_obj = AppScope.FlyRoute.thisRoute;
            AppScope.Viewer.clock.onTick.addEventListener(frame_handler);
        }
        return fly_obj;
    }
    if (AppScope.FlyRoute == undefined) {
        AppScope.FlyRoute = Flyobj();
    }
 
    thisoption.ShowTool = function () {
 
        layer.closeAll();
        var html1 = "<div style='margin:6px'><input type='button' class='cesium-button' value='开始飞行' onclick='AppScope.FlyRoute.start();'><input type='button' class='cesium-button' value='暂停\\继续' onclick='AppScope.FlyRoute.pause();'><input class='cesium-button layui-layer-close' type='button' value='退出飞行'/></div>";
        $('.cesium-viewer-animationContainer').hide();
        layer.closeAll();
        layer.open({
            type: 1,
            title: "飞行功能",
            area: ['280px', '80px'],
            shade: 0,
            offset: ['100px', 'calc(100% - 380px)'],
            content: html1,
            zIndex: layer.zIndex,
            success: function (layero) {
                layer.setTop(layero);
                AppScope.FlyRoute.load();
            },
            end: function () {
                AppScope.FlyRoute.stop();
            }
        });
    }
 
 
    return thisoption;
});