globe.js 13 KB
Newer Older
JOE Thunyathep S's avatar
update  
JOE Thunyathep S committed
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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJjNjM5YzFjNC03NWNmLTQ2M2YtOWJiNC0xODNmMTY2ZjkwNTkiLCJpZCI6MzY3NjEsImlhdCI6MTYwMzk4NTU3Nn0.G3fnwzZ50towP1Nv9goyvu0JxJW5GtiudTR7X67Zo84';

var viewer = new Cesium.Viewer('cesiumContainer', {
    homeButton: false,
    baseLayerPicker: false,
    navigationHelpButton: false,
    timeline: false,
    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";

// 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
var gmlID

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

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

        nameOverlay.textContent = name;
        // 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) {
                    highlightedFeatures.push(new featurevar(t, t.color));
                    t.color = Cesium.Color.YELLOW;
                }
            });
        }

    }
    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");
        hoverpid = name

        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);

var pickBuildingFunction
var pickYearOfConstruction
var pickHeight
var pickHeatedVolume
var pickTotalSurfaceArea = 0
var pickRoofType
var pickUValue



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

    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 = [];
            //highlight newly selected feature
            tileContent.forEach(t => {
                if (t === pickedFeature) {
                    t.color = Cesium.Color.LIME;
                }
            });
        } 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;
                }
            });
        }



        //set feature infobox description
        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>" +
            "</tbody></table>";

    }
    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)

        //set feature infobox description
        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);

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
            }
        })
    }

}