globe.js 20.4 KB
Newer Older
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
1
2
3
4
5
6
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJjNjM5YzFjNC03NWNmLTQ2M2YtOWJiNC0xODNmMTY2ZjkwNTkiLCJpZCI6MzY3NjEsImlhdCI6MTYwMzk4NTU3Nn0.G3fnwzZ50towP1Nv9goyvu0JxJW5GtiudTR7X67Zo84';

var viewer = new Cesium.Viewer('cesiumContainer', {
    homeButton: false,
    baseLayerPicker: false,
    navigationHelpButton: false,
BujarMuharemi's avatar
BujarMuharemi committed
7
    timeline: true,
8
    //shadows:true,
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
9
10
11
    animation: false,
    sceneModePicker: false,
    geocoder: false,
BujarMuharemi's avatar
BujarMuharemi committed
12
    sceneMode: Cesium.SceneMode.SCENE3D
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
13
14
15
16
17
18
});

viewer.scene.globe.depthTestAgainstTerrain = true;
//--------------------hover over surfaces--------------------
var singleChart = document.getElementById('singleChartContainer');

BujarMuharemi's avatar
BujarMuharemi committed
19
var roofViewActivated = false;
20
21
var chosenAttributeRadioBtnFlag = "shadowValue";

Kolokolnikova's avatar
Kolokolnikova committed
22
23
function chosenAttributeHandler(radioBtnValue) {
    chosenAttributeRadioBtnFlag = radioBtnValue.value;
24
}
Izairi's avatar
Izairi committed
25

26

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
27
28
29
30
31
32
33
34
35
36
37
38
// HTML overlay for showing feature name on mouseover
var nameOverlay = document.createElement("div");
viewer.container.appendChild(nameOverlay);
nameOverlay.className = "backdrop";
nameOverlay.style.display = "none";
nameOverlay.style.position = "absolute";
nameOverlay.style.bottom = "0";
nameOverlay.style.left = "0";
nameOverlay.style["pointer-events"] = "none";
nameOverlay.style.padding = "4px";
nameOverlay.style.backgroundColor = "white";

39

40
var chosenDate = new Cesium.GregorianDate(2021, 12, 2, 11); //the chosen Date from the Timeline
41
42
43

//getting clicked Date from the timeline element
viewer.timeline.container.onmouseup = (e) => {
44
    var julianDate = viewer.clock.currentTime;
45
46
47
48
    Cesium.JulianDate.toGregorianDate(julianDate, chosenDate);
    //console.log(chosenDate);
}

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
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
// An entity object which will hold info about the currently selected feature for infobox display
var selectedEntity = new Cesium.Entity();

// Get default left click handler for when a feature is not picked on left click
var clickHandler = viewer.screenSpaceEventHandler.getInputAction(
    Cesium.ScreenSpaceEventType.LEFT_CLICK
);

function featurevar(feature, originalColor) {
    this.feature = feature;
    this.originalColor = originalColor;
}

//conatins currently highlighted features
var highlightedFeatures = [];

//conatins currently selected features
var selectedFeatures = [];
var hoverpid;
var selectpid;
var pickedselect = true;
var pickedhigh = false;
var pickedalreadyselect = false;
var radio = document.getElementById('radio-group');

//selected gmlID
BujarMuharemi's avatar
BujarMuharemi committed
75
76
var gmlID;

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
77

78
radio.addEventListener('click', function() {
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    //hide chart for individual buildings
    singleChart.style.visibility = 'hidden';

    // If a feature was previously selected, undo the highlight
    tileContent.forEach(t => {
        if (t.getProperty("gml_parent_id") === selectpid) {
            selectedFeatures.forEach(s => {
                if (Cesium.defined(s.feature) && (t.getProperty("gml_id") === s.feature.getProperty("gml_id"))) {
                    t.color = s.originalColor;
                }
            });
        }
    });
    selectedFeatures = [];
    selectpid = 0;
}, false);

