appCesium.js 11.1 KB
Newer Older
Pithon Kabiro's avatar
Pithon Kabiro committed
1
2
"use strict";

3
4
5
6
7
8
// Functions
import {
    aggregateResponse,
} from "./aggregation.js";


9
10
// Functions
import {
11
12
13
14
15
16
17
18
    getDatastreamIdFromBuildingNumber,
    getObservationsUrl,
    createTemporalFilterString,
    formatSTAResponseForHeatMap,
    drawHeatMapHC,
    formatSTAResponseForLineChart,
    drawLineChartHC,
    followNextLink,
19
20
21
22
} from "./appChart.js";

// Constants
import {
23
24
25
26
    BASE_URL,
    PARAM_RESULT_FORMAT,
    PARAM_ORDER_BY,
    PARAM_SELECT,
27
28
} from "./appChart.js";

Pithon Kabiro's avatar
Pithon Kabiro committed
29
Cesium.Ion.defaultAccessToken =
30
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIyODgxYzJlNi1kNDZiLTQ3ZmQtYmUxYy0yMWI0OGM3NDA5MzAiLCJpZCI6NDczOSwic2NvcGVzIjpbImFzciIsImdjIl0sImlhdCI6MTU0MTUyMzU0MX0.shj2hM3pvsvcmE_wMb2aBDuk_cKWmFmbolltInGImwU";
Pithon Kabiro's avatar
Pithon Kabiro committed
31
32
33
34
35
36

// Flag to determine models that will be loaded
// Set to `true` or `false`
const LOAD_DETAILED_BLDG225 = false;

// Global variable
37
const viewer = new Cesium.Viewer("cesiumGlobeContainer", {
38
39
40
41
    scene3DOnly: true,
    imageryProvider: Cesium.createOpenStreetMapImageryProvider({
        url: "https://a.tile.openstreetmap.org/",
    }),
Pithon Kabiro's avatar
Pithon Kabiro committed
42
43
});

44
45
46
47
48
/**
 * Load and zoom to the extents of 3DTiles
 * @param {String} urlTiles URL to the 3DTiles to be loaded
 * @returns {undefined} undefined
 */
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const loadTiles = function(urlTiles) {
    const tileset = new Cesium.Cesium3DTileset({
        url: urlTiles,
    });
    viewer.scene.primitives.add(tileset);

    tileset.readyPromise.then(function() {
        viewer
            .zoomTo(
                tileset,
                new Cesium.HeadingPitchRange(
                    0.0, -0.5,
                    tileset.boundingSphere.radius / 0.5
                )
            )
            .otherwise(function(err) {
                throw err;
            });
    });
Pithon Kabiro's avatar
Pithon Kabiro committed
68
69
};

70
71
72
73
74
/**
 * Load 3DTiles for all the buildings; ignore any glTF models
 * @param{*}
 * @returns {undefined} undefined
 */
75
76
77
const loadNonDetailed = function() {
    // Paths to data sources
    const URL_3DTILES = "data_3d/3dtiles/1_full/tileset.json";
Pithon Kabiro's avatar
Pithon Kabiro committed
78

79
80
    // Tileset with all buildings
    loadTiles(URL_3DTILES);
Pithon Kabiro's avatar
Pithon Kabiro committed
81
82
};

83
84
85
86
87
88
/**
 * Load glTF models
 * @param {String} gltfUrl Path to the folder containing the glTF models
 * @param {String} gltfId Name of the glTF model file without the extension i.e. exclude the `.gltf` suffix
 * @returns {undefined} undefined
 */
89
90
91
92
93
94
95
96
97
98
99
100
101
const gltfLoad = function(gltfUrl, gltfId) {
    const modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
        Cesium.Cartesian3.fromDegrees(9.083385, 48.881342, 0)
    );

    viewer.scene.primitives.add(
        Cesium.Model.fromGltf({
            url: `${gltfUrl}/${gltfId}.gltf`,
            modelMatrix: modelMatrix,
            scale: 0.0254,
            allowPicking: true,
        })
    );
