Completed
Push — master ( ddf3fd...fcb639 )
by Yannick
39:40
created

js/map.3d.weather.js   A

Complexity

Total Complexity 35
Complexity/F 4.38

Size

Lines of Code 228
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
nc 4
dl 0
loc 228
rs 9
c 2
b 0
f 0
wmc 35
mnd 6
bc 38
fnc 8
bpm 4.75
cpm 4.375
noi 18

6 Functions

Rating   Name   Duplication   Size   Complexity  
A map.3d.weather.js ➔ delete_clouds 0 8 3
B viewer.clock.onTick.addEventListener 0 19 5
A handler_all.setInputAction 0 8 3
B map.3d.weather.js ➔ create_clouds 0 156 1
A map.3d.weather.js ➔ loadFire 0 19 3
A map.3d.weather.js ➔ clickFire 0 3 1
1
/**
2
 * This javascript is part of FlightAirmap.
3
 *
4
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
5
 * Licensed under AGPL license.
6
 * For more information see: https://www.flightairmap.com/
7
*/
8
/** global: Cesium */
9
/** global: viewer */
10
/** global: A */
11
12
function clickDisplayWeather(cb) {
13
    createCookie('show_Weather',cb.checked,2);
14
//    window.location.reload();
15
}
16
17
var cloudscenter;
18
function delete_clouds() {
19
	for (var i =0; i < viewer.dataSources.length; i++) {
20
		if (viewer.dataSources.get(i).name == 'clouds') {
21
			viewer.dataSources.remove(viewer.dataSources.get(i),true);
22
			break;
23
		}
24
	}
25
}
26
27
function create_clouds(cposition) {
28
	//console.log('Create clouds');
29
	cloudscenter = cposition;
30
	$.getJSON('/weather-json.php?latitude='+Cesium.Math.toDegrees(cposition.latitude)+'&longitude='+Cesium.Math.toDegrees(cposition.longitude),function(data) {
31
		//delete_clouds();
32
		var coord = A.EclCoord.fromWgs84(Cesium.Math.toDegrees(cposition.latitude),Cesium.Math.toDegrees(cposition.longitude),0);
33
		var tp = A.Solar.topocentricPosition(new A.JulianDay(new Date(viewer.clock.currentTime.toString())),coord,true);
34
		var tpn = A.Solar.topocentricPosition(new A.JulianDay(new Date(Cesium.JulianDate.addSeconds(viewer.clock.currentTime,60,new Cesium.JulianDate()).toString())),coord,true);
35
		//console.log(tp.hz);
36
		//console.log(tpn.hz);
37
		var ctime = Cesium.JulianDate.toGregorianDate(viewer.clock.currentTime);
38
		var chour = ctime['hour'];
39
		var cminute = ctime['minute'];
40
		var datasource = new Cesium.CustomDataSource('clouds');
41
		var clouds = {ci: ['cirrocumulus1.glb','cirrocumulus2.glb','cirrocumulus3.glb','cirrocumulus4.glb','cirrocumulus5.glb','cirrocumulus6.glb','cirrocumulus7.glb','cirrocumulus8.glb','cirrocumulus9.glb']}; 
42
		    //ac: ['altocumulus1.glb','altocumulus2.glb','altocumulus3.glb','altocumulus4.glb','altocumulus5.glb','altocumulus6.glb'], 
43
		    //ns: ['nimbus1.glb','nimbus_sl1.glb','nimbus_sl2.glb','nimbus_sl3.glb','nimbus_sl4.glb','nimbus_sl5.glb','nimbus_sl6.glb']};
44
		    //st: ['stratus1.glb','stratus2.glb','stratus3.glb','stratus4.glb','stratus5.glb']};
45
		    // st need to follow camera
46
		var cloudsb = {ac: ['altocumulus1.png','altocumulus2.png','altocumulus3.png','altocumulus4.png','altocumulus5.png','altocumulus6.png','altocumulus7.png','altocumulus8.png','altocumulus9.png'], 
47
		    st: ['stratus1.png','stratus2.png','stratus3.png','stratus4.png','stratus5.png','stratus6.png'],
48
		    sc: ['congestus1.png','congestus2.png','congestus3.png'],
49
		    cu: ['cumulus1.png','cumulus2.png','cumulus3.png','cumulus4.png','cumulus5.png','cumulus6.png','cumulus7.png','cumulus8.png','cumulus9.png']};
50
		for (var i = 0; i < data.length; i++) {
51
			var height = data[i]['alt'];
52
			var cov = data[i]['cov'];
53
			var cloud = clouds[data[i]['type']];
54
			//var cloud = clouds['ci'];
55
			//var cloudb = cloudsb['fg'];
56
			var cloudb = cloudsb[data[i]['type']];
57
			var rh = data[i]['rh'];
58
			/*
59
			var timecolors = [[100,100,100],[255,150,100],[255,255,255],[255,255,255],[255,255,255],[255,255,255],[255,150,100],[100,100,100],[100,100,100],[100,100,100],[100,100,100]];
60
			var timecolorsstep = chour/24*10;
61
			if (Math.round(timecolorsstep) > Math.ceil(timecolorsstep)) {
62
				console.log(Math.ceil(timecolorsstep));
63
				var prevcolor = timecolors[Math.ceil(timecolorsstep)];
64
				var nextcolor = timecolors[Math.round(timecolorsstep)];
65
			} else {
66
				if (Math.round(timecolorsstep) == 0) {
67
					var prevcolor = timecolors[0];
68
				} else {
69
					var prevcolor = timecolors[Math.round(timecolorsstep)-1];
70
				}
71
				var nextcolor = timecolors[Math.round(timecolorsstep)];
72
			}
73
			var currentcolor = getColor(prevcolor,nextcolor,3*60,(timecolorsstep%3)*60+cminute);
74
			var color = new Cesium.Color.multiply(new Cesium.Color(rh/100,rh/100,rh/100,1),new Cesium.Color.fromBytes(currentcolor['r'],currentcolor['v'],currentcolor['b'],255), new Cesium.Color());
75
			*/
76
			//var color = new Cesium.Color(rh/100,rh/100,rh/100,1);
77
			
78
			// 17:17 => az : 1.008 - alt : -0.021
79
			
80
			var prevcolor = [255,255,255];
0 ignored issues
show
Unused Code introduced by
The assignment to variable prevcolor seems to be never used. Consider removing it.
Loading history...
81
			if (tp.hz.alt < 0) {
82
				prevcolor = [100,100,100];
83
			} else if (tp.hz.alt < 0.172) {
84
				prevcolor = [255,150,100];
85
			} else if (tp.hz.alt < Math.PI/2) {
86
				prevcolor = [255,255,255];
87
			} else if (tp.hz.alt < 2.9) {
88
				prevcolor = [255,255,255];
89
			} else if (tp.hz.alt < 3.0) {
90
				prevcolor = [255,150,100];
91
			} else {
92
				prevcolor = [100,100,100];
93
			}
94
			var nextcolor =  [255,255,255];
0 ignored issues
show
Unused Code introduced by
The assignment to variable nextcolor seems to be never used. Consider removing it.
Loading history...
95
			if (tpn.hz.alt < 0) {
96
				nextcolor = [100,100,100];
97
			} else if (tpn.hz.alt < 0.172) {
98
				nextcolor = [255,150,100];
99
			} else if (tpn.hz.alt < Math.PI/2) {
100
				nextcolor = [255,255,255];
101
			} else if (tpn.hz.alt < 2.9) {
102
				nextcolor = [255,255,255];
103
			} else if (tpn.hz.alt < 3.0) {
104
				nextcolor = [255,150,100];
105
			} else {
106
				nextcolor = [100,100,100];
107
			}
108
			var timecolorsstep = chour/24*10;
109
			var currentcolor = getColor(prevcolor,nextcolor,3*60,(timecolorsstep%3)*60+cminute);
110
			var color = new Cesium.Color.multiply(new Cesium.Color(rh/100,rh/100,rh/100,1),new Cesium.Color.fromBytes(currentcolor['r'],currentcolor['v'],currentcolor['b'],255), new Cesium.Color());
111
112
			if (typeof cloudb != 'undefined') {
113
				for (j = 0; j < 2000*cov; j++) {
0 ignored issues
show
Bug introduced by
The variable j seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.j.
Loading history...
114
					var cloudcoord = generateRandomPoint(Cesium.Math.toDegrees(cposition.latitude),Cesium.Math.toDegrees(cposition.longitude), height,240,70000);
115
					var position = Cesium.Cartesian3.fromDegrees(cloudcoord['longitude'],cloudcoord['latitude'],cloudcoord['alt']);
116
					var heading = Cesium.Math.toRadians(135);
117
					var pitch = 0;
118
					var roll = 0;
119
					var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
120
					var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
121
					var urlb = '/images/weather/clouds/'+cloudb[Math.floor((Math.random() * cloudb.length))];
122
					var entity = datasource.entities.add({
0 ignored issues
show
Unused Code introduced by
The variable entity seems to be never used. Consider removing it.
Loading history...
123
					    name : url,
0 ignored issues
show
Bug introduced by
The variable url seems to not be initialized for all possible execution paths.
Loading history...
124
					    position : position,
125
					    orientation : orientation,
126
					    billboard: {
127
						image : urlb,
128
						sizeInMeters: true,
129
						scale: Math.random()*10.0,
130
						horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
131
						verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
132
						eyeOffset: new Cesium.Cartesian3(0,6,0),
133
						heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
134
						//fillColor: Cesium.Color.fromCssColorString("#ffc107"),
135
						//translucencyByDistance: new Cesium.NearFarScalar(200,.8,5E4,.2)
136
						distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0,70000.0),
137
						translucencyByDistance: new Cesium.NearFarScalar(1E5/2,.9,1E5,.3),
138
						color: color,
139
						opacity: .9
140
					    }
141
					});
142
				}
143
			}
144
			//console.log(data[i]);
145
			//console.log(cloud);
146
			if (typeof cloud != 'undefined') {
147
				//console.log('models');
148
				for (j = 0; j < 1000*cov; j++) {
149
					var cloudcoord = generateRandomPoint(Cesium.Math.toDegrees(cposition.latitude),Cesium.Math.toDegrees(cposition.longitude), height,240,70000);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable cloudcoord already seems to be declared on line 114. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
150
					//console.log(cloudcoord);
151
					var position = Cesium.Cartesian3.fromDegrees(cloudcoord['longitude'],cloudcoord['latitude'],cloudcoord['alt']);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable position already seems to be declared on line 115. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
152
					if (data[i]['type'] == 'st') {
153
						var heading = camera.heading;
0 ignored issues
show
Bug introduced by
The variable camera seems to be never declared. If this is a global, consider adding a /** global: camera */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Comprehensibility Naming Best Practice introduced by
The variable heading already seems to be declared on line 116. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
154
					} else {
155
						var heading = Cesium.Math.toRadians(135);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable heading already seems to be declared on line 116. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
156
					}
157
					var pitch = 0;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable pitch already seems to be declared on line 117. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
158
					var roll = 0;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable roll already seems to be declared on line 118. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
159
					var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable hpr already seems to be declared on line 119. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
160
					var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable orientation already seems to be declared on line 120. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
161
					var url = '/models/gltf2/weather/'+cloud[Math.floor((Math.random() * cloud.length))];
162
					var entity = datasource.entities.add({
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable entity already seems to be declared on line 122. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
163
					    name : url,
164
					    position : position,
165
					    orientation : orientation,
166
					    model : {
167
						uri : url,
168
						minimumPixelSize : 1,
169
						maximumScale : 20000,
170
						heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
171
						color: color,
172
						colorBlendMode: Cesium.ColorBlendMode.MIX,
173
						distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0,70000.0),
174
						allowPicking: false
175
					    }
176
					});
177
				}
178
			}
179
		}
180
		viewer.dataSources.add(datasource);
181
	});