// Color a feature yellow on hover.
viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(movement) {
98
        pickedselect = true;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        // If a feature was previously highlighted, undo the highlight
        tileContent.forEach(t => {
            if (t.getProperty("gml_parent_id") === hoverpid) {
                highlightedFeatures.forEach(h => {
                    if (Cesium.defined(h.feature) && (t.getProperty("gml_id") === h.feature.getProperty("gml_id"))) {
                        t.color = h.originalColor;
                    }
                });
            }
        });
        highlightedFeatures = [];
        hoverpid = 0;

        // Pick a new feature
        var pickedFeature = viewer.scene.pick(movement.endPosition);
        if (!Cesium.defined(pickedFeature)) {
            nameOverlay.style.display = "none";
            return;
        }

120
        //Surface-View @onMouseMove
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        if (document.getElementById("surface").checked) {

            // A feature was picked, so show it's overlay content
            nameOverlay.style.display = "block";
            nameOverlay.style.bottom = viewer.canvas.clientHeight - movement.endPosition.y + "px";
            nameOverlay.style.left = movement.endPosition.x + "px";

            if (!Cesium.defined(pickedFeature) || !(pickedFeature instanceof Cesium.Cesium3DTileFeature)) {
                nameOverlay.style.display = 'none';
                return;
            }

            var name = pickedFeature.getProperty("gml_id");
            var shadowhourvaluepercent = 0;
135
136
            nameOverlay.textContent = "ID: " + name;
            //nameOverlay.textContent = "ID: " + name + "\t shadowvalue:" + shadowhourvaluepercent;
137
138
139
140
            // Highlight the feature if it's not already selected.
            selectedFeatures.forEach(s => {
                if (s.feature === pickedFeature) {
                    pickedselect = false;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
141
142
143
                }
            });

144
            hoverpid = pickedFeature.getProperty("gml_parent_id");
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
145

146
147
148
149
150
151
152
153
154
155
156
157
158
159
            if (pickedselect) {
                tileContent.forEach(t => {
                    if (t === pickedFeature) {
                        if (t.getProperty("feature_type") === "RoofSurface") {
                            highlightedFeatures.push(new featurevar(t, t.color));
                            t.color = Cesium.Color.GREEN;

                        } else {
                            highlightedFeatures.push(new featurevar(t, t.color));
                            t.color = Cesium.Color.YELLOW;
                        }
                    }
                });
            }
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
160
161
        }

162
163
164
165
166
167
        //Building View
        else if (document.getElementById("building").checked) {
            // A feature was picked, so show it's overlay content
            nameOverlay.style.display = "block";
            nameOverlay.style.bottom = viewer.canvas.clientHeight - movement.endPosition.y + "px";
            nameOverlay.style.left = movement.endPosition.x + "px";
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
168

169
170
171
            if (!Cesium.defined(pickedFeature) || !(pickedFeature instanceof Cesium.Cesium3DTileFeature)) {
                nameOverlay.style.display = 'none';
                return;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
172
173
174
            }


175
176
            name = pickedFeature.getProperty("gml_parent_id");
            hoverpid = name;
BujarMuharemi's avatar
BujarMuharemi committed
177

178
179
180
181
182
            nameOverlay.textContent = name;
            // Highlight the feature if it's not already selected.
            if (hoverpid !== selectpid) {
                tileContent.forEach(t => {
                    if (t.getProperty("gml_parent_id") === hoverpid) {
BujarMuharemi's avatar
BujarMuharemi committed
183
184
                        highlightedFeatures.push(new featurevar(t, t.color));
                        t.color = Cesium.Color.YELLOW;
BujarMuharemi's avatar
BujarMuharemi committed
185
                    }
186
187
                });
            }
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
188
189
        }

190
        //Roof View
191
192
193
194
        // else if (document.getElementById("roof").checked) {
        //     console.log("clicked when in roof view and on hover");
        // }
        else if (document.getElementById("solarPlanner").checked) {
195
            console.log("clicked when in roof view and on hover");
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
196
        }
197

198
    },
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
199
200
    Cesium.ScreenSpaceEventType.MOUSE_MOVE);

BujarMuharemi's avatar
BujarMuharemi committed
201
202

//----------------------------------------------------------------------------------------------- 
203
204
205
206
207
208
var pickBuildingFunction;
var pickYearOfConstruction;
var pickHeight;
var pickHeatedVolume;
var pickTotalSurfaceArea = 0;
var pickRoofType;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
209
var pickUValue
210

211
var shadowHourValue = 0;
212
var SurfacePvPotential;
213
var SurfacePvPotentialObject = []; //object which stores all the pvPotential data
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
214
215
216
217