102
103
};

104
105
106
107
108
/**
 * Load detailed glTF models for Building 225 and 3DTiles for the rest of the buildings
 * @param{*}
 * @returns {undefined} undefined
 */
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
const loadDetailed = function() {
    // Paths to data sources
    const URL_3DTILES = "data_3d/3dtiles/2_partial/tileset.json";
    const URL_GLTF = "data_3d/gltf";

    // Tileset without building 225
    loadTiles(URL_3DTILES);

    // Load Building 225
    gltfLoad(URL_GLTF, "bosch_si225_3");

    // Load sensors in Building 225
    const gltfArray = [
        "sensor_013",
        "sensor_023",
        "sensor_033",
        "sensor_053",
        "sensor_063",
        "sensor_073",
        "sensor_083",
        "sensor_093",
        "sensor_103",
        "sensor_113",
        "sensor_123",
        "sensor_133",
        "sensor_143",
        "sensor_153",
        "sensor_163",
        "sensor_173",
        "sensor_183",
        "sensor_213",
        "sensor_223",
        "sensor_233",
        "sensor_253",
        "sensor_263",
        "sensor_273",
        "sensor_283",
        "sensor_293",
        "sensor_303",
        "sensor_313",
        "sensor_323",
        "sensor_333",
        "sensor_343",
        "sensor_353",
        "sensor_363",
        "sensor_373",
        "sensor_383_v2",
    ];

    gltfArray.forEach((sensor) => gltfLoad(URL_GLTF, sensor));
Pithon Kabiro's avatar
Pithon Kabiro committed
159
160
161
};

if (!LOAD_DETAILED_BLDG225) {
162
163
    // Default case: load only 3dTiles
    loadNonDetailed();
Pithon Kabiro's avatar
Pithon Kabiro committed
164
} else {
165
166
    // Alternative case: load 3dTiles + glTF
    loadDetailed();
Pithon Kabiro's avatar
Pithon Kabiro committed
167
}
168
169
170
171

