forked from cafri-labs/esf-openfrontier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.html
More file actions
238 lines (215 loc) · 9.97 KB
/
Copy pathviewer.html
File metadata and controls
238 lines (215 loc) · 9.97 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ESF OpenFrontier - Carbon Data Viewer</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
#map { position: absolute; top: 0; left: 0; right: 0; bottom: 0; }
.controls {
position: absolute; bottom: 30px; left: 50%; transform: translateX(-50%);
z-index: 1000; background: rgba(255,255,255,0.95); padding: 16px 24px;
border-radius: 10px; box-shadow: 0 2px 12px rgba(0,0,0,0.3);
display: flex; flex-direction: column; align-items: center; gap: 10px;
min-width: 400px;
}
.controls-row {
display: flex; align-items: center; gap: 12px; width: 100%;
}
.variable-buttons { display: flex; gap: 6px; }
.variable-buttons button {
padding: 6px 14px; border: 2px solid #555; border-radius: 6px;
background: white; cursor: pointer; font-size: 13px; font-weight: 600;
transition: all 0.15s;
}
.variable-buttons button.active {
background: #2d6a4f; color: white; border-color: #2d6a4f;
}
.variable-buttons button:hover:not(.active) { background: #e8e8e8; }
.slider-row { display: flex; align-items: center; gap: 10px; width: 100%; }
#time-slider { flex: 1; cursor: pointer; accent-color: #2d6a4f; }
#year-display {
font-size: 22px; font-weight: 700; color: #2d6a4f;
min-width: 50px; text-align: center;
}
.play-btn {
width: 36px; height: 36px; border-radius: 50%; border: 2px solid #2d6a4f;
background: white; cursor: pointer; display: flex; align-items: center;
justify-content: center; font-size: 14px; transition: all 0.15s;
}
.play-btn:hover { background: #2d6a4f; color: white; }
.play-btn.playing { background: #2d6a4f; color: white; }
.title { font-size: 14px; font-weight: 600; color: #333; }
/* Colorbar */
.colorbar {
position: absolute; z-index: 1000;
bottom: 110px; right: 12px;
background: rgba(255,255,255,0.95); padding: 10px 12px;
border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.25);
display: flex; align-items: stretch; gap: 8px;
}
.colorbar-gradient {
width: 18px; height: 160px; border-radius: 3px; border: 1px solid #ccc;
background: linear-gradient(to top,
#440154 0%, #482878 12.5%, #3e4989 25%, #31688e 37.5%,
#26828e 50%, #1f9e89 62.5%, #35b779 75%, #6ece58 87.5%,
#fde725 100%);
}
.colorbar-labels {
display: flex; flex-direction: column; justify-content: space-between;
font-size: 11px; font-weight: 600; color: #444;
}
.colorbar-title {
font-size: 11px; font-weight: 700; color: #333;
writing-mode: vertical-rl; text-orientation: mixed;
transform: rotate(180deg);
display: flex; align-items: center; justify-content: center;
}
/* Point query popup */
.leaflet-popup-content { font-family: inherit; font-size: 13px; }
.point-val { font-size: 18px; font-weight: 700; color: #2d6a4f; }
.point-label { font-size: 11px; color: #666; }
.point-loading { color: #999; font-style: italic; }
</style>
</head>
<body>
<div id="map"></div>
<div class="colorbar" id="colorbar">
<div class="colorbar-title" id="colorbar-title">AGB (Mg/ha)</div>
<div class="colorbar-gradient"></div>
<div class="colorbar-labels">
<span id="cb-max">250</span>
<span id="cb-mid">125</span>
<span id="cb-min">0</span>
</div>
</div>
<div class="controls">
<div class="controls-row">
<span class="title">Variable:</span>
<div class="variable-buttons">
<button data-var="agb" class="active" title="Aboveground Biomass (Mg/ha)">AGB</button>
<button data-var="agc" title="Aboveground Carbon (Mg C/ha)">AGC</button>
<button data-var="bgc" title="Belowground Carbon (Mg C/ha)">BGC</button>
</div>
<span class="title" style="margin-left:auto; font-size:12px; color:#888;">Click map for values</span>
</div>
<div class="slider-row">
<button class="play-btn" id="play-btn" title="Play/Pause">▶</button>
<input type="range" id="time-slider" min="1990" max="2023" value="2023" step="1">
<span id="year-display">2023</span>
</div>
</div>
<script>
const TITILER = 'https://vaiqqtybhb.execute-api.us-east-1.amazonaws.com';
const R2_BUCKET = 'osc';
const VAR_INFO = {
agb: { label: 'AGB', fullLabel: 'Aboveground Biomass', unit: 'Mg/ha', max: 250 },
agc: { label: 'AGC', fullLabel: 'Aboveground Carbon', unit: 'Mg C/ha', max: 125 },
bgc: { label: 'BGC', fullLabel: 'Belowground Carbon', unit: 'Mg C/ha', max: 75 },
};
let currentVar = 'agb';
let currentYear = 2023;
let playing = false;
let playInterval = null;
// Initialize map centered on New York State
const map = L.map('map').setView([42.8, -75.5], 7);
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© OpenStreetMap © CARTO',
maxZoom: 19
}).addTo(map);
let dataLayer = null;
function tileUrl(variable, year) {
return `${TITILER}/esf/tiles/WebMercatorQuad/{z}/{x}/{y}.png`
+ `?variable=${variable}&time=${year}&colormap_name=viridis`;
}
function cogUrl(variable, year) {
return `s3://${R2_BUCKET}/esf-${variable}/${variable}_${year}_cog.tif`;
}
function updateColorbar() {
const v = VAR_INFO[currentVar];
document.getElementById('colorbar-title').textContent = `${v.label} (${v.unit})`;
document.getElementById('cb-max').textContent = v.max;
document.getElementById('cb-mid').textContent = Math.round(v.max / 2);
document.getElementById('cb-min').textContent = '0';
}
function updateLayer() {
if (dataLayer) map.removeLayer(dataLayer);
dataLayer = L.tileLayer(tileUrl(currentVar, currentYear), {
opacity: 0.8,
attribution: 'ESF / Frontier Geospatial'
}).addTo(map);
document.getElementById('year-display').textContent = currentYear;
document.getElementById('time-slider').value = currentYear;
updateColorbar();
}
// Variable buttons
document.querySelectorAll('.variable-buttons button').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.variable-buttons button').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentVar = btn.dataset.var;
updateLayer();
});
});
// Time slider
const slider = document.getElementById('time-slider');
slider.addEventListener('input', () => {
currentYear = parseInt(slider.value);
updateLayer();
});
// Play button
const playBtn = document.getElementById('play-btn');
playBtn.addEventListener('click', () => {
playing = !playing;
playBtn.classList.toggle('playing', playing);
playBtn.innerHTML = playing ? '▮▮' : '▶';
if (playing) {
playInterval = setInterval(() => {
currentYear++;
if (currentYear > 2023) currentYear = 1990;
updateLayer();
}, 1000);
} else {
clearInterval(playInterval);
}
});
// Point query on click
map.on('click', async (e) => {
const { lng, lat } = e.latlng;
const v = VAR_INFO[currentVar];
const popup = L.popup()
.setLatLng(e.latlng)
.setContent('<span class="point-loading">Loading...</span>')
.openOn(map);
try {
const url = `${TITILER}/cog/point/${lng},${lat}?url=${encodeURIComponent(cogUrl(currentVar, currentYear))}`;
const res = await fetch(url);
const data = await res.json();
const val = data.values?.[0];
let html;
if (val === null || val === undefined || val === -9999) {
html = `<span class="point-label">${v.fullLabel} ${currentYear}</span><br>`;
html += '<span class="point-val" style="color:#999">No data</span>';
} else {
html = `<span class="point-label">${v.fullLabel} ${currentYear}</span><br>`;
html += `<span class="point-val">${val.toFixed(1)} ${v.unit}</span>`;
}
popup.setContent(html);
} catch (err) {
popup.setContent('<span class="point-loading">Error fetching value</span>');
}
});
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft' && currentYear > 1990) { currentYear--; updateLayer(); }
if (e.key === 'ArrowRight' && currentYear < 2023) { currentYear++; updateLayer(); }
if (e.key === ' ') { e.preventDefault(); playBtn.click(); }
});
updateLayer();
</script>
</body>
</html>