//Color a feature on selection and show metadata in the InfoBox
viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
218
219
        //hide chart for individual buildings
        singleChart.style.visibility = 'hidden';
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
220

221
        pickedhigh = false;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
222

223
        pickedalreadyselect = false;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
224

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
        // If a feature was previously selected, undo the highlight
        tileContent.forEach(t => {
            if (t.getProperty("gml_parent_id") === selectpid) {
                selectedFeatures.forEach(s => {
                    if (Cesium.defined(s.feature) && (t.getProperty("gml_id") === s.feature.getProperty("gml_id"))) {
                        t.color = s.originalColor;
                    }
                });
            }
        });
        selectpid = 0;
        selectedFeatures = [];

        //pick a new feature
        var pickedFeature = viewer.scene.pick(movement.position);
        if (!Cesium.defined(pickedFeature)) {
            clickHandler(movement);
            return;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
243
        }
244
     
245
        selectpid = pickedFeature.getProperty("gml_parent_id");
246
        
247
248
249
250
        //Show chart for individual buildings. But only if building view is activated
        if (Cesium.defined(pickedFeature) && (!document.getElementById("surface").checked)) {
            singleChart.style.visibility = 'visible';
        }
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
251

252
253
254
255
256
        highlightedFeatures.forEach(h => {
            if (h.feature === pickedFeature) {
                pickedhigh = true;
            }
        });
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
257

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        //fill individual charts
        gmlID = pickedFeature.getProperty("gml_parent_id");
        surID = pickedFeature.getProperty("gml_id")

        pickBuildingFunction = "Not Defined"
        pickYearOfConstruction = "Not Defined"
        pickHeight = "Not Defined"
        pickHeatedVolume = "Not Defined"
        pickTotalSurfaceArea = 0
        pickRoofType = "Not Defined"
        pickUValue = "Not Defined"

        //fill variables for Single Building Properties
        fillTableProperties(gmlID, surID)

273
274
        //########################################################################################################################
        //Different Views from here 
Kolokolnikova's avatar
Kolokolnikova committed
275

276
        //Surface View @onLeftClick
