globe.js 15.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,
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    animation: false,
    sceneModePicker: false,
    geocoder: false,
});

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

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

29
30
31
32
33
34
35
36
37
38

var chosenDate = new Cesium.GregorianDate; //the chosen Date from the Timeline

//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
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
// 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
65
66
var gmlID;

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

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
110
    //SURFACE-VIEW
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    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");

125
        nameOverlay.textContent = "ID: " + name  + "\t shadowvalue:" + shadowHourValue;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
126
127
128
129
130
131
132
133
134
135
136
137
        // 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
138
139
140
                    if(t.getProperty("feature_type")==="RoofSurface"){
                        highlightedFeatures.push(new featurevar(t, t.color));
                        t.color = Cesium.Color.GREEN;                      
BujarMuharemi's avatar
BujarMuharemi committed
141
142

                    }else{
BujarMuharemi's avatar
BujarMuharemi committed
143
144
                        highlightedFeatures.push(new featurevar(t, t.color));
                        t.color = Cesium.Color.YELLOW;
BujarMuharemi's avatar
BujarMuharemi committed
145
146
                    }

BujarMuharemi's avatar
BujarMuharemi committed
147
148
149
                }
            });
        }
BujarMuharemi's avatar
BujarMuharemi committed
150

BujarMuharemi's avatar
BujarMuharemi committed
151
152
    }
    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    else {

        // 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
167
        hoverpid = name;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
168
169
170
171
172
173
174
175
176
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) {
                    highlightedFeatures.push(new featurevar(t, t.color));
                    t.color = Cesium.Color.YELLOW;
                }
            });
        }
    }
},
    Cesium.ScreenSpaceEventType.MOUSE_MOVE);

BujarMuharemi's avatar
BujarMuharemi committed
183
184

//----------------------------------------------------------------------------------------------- 
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
185
186
187
188
189
190
191
var pickBuildingFunction
var pickYearOfConstruction
var pickHeight
var pickHeatedVolume
var pickTotalSurfaceArea = 0
var pickRoofType
var pickUValue
192
var shadowHourValue=0;
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
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


//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"

    //fill variables for Single Building Properties
    fillTableProperties(gmlID, surID)
262
   
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
263
264
265
266
267
268
269
270
271
272
273

    if (document.getElementById("surface").checked) {

        //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 = [];
274

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
275
276
            //highlight newly selected feature
            tileContent.forEach(t => {
277
278
279
                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
280
                    t.color = Cesium.Color.LIME;
281
282
283
284
285
286
287
288
289
290
291
292
293
294

                    for (const [key, value] of Object.entries(shadowdata)) {
                        if(key === t.getProperty("gml_id")){
                            console.log("hour:",chosenDate.hour);
                            shadowHourValue = value[chosenDate.hour-1]
                            console.log(value[chosenDate.hour-1]);

                            if(value[chosenDate.hour-1]===0){
                                t.color = Cesium.Color.BLACK;                      
                            }
                        }
                    }

                    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
                }
            });
        } 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;
                }
            });
        }


312
        //set feature infobox description (surface view)
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
313
314
315
316
317
318
319
320
        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>" +
321
322
            "<tr><th>description</th><td>" + pickedFeature.getProperty("description") + "</td></tr>" +
            "<tr><th>feature_type</th><td>" + pickedFeature.getProperty("feature_type") + "</td></tr>" +
323
324
325
            "<tr><th>shadow value</th><td>" + shadowHourValue + "</td></tr>" +

            
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
326
            "</tbody></table>";
327
          
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
328
329

    }
330

JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    else {
        //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)

365
        //set feature infobox description (building view)
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        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>";

    }

},
    Cesium.ScreenSpaceEventType.LEFT_CLICK);

BujarMuharemi's avatar
BujarMuharemi committed
386
387

//filling the table in the info sidebar with data    
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
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
            }
        })
    }

}
BujarMuharemi's avatar
BujarMuharemi committed
452
453
454
455
456
457
458
459
460
461

function randomRoofColor(){
    var r = Math.floor(Math.random() * 10);
    var shade =29*r;
    
    var color = new Cesium.Color;
    Cesium.Color.fromBytes(shade,shade,shade,255,color);
    //console.log(color);
    return color;
}