182
}
183
184
185
var handler_all = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
186
handler_all.setInputAction(function(click) {
187
	var pickedObject = viewer.scene.pick(click.position);
188
	if (Cesium.defined(pickedObject) && getCookie('show_Weather') == 'true') {
189
		delete_clouds();
190
		var cposition = pickedObject.id.position.getValue(viewer.clock.currentTime);
191
		create_clouds(viewer.scene.globe.ellipsoid.cartesianToCartographic(cposition));
192
	}
193
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
194
195
196
viewer.clock.onTick.addEventListener(function(clock) {
197
	if (getCookie('show_Weather') == 'true') {
198
		if (Cesium.defined(viewer.trackedEntity)) {
199
			if (typeof cloudscenter == 'undefined') {
200
				var cposition = viewer.trackedEntity.position.getValue(viewer.clock.currentTime);
201
				create_clouds(viewer.scene.globe.ellipsoid.cartesianToCartographic(cposition));
202
			} else {
203
				var cposition = viewer.trackedEntity.position.getValue(viewer.clock.currentTime);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable cposition already seems to be declared on line 200. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
204
				var ellipsoidGeodesic = new Cesium.EllipsoidGeodesic(Cesium.Cartographic.fromCartesian(cposition),cloudscenter);
205
				var distance = ellipsoidGeodesic.surfaceDistance;
206
				if (distance > 25000) {
207
					create_clouds(viewer.scene.globe.ellipsoid.cartesianToCartographic(cposition));
208
				}
209
			}
210
		} else {
211
			delete_clouds();
212
		}
213
	}
214
});
215
216
if (getCookie('weather_fire') == 'true') loadFire(getCookie('weather_fire'));
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
217
var fireLayer;
218
function clickFire(cb) {
219
    loadFire(cb.checked);
220
}
221
function loadFire(val) {
222
    var fire = getCookie('weather_fire');
223
    if (fire == 'true' && val != 'true') {
224
	viewer.imageryLayers.remove(fireLayer,true);
225
	delCookie('weather_fire');
226
    } else {
227
	createCookie('weather_fire',val,999);
228
	var fireProvider = new Cesium.WebMapServiceImageryProvider({
229
	    url : corsproxy+'https://firms.modaps.eosdis.nasa.gov/wms/viirs',
0 ignored issues
show
Bug introduced by
The variable corsproxy seems to be never declared. If this is a global, consider adding a /** global: corsproxy */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
230
	    layers : 'NASA FIRMS',
231
	    parameters : {
232
	    transparent : true,
233
	    format : 'image/png'
234
	    }
235
	});
236
	fireLayer = new Cesium.ImageryLayer(fireProvider);
237
	viewer.imageryLayers.add(fireLayer);
238
    }
239
}