277
278
        if (document.getElementById("surface").checked) {
            resetRoofColors();
279
            setMinAndMax(chosenAttributeRadioBtnFlag);
280

281
282
283
284
285
286
287
288
            //save the selected feature's original color
            if (pickedhigh) {
                highlightedFeatures.forEach(h => {
                    if (h.feature === pickedFeature) {
                        selectedFeatures.push(new featurevar(h.feature, h.originalColor));
                    }
                });
                highlightedFeatures = [];
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
289

290
291
292
                //highlight newly selected feature
                tileContent.forEach(t => {
                    if (t === pickedFeature) {
293
294
                        //used for debugging, when clicking on a surface
                        //console.log(t.getProperty("description") + "\n" + t.getProperty("feature_type") + "\n" + t.getProperty("gml_id") + "\n" + t.getProperty("gml_parent_id"));
295

296
297
298
299
300
                        //color surfaces depending on their shadow value
                        for (const [key, value] of Object.entries(shadowdata)) { //looping threw the shadowdata array
                            if (key === t.getProperty("gml_id")) { //checking if the ids of the current surface match the id in the shadowdata array
                                //console.log("hour:",chosenDate.hour);
                                shadowHourValue = value[chosenDate.hour - 1] //getting the current chosen hour from the timeline
Kolokolnikova's avatar
Kolokolnikova committed
301
302
303

                                if (chosenAttributeRadioBtnFlag === "shadowValue") {
                                    t.color = Cesium.Color.fromCssColorString(getColorFromPalette(shadowHourValue, "shadowValue")); //coloring the surface
304
305
                                }
                                //t.color = Cesium.Color.fromCssColorString(getShadowPalette(shadowHourValue)); //coloring the surface
306
307
                            }
                        }
308
                        //console.log(pvPotentialSurfaces);
309
                        //coloring surfaces on pv potential
310
                        
311
                        //console.log("minie maus", minAndMaxPvPotentials);
Kolokolnikova's avatar
Kolokolnikova committed
312
                        for (const [key, value] of Object.entries(pvPotentialSurfaces)) {
313
                            if (value["id"] === t.getProperty("gml_id") && chosenAttributeRadioBtnFlag !== "shadowValue") { //checking if the ids of the current surface match the id in the shadowdata array
Kolokolnikova's avatar
Kolokolnikova committed
314
                                //console.log("hour:",chosenDate.hour);    
315
                                var normalizedPvValue="";
Kolokolnikova's avatar
Kolokolnikova committed
316
                                chosenPvPotentialValue = value["attributes"]["pvPotential"][chosenAttributeRadioBtnFlag];
317
                                //console.log("chosenPvPotentialValue",chosenPvPotentialValue);
318
                                
Kolokolnikova's avatar
Kolokolnikova committed
319
                                normalizedPvValue = getNormalizedValue(chosenPvPotentialValue);
320
                                console.log("normalisierter", normalizedPvValue);                                
Kolokolnikova's avatar
Kolokolnikova committed
321
                                t.color = Cesium.Color.fromCssColorString(getColorFromPalette(normalizedPvValue, "pvPotential")); //coloring the surface
322
323
                                SurfacePvPotential = chosenPvPotentialValue;
            
324
                                SurfacePvPotentialObject = value["attributes"]["pvPotential"];
Kolokolnikova's avatar
Kolokolnikova committed
325
                                console.log(SurfacePvPotentialObject);
326
327
                            }
                        }
328
329
                    }
                });
330

331
332
333
334
335
336
337
338
339
340
341
342
343
            } else {
                tileContent.forEach(t => {
                    if (t === pickedFeature) {
                        selectedFeatures.push(new featurevar(t, t.color));
                    }
                });
                //highlight newly selected feature
                tileContent.forEach(t => {
                    if (t === pickedFeature) {
                        t.color = Cesium.Color.LIME;
                    }
                });
            }
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
344

345
            //show the attributes of the chosen surface in the table 
346
347
348
349
350
351
352
353
354
355
            var chosenAttribute2Show = "";
            var shadowhourvaluepercent = shadowHourValue * 100;
            console.log("shadowHourValue:",shadowHourValue*100,"\n shadowhourvaluepercent:",shadowhourvaluepercent);

            if(chosenAttributeRadioBtnFlag==="shadowValue"){
                chosenAttribute2Show ="<tr><th>shadow value</th><td>" + shadowhourvaluepercent.toFixed(1) + "%" + "</td></tr>";
            }else{
                chosenAttribute2Show = "<tr><th>"+chosenAttributeRadioBtnFlag+"</th><td>" + SurfacePvPotential + "</td></tr>";
            }
            
356
357
358
359
360
361
362
363
364
365
366
            //set feature infobox description (surface view)
            var featureName = pickedFeature.getProperty("name");
            selectedEntity.name = featureName;
            selectedEntity.description = 'Loading <div class="cesium-infoBox-loading"></div>';
            viewer.selectedEntity = selectedEntity;
            selectedEntity.description =
                '<table class="cesium-infoBox-defaultTable"><tbody>' +
                "<tr><th>Surface_ID</th><td>" + pickedFeature.getProperty("gml_id") + "</td></tr>" +
                "<tr><th>u_Value</th><td>" + pickUValue + "</td></tr>" +
                "<tr><th>description</th><td>" + pickedFeature.getProperty("description") + "</td></tr>" +
                "<tr><th>feature_type</th><td>" + pickedFeature.getProperty("feature_type") + "</td></tr>" +
367
                chosenAttribute2Show;
368
                "</tbody></table>";
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
369
370
371
372


        }

