globe.js 17.1 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,
12
    
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');

19
20
console.log(viewer.ShadowMap);

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
21
22
23
24
25
26
27
28
29
30
31
32
// 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";

33

34
var chosenDate = new Cesium.GregorianDate(2021,12,2,11); //the chosen Date from the Timeline
35
36
37
38
39
40
41
42

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

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
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
// 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
69
70
var gmlID;

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
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

radio.addEventListener('click', function () {
    //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) {
    pickedselect = true;

    // 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;
    }

BujarMuharemi's avatar
BujarMuharemi committed
114
    //SURFACE-VIEW
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    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");

Izairi's avatar
Izairi committed
129
        nameOverlay.textContent = "ID: " + name  + "\t shadowvalue:" + shadowhourvaluepercent;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
130
131
132
133
134
135
136
137
138
139
140
141
        // Highlight the feature if it's not already selected.
        selectedFeatures.forEach(s => {
            if (s.feature === pickedFeature) {
                pickedselect = false;
            }
        });

        hoverpid = pickedFeature.getProperty("gml_parent_id");

        if (pickedselect) {
            tileContent.forEach(t => {
                if (t === pickedFeature) {
BujarMuharemi's avatar
BujarMuharemi committed
142
143
144
                    if(t.getProperty("feature_type")==="RoofSurface"){
                        highlightedFeatures.push(new featurevar(t, t.color));
                        t.color = Cesium.Color.GREEN;                      
BujarMuharemi's avatar
BujarMuharemi committed
145
146

                    }else{
BujarMuharemi's avatar
BujarMuharemi committed
147
148
                        highlightedFeatures.push(new featurevar(t, t.color));
                        t.color = Cesium.Color.YELLOW;
BujarMuharemi's avatar
BujarMuharemi committed
149
150
                    }

BujarMuharemi's avatar
BujarMuharemi committed
151
152
153
                }
            });
        }
BujarMuharemi's avatar
BujarMuharemi committed
154

BujarMuharemi's avatar
BujarMuharemi committed
155
156
    }
    
157
158
    //Building View
    else if(document.getElementById("building").checked){
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
159
160
161
162
163
164
165
166
167
168
169
170
        // 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;
        }


        name = pickedFeature.getProperty("gml_parent_id");
BujarMuharemi's avatar
BujarMuharemi committed
171
        hoverpid = name;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
172
173
174
175
176
177
178
179
180
181
182
183

        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) {
                    highlightedFeatures.push(new featurevar(t, t.color));
                    t.color = Cesium.Color.YELLOW;
                }
            });
        }
    }
184
185
186
187
188
189

    //Roof View
    else if(document.getElementById("roof").checked){
        console.log("clicked when in roof view and on hover");
    }
    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
190
191
192
},
    Cesium.ScreenSpaceEventType.MOUSE_MOVE);

BujarMuharemi's avatar
BujarMuharemi committed
193
194

//----------------------------------------------------------------------------------------------- 
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
195
196
197
198
199
200
201
var pickBuildingFunction
var pickYearOfConstruction
var pickHeight
var pickHeatedVolume
var pickTotalSurfaceArea = 0
var pickRoofType
var pickUValue
202
var shadowHourValue=0;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
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


//Color a feature on selection and show metadata in the InfoBox
viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
    //hide chart for individual buildings
    singleChart.style.visibility = 'hidden';

    pickedhigh = false;

    pickedalreadyselect = false;

    // 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;
    }
    selectpid = pickedFeature.getProperty("gml_parent_id");

    //Show chart for individual buildings. But only if building view is activated
    if (Cesium.defined(pickedFeature) && (!document.getElementById("surface").checked)) {
        singleChart.style.visibility = 'visible';
    }

    /*select the feature if it's not already selected
    selectedFeatures.forEach(s => {
        if (pickedFeature === s.feature) {
            console.log("return");
            pickedalreadyselect = true;
        }
    });

    if(pickedalreadyselect){
        return;
    }*/

    highlightedFeatures.forEach(h => {
        if (h.feature === pickedFeature) {
            pickedhigh = true;
        }
    });

    //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"
Izairi's avatar
Izairi committed
269
    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
270
271
    //fill variables for Single Building Properties
    fillTableProperties(gmlID, surID)
272
   
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
273