/**
 * Activate feature picking for the displayed 3DTiles
 * @param {*}
172
 * @returns {undefined}
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
const activate3DTileFeaturePicking = function() {
    // HTML overlay for showing feature name on mouseover
    const 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 = "black";
    nameOverlay.style.color = "white";
    nameOverlay.style.fontFamily = "Fira Sans, sans-serif";
    nameOverlay.style.fontSize = "0.75em";

    // Information about the currently selected feature
    const selected = {
        feature: undefined,
        originalColor: new Cesium.Color(),
    };

    // An entity object which will hold info about the currently selected feature for infobox display
    const selectedEntity = new Cesium.Entity();

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

    // Change the feature color on mouse over

    // Information about the currently highlighted feature
    const highlighted = {
        feature: undefined,
        originalColor: new Cesium.Color(),
    };

    // Color a feature on hover.
    viewer.screenSpaceEventHandler.setInputAction(function onMouseMove(movement) {
        // If a feature was previously highlighted, undo the highlight
        if (Cesium.defined(highlighted.feature)) {
            highlighted.feature.color = highlighted.originalColor;
            highlighted.feature = undefined;
        }
        // Pick a new feature
        const pickedFeature = viewer.scene.pick(movement.endPosition);
        if (!Cesium.defined(pickedFeature)) {
            nameOverlay.style.display = "none";
            return;
        }
        // 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";
        let name = pickedFeature.getProperty("_gebaeude");
        if (!Cesium.defined(name)) {
            name = pickedFeature.getProperty("id");
        }
        nameOverlay.textContent = name;
        // Highlight the feature if it's not already selected.
        if (pickedFeature !== selected.feature) {
            highlighted.feature = pickedFeature;
            Cesium.Color.clone(pickedFeature.color, highlighted.originalColor);
            pickedFeature.color = Cesium.Color.GREY;
        }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    // Color a feature on selection and show metadata in the InfoBox.
    viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(movement) {
        // If a feature was previously selected, undo the highlight
        if (Cesium.defined(selected.feature)) {
            selected.feature.color = selected.originalColor;
            selected.feature = undefined;
        }
        // Pick a new feature
        const pickedFeature = viewer.scene.pick(movement.position);
        if (!Cesium.defined(pickedFeature)) {
            clickHandler(movement);
            return;
        }
        // Select the feature if it's not already selected
        if (selected.feature === pickedFeature) {
            return;
        }
        selected.feature = pickedFeature;
        // Save the selected feature's original color
        if (pickedFeature === highlighted.feature) {
            Cesium.Color.clone(highlighted.originalColor, selected.originalColor);
            highlighted.feature = undefined;
        } else {
            Cesium.Color.clone(pickedFeature.color, selected.originalColor);
        }
        // Highlight newly selected feature
        pickedFeature.color = Cesium.Color.LIME;
        // Set feature infobox description
        const featureName = pickedFeature.getProperty("name");
        selectedEntity.name = featureName;
        selectedEntity.description =
            'Loading <div class="cesium-infoBox-loading"></div>';
        viewer.selectedEntity = selectedEntity;
        selectedEntity.description = `
277
278
279
    <table class="cesium-infoBox-defaultTable">
      <tbody> 
        <tr><th>Bau</th><td>
280
        ${pickedFeature.getProperty("_gebaeude")} 
281
282
        </td></tr> 
        <tr><th>Nutzung</th><td>
283
        ${pickedFeature.getProperty("_nutzung")}
284
285
        </td></tr>
        <tr><th>Baujahr</th><td>
286
        ${pickedFeature.getProperty("_baujahr")}
287
288
        </td></tr>
        <tr><th>Geschosse</th><td>
289
        ${pickedFeature.getProperty("_geschosse")} 
290
        </td></tr>
291
292
        <tr><th>Gebäudefläche (m<sup>2</sup>)</th><td>
        ${pickedFeature.getProperty("_gebaeudeflaeche")} 
293
294
295
296
        </td></tr>
      </tbody>
    </table>
    `;
297

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
        const clickedBuilding = pickedFeature.getProperty("_gebaeude");
        const clickedBuildingDatastreamId = getDatastreamIdFromBuildingNumber(
            clickedBuilding,
            "vl",
            "60min"
        );

        const BASE_URL_OBSERVATIONS = getObservationsUrl(
            BASE_URL,
            clickedBuildingDatastreamId
        );
        const PARAM_FILTER = createTemporalFilterString("2020-01-01", "2021-01-01");

        const axiosGetRequest = axios.get(BASE_URL_OBSERVATIONS, {
            params: {
                "$resultFormat": PARAM_RESULT_FORMAT,
                "$orderBy": PARAM_ORDER_BY,
                "$filter": PARAM_FILTER,
                "$select": PARAM_SELECT,
            },
        });
319
320


321
322
323
324
        // Get "ALL" the Observations that satisfy our query
        followNextLink(axiosGetRequest)
            .then((success) => {
                const successValue = success.data.value;
325

326
327
                // Array that will hold the combined observations
                const combinedObservations = [];
328

329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
                successValue.forEach((dataObj) => {
                    // Each page of results will have a dataArray that holds the observations
                    const dataArrays = dataObj.dataArray;

                    combinedObservations.push(...dataArrays);
                });
                // DEBUG: Check total number of observations
                console.log(combinedObservations.length);
                // DEBUG: Print the array of observations
                console.log(combinedObservations);

                return combinedObservations;
            })
            .catch((err) => {
                console.log(err);
            })
            .then((observationArr) => {
Sven Schneider's avatar
minor    
Sven Schneider committed
346
                var agg = aggregateResponse(observationArr, 0, 'min');
347
                console.log(agg);
348
349
                drawHeatMapHC(formatSTAResponseForHeatMap(agg));
                drawLineChartHC(formatSTAResponseForLineChart(agg));
350
351
352
            });

    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
353

354
355
};

356
activate3DTileFeaturePicking();