373
374
375
376
377
378
379
380
381
382
383
        //Building View
        else if (document.getElementById("building").checked) {
            resetRoofColors();
            //save the selected feature's original color
            if (pickedhigh) {
                highlightedFeatures.forEach(h => {
                    if (h.feature.getProperty("gml_parent_id") === selectpid) {
                        selectedFeatures.push(new featurevar(h.feature, h.originalColor));
                    }
                });
                highlightedFeatures = [];
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
384

385
386
387
388
389
390
                //highlight newly selected feature
                tileContent.forEach(t => {
                    if (t.getProperty("gml_parent_id") === selectpid) {
                        t.color = Cesium.Color.LIME;
                    }
                });
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
391

392
393
394
395
396
397
            } else {
                tileContent.forEach(t => {
                    if (t.getProperty("gml_parent_id") === selectpid) {
                        selectedFeatures.push(new featurevar(t, t.color));
                    }
                });
398

399
400
401
402
                //highlight newly selected feature
                tileContent.forEach(t => {
                    if (t.getProperty("gml_parent_id") === selectpid) {
                        t.color = Cesium.Color.LIME;
403
                    }
404
                });
405
406
            }

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
            buildings(gmlID)

            //set feature infobox description (building view)
            var featureName = pickedFeature.getProperty("name");
            selectedEntity.name = featureName;
            selectedEntity.description = 'Loading <div class="cesium-infoBox-loading"></div>';
            viewer.selectedEntity = selectedEntity;
            selectedEntity.description =
                '<table class="cesium-infoBox-defaultTable"><tbody>' +
                "<tr><th>Building_ID</th><td>" + pickedFeature.getProperty("gml_parent_id") + "</td></tr>" +
                "<tr><th>buildingFunction</th><td>" + pickBuildingFunction + "</td></tr>" +
                "<tr><th>yearOfConstruction</th><td>" + pickYearOfConstruction + "</td></tr>" +
                "<tr><th>height</th><td>" + pickHeight + "</td></tr>" +
                "<tr><th>heatedVolume</th><td>" + pickHeatedVolume + "</td></tr>" +
                "<tr><th>Total_Surface_Area</th><td>" + pickTotalSurfaceArea + "</td></tr>" +
                "<tr><th>rooftype</th><td>" + pickRoofType + "</td></tr>" +
                "</tbody></table>";

        }

427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
        //solarPlanner View       
        else if (document.getElementById("solarPlanner").checked) {
            resetRoofColors();
            
            //looping threw all surfaces
            tileContent.forEach(t => {
                for (const [key, value] of Object.entries(pvPotentialSurfaces)) {
                    if (value["id"] === t.getProperty("gml_id") && chosenAttributeRadioBtnFlag !== "shadowValue") { //checking if the ids of the current surface match the id in the shadowdata array
                        //console.log("hour:",chosenDate.hour);    
                        var normalizedPvValue="";
                        chosenPvPotentialValue = value["attributes"]["pvPotential"][chosenAttributeRadioBtnFlag];
                        //console.log("chosenPvPotentialValue",chosenPvPotentialValue);
                        
                        normalizedPvValue = getNormalizedValue(chosenPvPotentialValue);
                        console.log("normalisierter", normalizedPvValue);                                
                        t.color = Cesium.Color.fromCssColorString(getColorFromPalette(normalizedPvValue, "pvPotential")); //coloring the surface
                        SurfacePvPotential = chosenPvPotentialValue;
444

445
446
447
448
449
450
451
452
453
454
455
456
                        SurfacePvPotentialObject = value["attributes"]["pvPotential"];
                        console.log(SurfacePvPotentialObject);
                    }
                }
            });         

            
        }

        /**
         * @deprecated roofview is not used anymore, maybe later for "color all surface, depending on one attribute"
         */
BujarMuharemi's avatar
BujarMuharemi committed
457
        else if (document.getElementById("roofView").checked && roofViewActivated){
458
459
460
            console.log(pickedhigh,highlightedFeatures,tileContent);
            resetRoofColors();
            
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
            //looping threw all surfaces
            tileContent.forEach(t => {
                if (t.getProperty("description").includes("Roof")) { //HACK: checking if the are a roof surface (should come from feature_type)            
                    for (const [key, value] of Object.entries(shadowdata)) { //looping threw the shadowdata array 
                        if (key === t.getProperty("gml_id")) { //checking the id
                            //console.log(value); 
                            shadowHourValue = value[chosenDate.hour - 1]; //getting the current chosen hour from the timeline                 
                            t.color = Cesium.Color.fromCssColorString(getShadowPalette(shadowHourValue)); //coloring the surface                        
                        }
                    }
                }
            });
        }

    },
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
476
477
    Cesium.ScreenSpaceEventType.LEFT_CLICK);

BujarMuharemi's avatar
BujarMuharemi committed
478

479
function resetRoofColors() {
480
    tileContent.forEach(t => {
481
482
483
484
        if (t.getProperty("description").includes("Roof")) {
            for (const [key, value] of Object.entries(shadowdata)) {
                if (key === t.getProperty("gml_id")) {
                    t.color = Cesium.Color.WHITE;
485
                }
486
487
            }
        }
488
489
    });
}