274
    //Surface View
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
275
    if (document.getElementById("surface").checked) {
276
        resetRoofColors();
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
277
278
279
280
281
282
283
284
        //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 = [];
285

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
286
287
            //highlight newly selected feature
            tileContent.forEach(t => {
288
289
290
                if (t === pickedFeature) {  
                    console.log(t.getProperty("description")+"\n"+t.getProperty("feature_type")+"\n" + t.getProperty("gml_id") + "\n" + t.getProperty("gml_parent_id"));

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
291
                    t.color = Cesium.Color.LIME;
292

293
294
295
296
297
298
299
300
301
                    //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
                            //console.log("shadowHourValue",shadowHourValue);
                            
                            t.color = Cesium.Color.fromCssColorString(getShadowPalette(shadowHourValue)); //coloring the surface
                            
302
                        }
303
                    }                    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
                }
            });
        } 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;
                }
            });
        }

Izairi's avatar
Izairi committed
320
        var shadowhourvaluepercent =  100-(shadowHourValue*100)
321
        //set feature infobox description (surface view)
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
322
323
324
325
326
327
328
329
        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>" +
330
331
            "<tr><th>description</th><td>" + pickedFeature.getProperty("description") + "</td></tr>" +
            "<tr><th>feature_type</th><td>" + pickedFeature.getProperty("feature_type") + "</td></tr>" +
Izairi's avatar
Izairi committed
332
            "<tr><th>shadow value</th><td>" +  shadowhourvaluepercent.toFixed(1) + "%" + "</td></tr>" +
333
334

            
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
335
            "</tbody></table>";
336
          
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
337
338

    }
339

340
341
    //Building View
    else if(document.getElementById("building").checked){
342
        resetRoofColors();
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
        //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 = [];

            //highlight newly selected feature
            tileContent.forEach(t => {
                if (t.getProperty("gml_parent_id") === selectpid) {
                    t.color = Cesium.Color.LIME;
                }
            });

        } else {
            tileContent.forEach(t => {
                if (t.getProperty("gml_parent_id") === selectpid) {
                    selectedFeatures.push(new featurevar(t, t.color));
                }
            });

            //highlight newly selected feature
            tileContent.forEach(t => {
                if (t.getProperty("gml_parent_id") === selectpid) {
                    t.color = Cesium.Color.LIME;
                }
            });
        }

        buildings(gmlID)

376
        //set feature infobox description (building view)
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
        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>";

    }

394
395
396
397
398
399
400
401
    //Roof View
    //BUG: shows random attributes ui element ??
    else if(document.getElementById("roof").checked){
        console.log("clicked when in roof view and on click");

        //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)            
402
                for (const [key, value] of Object.entries(shadowdata)) { //looping threw the shadowdata array 
403
                    if(key === t.getProperty("gml_id")){    //checking the id
404
                        console.log(value); 
405
                        shadowHourValue = value[chosenDate.hour-1]; //getting the current chosen hour from the timeline                 
406
                        t.color = Cesium.Color.fromCssColorString(getShadowPalette(shadowHourValue));   //coloring the surface                        
407
408
409
410
411
412
413
414
                    }
                }
                
            }
            
        });
    }

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
415
416
417
},
    Cesium.ScreenSpaceEventType.LEFT_CLICK);

BujarMuharemi's avatar
BujarMuharemi committed
418
419

//filling the table in the info sidebar with data    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
function fillTableProperties(gID, sID) {

    if (drawBox === false) {
        swal("CAN'T LOAD DATA!", "mark an area first!", "error");
        document.getElementById("singleChartContainer").style.visibility = "hidden"
        document.getElementsByClassName('cesium-infoBox-defaultTable').style.display = 'none';

    } else {

        //Property BuildingFunction
        buildingFunctionSortPick.forEach((value, key) => {
            if (key === gID) {
                pickBuildingFunction = value
            }
        })

        //Property Year of Construction
        constructionYearSortPick.forEach((value, key) => {
            if (key === gID) {
                pickYearOfConstruction = value
            }
        })

        //Property height
        heightSortPick.forEach((value, key) => {
            if (key === gID) {
                pickHeight = value
            }
        })

        //Property HeatedVolume
        heatedVolumeSortPick.forEach((value, key) => {
            if (key === gID) {
                pickHeatedVolume = value
            }
        })

        //Property TotalSurfaceArea
        totalSurfaceAreaSortPick.forEach((value, key) => {
            if (key === gID) {
                var sum = 0
                value.forEach(element => {
                    sum += element.totalSurfaceArea
                });
                pickTotalSurfaceArea = sum
            }
        })

        //Property RoofType
        roofTypeSortPick.forEach((value, key) => {
            if (key === gID) {
                pickRoofType = value
            }
        })

        //Property UValue
        uValueSortPick.forEach((value, key) => {
            if (key === sID) {
                pickUValue = value
            }
        })
    }

}
484
485
486
487
488
489
490
491
492
493
494
495
496


function resetRoofColors(){
    tileContent.forEach(t => {
        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;                        
                }
            }            
        }        
    });
}