-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
375 lines (330 loc) · 15.2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IceBoost Glacier Model Visualization</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.7.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.7.0/mapbox-gl.js"></script>
<script src="https://cdn.jsdelivr.net/npm/rbush"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#left-panel {
position: absolute;
top: 10px;
left: 10px;
width: 20%;
height: 20%;
background-color: rgba(0, 0, 0, 0.0);
padding: 10px;
font-family: Arial, sans-serif;
font-size: 12px;
}
#colorbar-container {
position: absolute;
bottom: 5vh; /* Set distance from the bottom of the screen (10% from the bottom) */
right: clamp(40px, 1vw, 50px);
width: 7vw; /* Set width as a percentage of the viewport width */
height: 25vh; /* Set height as a percentage of the viewport height */
display: flex;
flex-direction: row;
background-color: none;
}
#colorbar {
width: 20%; /* Width of the colorbar */
height: 100%; /* Full height of the container */
background: linear-gradient(to top,
rgb(0, 0, 127), rgb(0, 0, 132), rgb(0, 0, 136), rgb(0, 0, 141), rgb(0, 0, 145), rgb(0, 0, 150),
rgb(0, 0, 154), rgb(0, 0, 159), rgb(0, 0, 163), rgb(0, 0, 168), rgb(0, 0, 172), rgb(0, 0, 177),
rgb(0, 0, 182), rgb(0, 0, 186), rgb(0, 0, 191), rgb(0, 0, 195), rgb(0, 0, 200), rgb(0, 0, 204),
rgb(0, 0, 209), rgb(0, 0, 213), rgb(0, 0, 218), rgb(0, 0, 222), rgb(0, 0, 227), rgb(0, 0, 232),
rgb(0, 0, 236), rgb(0, 0, 241), rgb(0, 0, 245), rgb(0, 0, 250), rgb(0, 0, 254), rgb(0, 0, 255),
rgb(0, 0, 255), rgb(0, 0, 255), rgb(0, 0, 255), rgb(0, 4, 255), rgb(0, 8, 255), rgb(0, 12, 255),
rgb(0, 16, 255), rgb(0, 20, 255), rgb(0, 24, 255), rgb(0, 28, 255), rgb(0, 32, 255), rgb(0, 36, 255),
rgb(0, 40, 255), rgb(0, 44, 255), rgb(0, 48, 255), rgb(0, 52, 255), rgb(0, 56, 255), rgb(0, 60, 255),
rgb(0, 64, 255), rgb(0, 68, 255), rgb(0, 72, 255), rgb(0, 76, 255), rgb(0, 80, 255), rgb(0, 84, 255),
rgb(0, 88, 255), rgb(0, 92, 255), rgb(0, 96, 255), rgb(0, 100, 255), rgb(0, 104, 255), rgb(0, 108, 255),
rgb(0, 112, 255), rgb(0, 116, 255), rgb(0, 120, 255), rgb(0, 124, 255), rgb(0, 128, 255), rgb(0, 132, 255),
rgb(0, 136, 255), rgb(0, 140, 255), rgb(0, 144, 255), rgb(0, 148, 255), rgb(0, 152, 255), rgb(0, 156, 255),
rgb(0, 160, 255), rgb(0, 164, 255), rgb(0, 168, 255), rgb(0, 172, 255), rgb(0, 176, 255), rgb(0, 180, 255),
rgb(0, 184, 255), rgb(0, 188, 255), rgb(0, 192, 255), rgb(0, 196, 255), rgb(0, 200, 255), rgb(0, 204, 255),
rgb(0, 208, 255), rgb(0, 212, 255), rgb(0, 216, 255), rgb(0, 220, 254), rgb(0, 224, 250), rgb(0, 228, 247),
rgb(2, 232, 244), rgb(5, 236, 241), rgb(8, 240, 237), rgb(12, 244, 234), rgb(15, 248, 231), rgb(18, 252, 228),
rgb(21, 255, 225), rgb(24, 255, 221), rgb(28, 255, 218), rgb(31, 255, 215), rgb(34, 255, 212), rgb(37, 255, 208),
rgb(41, 255, 205), rgb(44, 255, 202), rgb(47, 255, 199), rgb(50, 255, 195), rgb(54, 255, 192), rgb(57, 255, 189),
rgb(60, 255, 186), rgb(63, 255, 183), rgb(66, 255, 179), rgb(70, 255, 176), rgb(73, 255, 173), rgb(76, 255, 170),
rgb(79, 255, 166), rgb(83, 255, 163), rgb(86, 255, 160), rgb(89, 255, 157), rgb(92, 255, 154), rgb(95, 255, 150),
rgb(99, 255, 147), rgb(102, 255, 144), rgb(105, 255, 141), rgb(108, 255, 137), rgb(112, 255, 134), rgb(115, 255, 131),
rgb(118, 255, 128), rgb(121, 255, 125), rgb(124, 255, 121), rgb(128, 255, 118), rgb(131, 255, 115), rgb(134, 255, 112),
rgb(137, 255, 108), rgb(141, 255, 105), rgb(144, 255, 102), rgb(147, 255, 99), rgb(150, 255, 95), rgb(154, 255, 92),
rgb(157, 255, 89), rgb(160, 255, 86), rgb(163, 255, 83), rgb(166, 255, 79), rgb(170, 255, 76), rgb(173, 255, 73),
rgb(176, 255, 70), rgb(179, 255, 66), rgb(183, 255, 63), rgb(186, 255, 60), rgb(189, 255, 57), rgb(192, 255, 54),
rgb(195, 255, 50), rgb(199, 255, 47), rgb(202, 255, 44), rgb(205, 255, 41), rgb(208, 255, 37), rgb(212, 255, 34),
rgb(215, 255, 31), rgb(218, 255, 28), rgb(221, 255, 24), rgb(224, 255, 21), rgb(228, 255, 18), rgb(231, 255, 15),
rgb(234, 255, 12), rgb(237, 255, 8), rgb(241, 252, 5), rgb(244, 248, 2), rgb(247, 244, 0), rgb(250, 240, 0),
rgb(254, 237, 0), rgb(255, 233, 0), rgb(255, 229, 0), rgb(255, 226, 0), rgb(255, 222, 0), rgb(255, 218, 0),
rgb(255, 215, 0), rgb(255, 211, 0), rgb(255, 207, 0), rgb(255, 203, 0), rgb(255, 200, 0), rgb(255, 196, 0),
rgb(255, 192, 0), rgb(255, 189, 0), rgb(255, 185, 0), rgb(255, 181, 0), rgb(255, 177, 0), rgb(255, 174, 0),
rgb(255, 170, 0), rgb(255, 166, 0), rgb(255, 163, 0), rgb(255, 159, 0), rgb(255, 155, 0), rgb(255, 152, 0),
rgb(255, 148, 0), rgb(255, 144, 0), rgb(255, 140, 0), rgb(255, 137, 0), rgb(255, 133, 0), rgb(255, 129, 0),
rgb(255, 126, 0), rgb(255, 122, 0), rgb(255, 118, 0), rgb(255, 115, 0), rgb(255, 111, 0), rgb(255, 107, 0),
rgb(255, 103, 0), rgb(255, 100, 0), rgb(255, 96, 0), rgb(255, 92, 0), rgb(255, 89, 0), rgb(255, 85, 0),
rgb(255, 81, 0), rgb(255, 77, 0), rgb(255, 74, 0), rgb(255, 70, 0), rgb(255, 66, 0), rgb(255, 63, 0),
rgb(255, 59, 0), rgb(255, 55, 0), rgb(255, 52, 0), rgb(255, 48, 0), rgb(255, 44, 0), rgb(255, 40, 0),
rgb(255, 37, 0), rgb(255, 33, 0), rgb(255, 29, 0), rgb(255, 26, 0), rgb(255, 22, 0), rgb(254, 18, 0),
rgb(250, 15, 0), rgb(245, 11, 0), rgb(241, 7, 0), rgb(236, 3, 0), rgb(232, 0, 0), rgb(227, 0, 0),
rgb(222, 0, 0), rgb(218, 0, 0), rgb(213, 0, 0), rgb(209, 0, 0), rgb(204, 0, 0), rgb(200, 0, 0),
rgb(195, 0, 0), rgb(191, 0, 0), rgb(186, 0, 0), rgb(182, 0, 0), rgb(177, 0, 0), rgb(172, 0, 0),
rgb(168, 0, 0), rgb(163, 0, 0), rgb(159, 0, 0), rgb(154, 0, 0), rgb(150, 0, 0), rgb(145, 0, 0),
rgb(141, 0, 0), rgb(136, 0, 0), rgb(132, 0, 0), rgb(127, 0, 0));
border: 1px solid black; /* Optional border */
}
#side-labels {
display: flex;
flex-direction: row; /* Arrange labels and title in a row */
align-items: center; /* Align items vertically centered */
margin-left: 10px; /* Space between colorbar and labels */
}
#colorbar-labels {
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-between;
font-family: Arial, sans-serif;
font-weight: normal;
font-size: clamp(12px, 0.8vw, 18px);
color: black;
text-align: left;
}
#colorbar-title {
writing-mode: vertical-rl;
transform: rotate(180deg);
font-family: Arial, sans-serif;
font-size: clamp(12px, 1.5vw, 18px);
color: black;
}
#coordinates {
position: absolute;
bottom: 10vh; /* 10% of viewport height from the bottom */
left: 1vw; /* 1% of viewport width from the left */
/*background-color: white;*/
padding: 5px;
font-family: Arial, sans-serif;
color: blue;
font-size: clamp(12px, 2vw, 18px);
font-weight: normal;
}
.loader {
border: 8px solid rgba(255, 255, 255, 0.3); /* Light border */
border-top: 8px solid #0074d9; /* Darker color for the spinner */
border-radius: 50%;
width: 50px; /* Size of the spinner */
height: 50px; /* Size of the spinner */
animation: spin 1s linear infinite; /* Animation properties */
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div id="map"></div>
<div id="loading" style="display: none; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000;">
<div class="loader"></div>
<p style="text-align: center; margin-top: 10px;">Loading tiles...</p>
</div>
<div id="left-panel">
<img src="logo_iceboost.svg" alt="ICEBOOST logo" style="max-width: 100%;">
<h4 style="color: blue;">Gradient-Boosted Regression Tree ensemble for ice thickness retrieval of the world's glaciers</h4>
</div>
<!-- Vertical colorbar for ice thickness -->
<div id="colorbar-container">
<div id="colorbar"></div> <!-- Vertical colorbar gradient -->
<div id="side-labels">
<div id="colorbar-labels">
<span>800+</span>
<span>700</span>
<span>600</span>
<span>500</span>
<span>400</span>
<span>300</span>
<span>200</span>
<span>100</span>
<span>0</span>
</div>
<div id="colorbar-title">Ice thickness (m)</div>
</div>
</div>
<div id="coordinates">Lat: N/A, Lon: N/A</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibWFmZmU4OSIsImEiOiJjbTExZXM4bmEwcmFkMmtxMnlleDE2aWo2In0.E6BiHnnixuupE5Zpx9FS8g';
const map = new mapboxgl.Map({
container: 'map', // container ID
//style: 'mapbox://styles/maffe89/cm2wh6ece00in01pw42nsbsxq',
style: 'mapbox://styles/mapbox/light-v11',
//style: 'mapbox://styles/mapbox/satellite-v9',
center: [-16, 64], // starting position [lng, lat]. Note that lat must be set between -90 and 90
zoom: 6, // starting zoom
});
// Add a scale bar to a map
map.addControl(new mapboxgl.ScaleControl({position: 'bottom-left'}));
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
// Display latitude and longitude as the mouse moves
map.on('mousemove', function (e) {
const coords = e.lngLat; // Get the coordinates
document.getElementById('coordinates').innerHTML = `<span>Lat: ${coords.lat.toFixed(5)}, Lon: ${coords.lng.toFixed(5)}</span>`; // Update the coordinates display
});
const githubRepo = 'nmaffe/iceboost_webapp'; // Replace with your GitHub username and repo
const s3BucketURL = 'https://as3-iceboost-tiles.s3.amazonaws.com/tiles/RGI62';
const jsonFileUrl = `${s3BucketURL}/tileFolders_RGI62_with_bbox.json.gz`; // Path to your JSON file
let glaciers; // Declare glaciers outside the fetch so it can be accessed in moveend
const rbushIndex = new RBush(); // Initialize RBush
// Fetch the tile folder information (loads the JSON file)
fetch(jsonFileUrl)
.then(response => {
if (!response.ok) {
throw new Error('Failed to fetch tile folders');
}
return response.json();
})
.then(data => {
glaciers = data.glaciers; // Store the glaciers array for later use
createSpatialIndex(glaciers); // Create the spatial index
})
.catch(error => {
console.error('Fetch error:', error);
});
function createSpatialIndex(glaciers) {
const items = glaciers.map(glacier => {
const [minX, minY, maxX, maxY] = glacier.bbox;
return {
minX: minX,
minY: minY,
maxX: maxX,
maxY: maxY,
id: glacier.id // Store glacier ID for reference
};
});
rbushIndex.load(items); // Load items into the RBush index
}
function loadTiles() {
document.getElementById('loading').style.display = 'block'; // Show loading indicator
const bounds = map.getBounds();
const sw = bounds.getSouthWest();
const ne = bounds.getNorthEast();
// Query the RBush index to find glaciers within the current bounds
const foundGlaciers = rbushIndex.search({
minX: sw.lng,
minY: sw.lat,
maxX: ne.lng,
maxY: ne.lat
});
console.log('Found glacier IDs:', foundGlaciers.map(glacier => glacier.id));
// Convert found glaciers to a set for quick lookup
const foundGlacierIds = new Set(foundGlaciers.map(glacier => glacier.id));
// Remove layers and sources that are no longer in view
glaciers.forEach(glacier => {
const sourceId = `${glacier.id}-tiles`;
const layerId = `${glacier.id}-tiles-layer`;
// Remove layers and sources that are no longer in view
if (!foundGlacierIds.has(glacier.id)) {
if (map.getLayer(layerId)) {
map.removeLayer(layerId);
}
if (map.getSource(sourceId)) {
map.removeSource(sourceId);
}
}
});
// Create a lookup for glacier data for faster access
const glacierDataMap = new Map(glaciers.map(g => [g.id, g]));
// Add only the necessary sources and layers that are in the view
foundGlaciers.forEach(glacier => {
const glacierData = glacierDataMap.get(glacier.id);
if (glacierData && glacierData.is_tile) {
const sourceId = `${glacier.id}-tiles`;
const layerId = `${glacier.id}-tiles-layer`;
// Add source only if it does not exist
if (!map.getSource(sourceId)) {
map.addSource(sourceId, {
'type': 'raster',
'tiles': [
`${s3BucketURL}/${glacier.id}/{z}/{x}/{y}.png`
//`http://localhost:8000/RGI62/${glacier.id}/{z}/{x}/{y}.png`
],
'tileSize': 256,
'scheme': 'tms'
});
console.log(`Source added: ${sourceId}`);
}
// Add layer only if it does not exist
if (!map.getLayer(layerId)) {
map.addLayer({
'id': layerId,
'type': 'raster',
'source': sourceId,
'paint': {
'raster-opacity': 0.6 // Adjust as needed
},
});
console.log(`Layer added: ${layerId}`);
}
}
});
document.getElementById('loading').style.display = 'none'; // Hide loading indicator after loading tiles
}
let debounceTimer;
let tilesRendered = false;
// Set up the initial state when the map is loaded
map.on('load', () => {
// Add the Mapbox Terrain-DEM v1 tileset as a source
map.addSource('terrain-dem', {
'type': 'raster-dem',
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1', // Terrain DEM v1 tileset
'tileSize': 256
});
// Add the hillshade layer
map.addLayer({
'id': 'hillshade',
'type': 'hillshade',
'source': 'terrain-dem',
'paint': {
'hillshade-exaggeration': 1.0,
'hillshade-highlight-color': '#ffffff', // Optional: highlight color
'hillshade-shadow-color': '#555555', // Optional: shadow color
'hillshade-illumination-direction': 315 // Optional: angle of illumination
}
});
console.log("Map has fully loaded.");
loadTiles(); // Load tiles immediately after the map is loaded
tilesRendered = true; // Set the flag to true since the initial tiles are loaded
});
// Reset the flag when the map starts moving or zooming
map.on('zoomstart', () => {
tilesRendered = false; // Reset flag since the map is changing
});
map.on('movestart', () => {
tilesRendered = false; // Reset flag since the map is changing
});
// Use 'idle' to determine when the map has finished moving/zooming
map.on('idle', () => {
if (tilesRendered) {
console.log("Map is idle and tiles are already rendered.");
return; // No need to load tiles again
}
// Wait for 2 seconds before calling loadTiles
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
if (map.isStyleLoaded() && map.loaded()) {
console.log("Loading tiles after 2 seconds.");
loadTiles(); // Load tiles after the map is idle for 2 seconds
tilesRendered = true; // Set flag to true again
}
}, 2000); // Adjust the delay as needed
});
</script>
</body>
</html>