635 lines
31 KiB
HTML
635 lines
31 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>NDVI 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>
|
|
<script src="https://cdn.jsdelivr.net/npm/geotiff@2.0.7/dist-browser/geotiff.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/proj4@2.9.0/dist/proj4.js"></script>
|
|
<style>
|
|
body { margin: 0; font-family: sans-serif; }
|
|
.slider-container { position: sticky; top: 0; background: white; padding: 20px; z-index: 1000; border-bottom: 1px solid #ccc; }
|
|
.container { max-width: 1400px; margin: 0 auto; padding: 20px; }
|
|
.header { display: flex; gap: 20px; margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-top: 10px;padding-bottom: 20px;}
|
|
.header-col { flex: 1; }
|
|
.site-info h1 { margin: 0 0 10px 0; font-size: 24px; }
|
|
.site-info h2 { margin: 0 0 20px 0; font-size: 18px; color: #666; }
|
|
.phenocam-label { font-size: 12px; margin-bottom: 5px; color: #666; }
|
|
.phenocam-date { font-size: 11px; margin-top: 5px; color: #999; }
|
|
.phenocam-image { width: 100%; height: 200px; object-fit: contain; border: 1px solid #ccc; }
|
|
.sitemap { height: 200px; border: 1px solid #ccc; margin-top: 32px; }
|
|
#greennesstimeseries { margin-top: 0; }
|
|
#dateSlider { width: 100%; }
|
|
#dateDisplay { text-align: center; margin: 10px 0; font-size: 18px; }
|
|
.maps { display: flex; gap: 20px; }
|
|
.map-container { flex: 1; }
|
|
.map-container h3 { margin: 10px 0; text-align: center; }
|
|
.timeseries-label { font-size: 12px; margin-bottom: 5px; color: #666; }
|
|
.timeseries { width: 100%; height: 120px; border: 1px solid #ccc; margin-bottom: 10px; }
|
|
.map-label { font-size: 12px; margin-bottom: 5px; color: #666; }
|
|
.map-date { font-size: 11px; margin-top: 5px; color: #999; }
|
|
.map { height: 500px; border: 1px solid #ccc; }
|
|
.combined-plot { margin-top: 20px; }
|
|
.combined-plot-label { font-size: 12px; margin-bottom: 5px; color: #666; }
|
|
.combined-plot-canvas { width: 100%; height: 120px; border: 1px solid #ccc; }
|
|
.leaflet-image-layer { image-rendering: pixelated; }
|
|
.leaflet-control-attribution { display: none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="slider-container">
|
|
<input type="range" id="dateSlider" min="0" max="365" value="0">
|
|
<div id="dateDisplay">2024-01-01</div>
|
|
</div>
|
|
<div class="header">
|
|
<div class="header-col site-info">
|
|
<h1 id="siteName">Innsbruck</h1>
|
|
<h2 id="season">2024</h2>
|
|
<div class="phenocam-label">PhenoCam</div>
|
|
<div id="phenocamdate" class="phenocam-date"></div>
|
|
<img id="phenocamimage" class="phenocam-image" alt="PhenoCam">
|
|
</div>
|
|
<div class="header-col">
|
|
<div class="timeseries-label">Greenness Index Timeseries</div>
|
|
<canvas id="greennesstimeseries" class="timeseries"></canvas>
|
|
</div>
|
|
<div class="header-col">
|
|
<div id="sitemap" class="sitemap"></div>
|
|
</div>
|
|
</div>
|
|
<div class="maps">
|
|
<div class="map-container">
|
|
<h3>S2</h3>
|
|
<div class="timeseries-label">NDVI Timeseries</div>
|
|
<canvas id="s2timeseries" class="timeseries"></canvas>
|
|
<div class="timeseries-label">Greenness Index Timeseries</div>
|
|
<canvas id="s2gcctimeseries" class="timeseries"></canvas>
|
|
<div class="map-label">RGB Composite</div>
|
|
<div id="s2rgbdate" class="map-date"></div>
|
|
<div id="s2map" class="map"></div>
|
|
</div>
|
|
<div class="map-container">
|
|
<h3>Fusion</h3>
|
|
<div class="timeseries-label">NDVI Timeseries</div>
|
|
<canvas id="fusiontimeseries" class="timeseries"></canvas>
|
|
<div class="timeseries-label">Greenness Index Timeseries</div>
|
|
<canvas id="fusiongcctimeseries" class="timeseries"></canvas>
|
|
<div class="map-label">RGB Composite</div>
|
|
<div id="fusionrgbdate" class="map-date"></div>
|
|
<div id="fusionmap" class="map"></div>
|
|
</div>
|
|
<div class="map-container">
|
|
<h3>S3</h3>
|
|
<div class="timeseries-label">NDVI Timeseries</div>
|
|
<canvas id="s3timeseries" class="timeseries"></canvas>
|
|
<div class="timeseries-label">Greenness Index Timeseries</div>
|
|
<canvas id="s3gcctimeseries" class="timeseries"></canvas>
|
|
<div class="map-label">RGB Composite</div>
|
|
<div id="s3rgbdate" class="map-date"></div>
|
|
<div id="s3map" class="map"></div>
|
|
</div>
|
|
</div>
|
|
<div class="combined-plot">
|
|
<div class="combined-plot-label">Greenness Index Timeseries (S2 & Fusion)</div>
|
|
<canvas id="combinedgcctimeseries" class="combined-plot-canvas"></canvas>
|
|
</div>
|
|
</div>
|
|
<script>
|
|
proj4.defs("EPSG:32632", "+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs");
|
|
proj4.defs("EPSG:4326", "+proj=longlat +datum=WGS84 +no_defs");
|
|
|
|
const start = new Date(2024, 0, 1);
|
|
const slider = document.getElementById("dateSlider");
|
|
const dateDisplay = document.getElementById("dateDisplay");
|
|
const osmUrl = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";
|
|
const osmOpts = { attribution: "OpenStreetMap", opacity: 0.4 };
|
|
const mapOpts = { zoomControl: false };
|
|
const sitePosition = [47.116171, 11.320308];
|
|
const siteMap = L.map("sitemap", { zoomControl: false }).setView(sitePosition, 4).addLayer(L.tileLayer(osmUrl, { attribution: "OpenStreetMap", opacity: 1 }));
|
|
L.marker(sitePosition, { icon: L.divIcon({ className: "site-marker", html: "<div style='width:8px;height:8px;background:red;border:2px solid white;border-radius:50%;box-shadow:0 0 2px rgba(0,0,0,0.5);'></div>", iconSize: [8, 8] }) }).addTo(siteMap);
|
|
const maps = {
|
|
s2: L.map("s2map", mapOpts).setView(sitePosition, 12).addLayer(L.tileLayer(osmUrl, osmOpts)),
|
|
fusion: L.map("fusionmap", mapOpts).setView(sitePosition, 12).addLayer(L.tileLayer(osmUrl, osmOpts)),
|
|
s3: L.map("s3map", mapOpts).setView(sitePosition, 12).addLayer(L.tileLayer(osmUrl, osmOpts))
|
|
};
|
|
const overlays = { s2: null, fusion: null, s3: null };
|
|
const markers = { s2: null, fusion: null, s3: null };
|
|
let timeseries = { s2: [], fusion: [], s3: [] };
|
|
let greennessTimeseries = { s2: [], fusion: [], s3: [] };
|
|
let phenocamGreennessTimeseries = [];
|
|
|
|
// Add site marker to all maps
|
|
for (const source of ["s2", "fusion", "s3"]) {
|
|
markers[source] = L.marker(sitePosition, { icon: L.divIcon({ className: "site-marker", html: "<div style='width:5px;height:5px;background:red;border:1px solid white;border-radius:50%;box-shadow:0 0 1px rgba(0,0,0,0.5);'></div>", iconSize: [5, 5] }) }).addTo(maps[source]);
|
|
}
|
|
|
|
let syncing = false;
|
|
const allMaps = Object.values(maps);
|
|
const syncMaps = (sourceMap) => {
|
|
if (syncing) return;
|
|
syncing = true;
|
|
const center = sourceMap.getCenter();
|
|
const zoom = sourceMap.getZoom();
|
|
allMaps.forEach(m => {
|
|
if (m !== sourceMap) m.setView(center, zoom);
|
|
});
|
|
syncing = false;
|
|
};
|
|
allMaps.forEach(m => {
|
|
m.on("moveend zoomend", () => syncMaps(m));
|
|
});
|
|
|
|
async function loadTimeseries() {
|
|
const [s2, fusion, s3, s2gcc, fusiongcc, s3gcc, phenocam] = await Promise.all([
|
|
fetch("../data/innsbruck/2024/processed/ndvi/s2/timeseries.json").then(r => r.json()),
|
|
fetch("../data/innsbruck/2024/processed/ndvi/fusion/timeseries.json").then(r => r.json()).catch(() => []),
|
|
fetch("../data/innsbruck/2024/processed/ndvi/s3/timeseries.json").then(r => r.json()),
|
|
fetch("../data/innsbruck/2024/processed/gcc/s2/timeseries.json").then(r => r.json()).catch(() => []),
|
|
fetch("../data/innsbruck/2024/processed/gcc/fusion/timeseries.json").then(r => r.json()).catch(() => []),
|
|
fetch("../data/innsbruck/2024/processed/gcc/s3/timeseries.json").then(r => r.json()).catch(() => []),
|
|
fetch("../data/innsbruck/2024/raw/phenocam/timeseries.json").then(r => r.json()).catch(() => [])
|
|
]);
|
|
timeseries = { s2, fusion, s3 };
|
|
greennessTimeseries = { s2: s2gcc, fusion: fusiongcc, s3: s3gcc };
|
|
phenocamGreennessTimeseries = phenocam;
|
|
drawTimeseries();
|
|
drawGreennessTimeseries();
|
|
drawPhenocamGreennessTimeseries();
|
|
drawCombinedGreennessTimeseries();
|
|
}
|
|
|
|
function drawGreennessTimeseries() {
|
|
const currentDate = dateFromDays(parseInt(slider.value));
|
|
for (const source of ["s2", "fusion", "s3"]) {
|
|
const canvas = document.getElementById(`${source}gcctimeseries`);
|
|
const ctx = canvas.getContext("2d");
|
|
canvas.width = canvas.offsetWidth;
|
|
canvas.height = 120;
|
|
const w = canvas.width, h = canvas.height;
|
|
const pad = 30;
|
|
const plotW = w - pad * 2, plotH = h - pad * 2;
|
|
|
|
ctx.clearRect(0, 0, w, h);
|
|
let data = greennessTimeseries[source].filter(t => t.date && t.greenness_index !== null && t.greenness_index !== undefined);
|
|
if (!data.length) continue;
|
|
|
|
const dates = data.map(t => new Date(t.date));
|
|
const minDate = new Date(Math.min(...dates));
|
|
const maxDate = new Date(Math.max(...dates));
|
|
const dateRange = maxDate - minDate || 1;
|
|
|
|
const values = data.map(t => t.greenness_index);
|
|
const minVal = Math.min(...values);
|
|
const maxVal = Math.max(...values);
|
|
const valRange = maxVal - minVal || 1;
|
|
|
|
const x = (d) => pad + ((new Date(d) - minDate) / dateRange) * plotW;
|
|
const y = (v) => pad + plotH - ((v - minVal) / valRange) * plotH;
|
|
|
|
ctx.strokeStyle = "#ccc";
|
|
ctx.beginPath();
|
|
ctx.moveTo(pad, pad);
|
|
ctx.lineTo(pad, pad + plotH);
|
|
ctx.lineTo(pad + plotW, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
ctx.fillStyle = "#000";
|
|
ctx.font = "9px sans-serif";
|
|
ctx.fillText(minVal.toFixed(3), 2, pad + plotH + 10);
|
|
ctx.fillText(maxVal.toFixed(3), 2, pad + 3);
|
|
|
|
ctx.strokeStyle = "#00aa00";
|
|
ctx.beginPath();
|
|
let first = true;
|
|
for (const t of data) {
|
|
const px = x(t.date), py = y(t.greenness_index);
|
|
if (first) { ctx.moveTo(px, py); first = false; }
|
|
else ctx.lineTo(px, py);
|
|
}
|
|
ctx.stroke();
|
|
ctx.fillStyle = "#888";
|
|
const axisY = pad + plotH;
|
|
for (const t of data) ctx.fillRect(x(t.date) - 1, axisY - 1, 2, 2);
|
|
|
|
const xPos = x(currentDate);
|
|
ctx.strokeStyle = "#f00";
|
|
ctx.lineWidth = 2;
|
|
ctx.beginPath();
|
|
ctx.moveTo(xPos, pad);
|
|
ctx.lineTo(xPos, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
const closest = data.reduce((c, t) =>
|
|
Math.abs(new Date(t.date) - new Date(currentDate)) < Math.abs(new Date(c.date) - new Date(currentDate)) ? t : c
|
|
);
|
|
if (closest && closest.greenness_index !== null) {
|
|
const yPos = y(closest.greenness_index);
|
|
ctx.fillStyle = "#f00";
|
|
ctx.font = "bold 10px sans-serif";
|
|
ctx.fillText(closest.greenness_index.toFixed(3), xPos + 5, yPos - 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
function drawPhenocamGreennessTimeseries() {
|
|
const canvas = document.getElementById("greennesstimeseries");
|
|
const ctx = canvas.getContext("2d");
|
|
canvas.width = canvas.offsetWidth;
|
|
canvas.height = 120;
|
|
const w = canvas.width, h = canvas.height;
|
|
const pad = 30;
|
|
const plotW = w - pad * 2, plotH = h - pad * 2;
|
|
|
|
ctx.clearRect(0, 0, w, h);
|
|
const data = phenocamGreennessTimeseries.filter(t => t.date && t.greenness_index !== null && t.greenness_index !== undefined);
|
|
if (!data.length) return;
|
|
|
|
const dates = data.map(t => new Date(t.date));
|
|
const minDate = new Date(Math.min(...dates));
|
|
const maxDate = new Date(Math.max(...dates));
|
|
const dateRange = maxDate - minDate || 1;
|
|
|
|
const values = data.map(t => t.greenness_index);
|
|
const minVal = Math.min(...values);
|
|
const maxVal = Math.max(...values);
|
|
const valRange = maxVal - minVal || 1;
|
|
|
|
const x = (d) => pad + ((new Date(d) - minDate) / dateRange) * plotW;
|
|
const y = (v) => pad + plotH - ((v - minVal) / valRange) * plotH;
|
|
|
|
ctx.strokeStyle = "#ccc";
|
|
ctx.beginPath();
|
|
ctx.moveTo(pad, pad);
|
|
ctx.lineTo(pad, pad + plotH);
|
|
ctx.lineTo(pad + plotW, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
ctx.fillStyle = "#000";
|
|
ctx.font = "9px sans-serif";
|
|
ctx.fillText(minVal.toFixed(3), 2, pad + plotH + 10);
|
|
ctx.fillText(maxVal.toFixed(3), 2, pad + 3);
|
|
|
|
ctx.strokeStyle = "#00aa00";
|
|
ctx.beginPath();
|
|
let first = true;
|
|
for (const t of data) {
|
|
const px = x(t.date), py = y(t.greenness_index);
|
|
if (first) { ctx.moveTo(px, py); first = false; }
|
|
else ctx.lineTo(px, py);
|
|
}
|
|
ctx.stroke();
|
|
ctx.fillStyle = "#888";
|
|
const axisY = pad + plotH;
|
|
for (const t of data) ctx.fillRect(x(t.date) - 1, axisY - 1, 2, 2);
|
|
|
|
const currentDate = dateFromDays(parseInt(slider.value));
|
|
const xPos = x(currentDate);
|
|
ctx.strokeStyle = "#f00";
|
|
ctx.lineWidth = 2;
|
|
ctx.beginPath();
|
|
ctx.moveTo(xPos, pad);
|
|
ctx.lineTo(xPos, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
const closest = data.reduce((c, t) =>
|
|
Math.abs(new Date(t.date) - new Date(currentDate)) < Math.abs(new Date(c.date) - new Date(currentDate)) ? t : c
|
|
);
|
|
if (closest && closest.greenness_index !== null) {
|
|
const yPos = y(closest.greenness_index);
|
|
ctx.fillStyle = "#f00";
|
|
ctx.font = "bold 10px sans-serif";
|
|
ctx.fillText(closest.greenness_index.toFixed(3), xPos + 5, yPos - 5);
|
|
}
|
|
}
|
|
|
|
function drawCombinedGreennessTimeseries() {
|
|
const canvas = document.getElementById("combinedgcctimeseries");
|
|
const ctx = canvas.getContext("2d");
|
|
canvas.width = canvas.offsetWidth;
|
|
canvas.height = 120;
|
|
const w = canvas.width, h = canvas.height;
|
|
const pad = 30;
|
|
const plotW = w - pad * 2, plotH = h - pad * 2;
|
|
|
|
ctx.clearRect(0, 0, w, h);
|
|
const s2data = greennessTimeseries.s2.filter(t => t.date && t.greenness_index !== null && t.greenness_index !== undefined);
|
|
const fusiondata = greennessTimeseries.fusion.filter(t => t.date && t.greenness_index !== null && t.greenness_index !== undefined);
|
|
const phenocamdata = phenocamGreennessTimeseries.filter(t => t.date && t.greenness_index !== null && t.greenness_index !== undefined);
|
|
if (!s2data.length && !fusiondata.length && !phenocamdata.length) return;
|
|
|
|
const allDates = [...s2data.map(t => new Date(t.date)), ...fusiondata.map(t => new Date(t.date)), ...phenocamdata.map(t => new Date(t.date))];
|
|
const minDate = new Date(Math.min(...allDates));
|
|
const maxDate = new Date(Math.max(...allDates));
|
|
const dateRange = maxDate - minDate || 1;
|
|
|
|
const allValues = [...s2data.map(t => t.greenness_index), ...fusiondata.map(t => t.greenness_index), ...phenocamdata.map(t => t.greenness_index)];
|
|
const minVal = Math.min(...allValues);
|
|
const maxVal = Math.max(...allValues);
|
|
const valRange = maxVal - minVal || 1;
|
|
|
|
const x = (d) => pad + ((new Date(d) - minDate) / dateRange) * plotW;
|
|
const y = (v) => pad + plotH - ((v - minVal) / valRange) * plotH;
|
|
|
|
ctx.strokeStyle = "#ccc";
|
|
ctx.beginPath();
|
|
ctx.moveTo(pad, pad);
|
|
ctx.lineTo(pad, pad + plotH);
|
|
ctx.lineTo(pad + plotW, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
ctx.fillStyle = "#000";
|
|
ctx.font = "9px sans-serif";
|
|
ctx.fillText(minVal.toFixed(3), 2, pad + plotH + 10);
|
|
ctx.fillText(maxVal.toFixed(3), 2, pad + 3);
|
|
|
|
if (s2data.length) {
|
|
ctx.strokeStyle = "#ff6600";
|
|
ctx.beginPath();
|
|
let first = true;
|
|
for (const t of s2data) {
|
|
const px = x(t.date), py = y(t.greenness_index);
|
|
if (first) { ctx.moveTo(px, py); first = false; }
|
|
else ctx.lineTo(px, py);
|
|
}
|
|
ctx.stroke();
|
|
}
|
|
|
|
if (fusiondata.length) {
|
|
ctx.strokeStyle = "#9900cc";
|
|
ctx.beginPath();
|
|
let first = true;
|
|
for (const t of fusiondata) {
|
|
const px = x(t.date), py = y(t.greenness_index);
|
|
if (first) { ctx.moveTo(px, py); first = false; }
|
|
else ctx.lineTo(px, py);
|
|
}
|
|
ctx.stroke();
|
|
}
|
|
|
|
if (phenocamdata.length) {
|
|
ctx.strokeStyle = "#00aa00";
|
|
ctx.beginPath();
|
|
let first = true;
|
|
for (const t of phenocamdata) {
|
|
const px = x(t.date), py = y(t.greenness_index);
|
|
if (first) { ctx.moveTo(px, py); first = false; }
|
|
else ctx.lineTo(px, py);
|
|
}
|
|
ctx.stroke();
|
|
}
|
|
|
|
ctx.fillStyle = "#888";
|
|
const axisY = pad + plotH;
|
|
for (const t of [...s2data, ...fusiondata, ...phenocamdata]) ctx.fillRect(x(t.date) - 1, axisY - 1, 2, 2);
|
|
|
|
const legendX = pad + plotW - 80, legendY = pad + 5;
|
|
ctx.font = "9px sans-serif";
|
|
if (s2data.length) { ctx.strokeStyle = "#ff6600"; ctx.beginPath(); ctx.moveTo(legendX, legendY); ctx.lineTo(legendX + 15, legendY); ctx.stroke(); ctx.fillStyle = "#000"; ctx.fillText("S2", legendX + 18, legendY + 3); }
|
|
if (fusiondata.length) { ctx.strokeStyle = "#9900cc"; ctx.beginPath(); ctx.moveTo(legendX, legendY + 12); ctx.lineTo(legendX + 15, legendY + 12); ctx.stroke(); ctx.fillStyle = "#000"; ctx.fillText("Fusion", legendX + 18, legendY + 15); }
|
|
if (phenocamdata.length) { ctx.strokeStyle = "#00aa00"; ctx.beginPath(); ctx.moveTo(legendX, legendY + 24); ctx.lineTo(legendX + 15, legendY + 24); ctx.stroke(); ctx.fillStyle = "#000"; ctx.fillText("PhenoCam", legendX + 18, legendY + 27); }
|
|
|
|
const currentDate = dateFromDays(parseInt(slider.value));
|
|
const xPos = x(currentDate);
|
|
ctx.strokeStyle = "#f00";
|
|
ctx.lineWidth = 2;
|
|
ctx.beginPath();
|
|
ctx.moveTo(xPos, pad);
|
|
ctx.lineTo(xPos, pad + plotH);
|
|
ctx.stroke();
|
|
}
|
|
|
|
function drawTimeseries() {
|
|
const currentDate = dateFromDays(parseInt(slider.value));
|
|
for (const source of ["s2", "fusion", "s3"]) {
|
|
const canvas = document.getElementById(`${source}timeseries`);
|
|
const ctx = canvas.getContext("2d");
|
|
canvas.width = canvas.offsetWidth;
|
|
canvas.height = 120;
|
|
const w = canvas.width, h = canvas.height;
|
|
const pad = 30;
|
|
const plotW = w - pad * 2, plotH = h - pad * 2;
|
|
|
|
ctx.clearRect(0, 0, w, h);
|
|
// Get all data with valid dates (dates are now in ISO format from JSON)
|
|
let data = timeseries[source].filter(t => {
|
|
if (!t.date) return false;
|
|
const date = new Date(t.date);
|
|
return !isNaN(date.getTime());
|
|
});
|
|
|
|
// Filter to only entries with non-null NDVI values for plotting
|
|
const dataWithNdvi = data.filter(t => t.ndvi !== null);
|
|
if (!dataWithNdvi.length) continue;
|
|
|
|
// Use data with NDVI for plotting
|
|
data = dataWithNdvi;
|
|
|
|
const dates = data.map(t => new Date(t.date));
|
|
const minDate = new Date(Math.min(...dates));
|
|
const maxDate = new Date(Math.max(...dates));
|
|
const dateRange = maxDate - minDate || 1; // Avoid division by zero
|
|
|
|
const ndvi = data.map(t => t.ndvi);
|
|
const minNdvi = Math.min(...ndvi);
|
|
const maxNdvi = Math.max(...ndvi);
|
|
const ndviRange = maxNdvi - minNdvi || 1; // Avoid division by zero
|
|
|
|
const x = (d) => pad + ((new Date(d) - minDate) / dateRange) * plotW;
|
|
const y = (v) => pad + plotH - ((v - minNdvi) / ndviRange) * plotH;
|
|
|
|
ctx.strokeStyle = "#ccc";
|
|
ctx.beginPath();
|
|
ctx.moveTo(pad, pad);
|
|
ctx.lineTo(pad, pad + plotH);
|
|
ctx.lineTo(pad + plotW, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
ctx.fillStyle = "#000";
|
|
ctx.font = "9px sans-serif";
|
|
ctx.fillText(minNdvi.toFixed(2), 2, pad + plotH + 10);
|
|
ctx.fillText(maxNdvi.toFixed(2), 2, pad + 3);
|
|
|
|
ctx.strokeStyle = "#0066ff";
|
|
ctx.beginPath();
|
|
let first = true;
|
|
for (const t of data) {
|
|
const px = x(t.date), py = y(t.ndvi);
|
|
if (first) { ctx.moveTo(px, py); first = false; }
|
|
else ctx.lineTo(px, py);
|
|
}
|
|
ctx.stroke();
|
|
ctx.fillStyle = "#888";
|
|
const axisY = pad + plotH;
|
|
for (const t of data) ctx.fillRect(x(t.date) - 1, axisY - 1, 2, 2);
|
|
|
|
const xPos = x(currentDate);
|
|
ctx.strokeStyle = "#f00";
|
|
ctx.lineWidth = 2;
|
|
ctx.beginPath();
|
|
ctx.moveTo(xPos, pad);
|
|
ctx.lineTo(xPos, pad + plotH);
|
|
ctx.stroke();
|
|
|
|
const validData = data.filter(t => !isNaN(new Date(t.date).getTime()));
|
|
if (validData.length === 0) continue;
|
|
const closest = validData.reduce((c, t) =>
|
|
Math.abs(new Date(t.date) - new Date(currentDate)) < Math.abs(new Date(c.date) - new Date(currentDate)) ? t : c
|
|
);
|
|
if (closest && closest.ndvi !== null) {
|
|
const yPos = y(closest.ndvi);
|
|
ctx.fillStyle = "#f00";
|
|
ctx.font = "bold 10px sans-serif";
|
|
ctx.fillText(closest.ndvi.toFixed(3), xPos + 5, yPos - 5);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function findFile(dateStr, source) {
|
|
const target = new Date(dateStr);
|
|
// Search outward from target date (0, ±1, ±2, ±3, ...) until we find the closest file
|
|
// Check dates in order: exact, then -1, +1, then -2, +2, etc.
|
|
// Limit to ±365 days to avoid infinite search
|
|
for (let offset = 0; offset <= 365; offset++) {
|
|
// Check exact date first (offset=0)
|
|
if (offset === 0) {
|
|
const date = target.toISOString().split("T")[0].replace(/-/g, "");
|
|
const filename = `${date}_0.geotiff`;
|
|
try {
|
|
const res = await fetch(`../data/innsbruck/2024/processed/${source}/${filename}`, { method: 'HEAD' });
|
|
if (res.ok) return filename;
|
|
} catch {}
|
|
} else {
|
|
// Check -offset and +offset days
|
|
for (const dir of [-1, 1]) {
|
|
const d = new Date(target);
|
|
d.setDate(d.getDate() + offset * dir);
|
|
const date = d.toISOString().split("T")[0].replace(/-/g, "");
|
|
const filename = `${date}_0.geotiff`;
|
|
try {
|
|
const res = await fetch(`../data/innsbruck/2024/processed/${source}/${filename}`, { method: 'HEAD' });
|
|
if (res.ok) return filename;
|
|
} catch {}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function transformBounds(bbox, fromCRS) {
|
|
const [sw, ne] = [proj4(fromCRS, "EPSG:4326", [bbox[0], bbox[1]]), proj4(fromCRS, "EPSG:4326", [bbox[2], bbox[3]])];
|
|
return [[sw[1], sw[0]], [ne[1], ne[0]]];
|
|
}
|
|
|
|
async function loadGeotiff(source, filename) {
|
|
const path = `../data/innsbruck/2024/processed/${source}/${filename}`;
|
|
const tiff = await GeoTIFF.fromArrayBuffer(await (await fetch(path)).arrayBuffer());
|
|
const image = await tiff.getImage();
|
|
const rasters = await image.readRasters();
|
|
const width = image.getWidth();
|
|
const height = image.getHeight();
|
|
const bbox = image.getBoundingBox();
|
|
const geoKeys = image.getGeoKeys();
|
|
const crsCode = geoKeys.ProjectedCSTypeGeoKey ? `EPSG:${geoKeys.ProjectedCSTypeGeoKey}` :
|
|
geoKeys.GeographicTypeGeoKey !== 4326 ? `EPSG:${geoKeys.GeographicTypeGeoKey}` : "EPSG:4326";
|
|
|
|
const bandIndices = [0, 1, 2];
|
|
const [blue, green, red] = bandIndices.map(i => Array.from(rasters[i]));
|
|
|
|
const normalize = (arr) => {
|
|
let min = Infinity, max = -Infinity;
|
|
for (const v of arr) if (!isNaN(v) && v > 0) { min = Math.min(min, v); max = Math.max(max, v); }
|
|
return arr.map(v => Math.max(0, Math.min(255, ((v - min) / (max - min || 1)) * 255)));
|
|
};
|
|
|
|
const [rNorm, gNorm, bNorm] = [red, green, blue].map(normalize);
|
|
|
|
const canvas = Object.assign(document.createElement("canvas"), { width, height });
|
|
const ctx = canvas.getContext("2d");
|
|
ctx.imageSmoothingEnabled = false;
|
|
const imgData = ctx.createImageData(width, height);
|
|
|
|
for (let i = 0; i < rNorm.length; i++) {
|
|
const idx = i * 4;
|
|
if (rNorm[i] === 0 && gNorm[i] === 0 && bNorm[i] === 0) {
|
|
imgData.data[idx + 3] = 0;
|
|
} else {
|
|
imgData.data[idx] = rNorm[i];
|
|
imgData.data[idx + 1] = gNorm[i];
|
|
imgData.data[idx + 2] = bNorm[i];
|
|
imgData.data[idx + 3] = 255;
|
|
}
|
|
}
|
|
ctx.putImageData(imgData, 0, 0);
|
|
|
|
const bounds = crsCode === "EPSG:4326" ? [[bbox[1], bbox[0]], [bbox[3], bbox[2]]] : transformBounds(bbox, crsCode);
|
|
if (overlays[source]) maps[source].removeLayer(overlays[source]);
|
|
overlays[source] = L.imageOverlay(canvas.toDataURL(), bounds, { opacity: 0.95 }).addTo(maps[source]);
|
|
maps[source].fitBounds(bounds);
|
|
|
|
// Extract date from filename to show the actual date of the file found (closest available date)
|
|
const dateStr = filename.split("_")[0];
|
|
const date = `${dateStr.slice(0,4)}-${dateStr.slice(4,6)}-${dateStr.slice(6,8)}`;
|
|
document.getElementById(`${source}rgbdate`).textContent = date;
|
|
}
|
|
|
|
const fmtDate = (d) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
|
const dateFromDays = (days) => fmtDate(new Date(start.getTime() + days * 86400000));
|
|
const daysFromDate = (dateStr) => {
|
|
const [y, m, d] = dateStr.split("-").map(Number);
|
|
return Math.floor((new Date(y, m - 1, d) - start) / 86400000);
|
|
};
|
|
|
|
async function loadPhenoCam(dateStr) {
|
|
const target = new Date(dateStr);
|
|
for (let offset = 0; offset < 15; offset++) {
|
|
for (const dir of [0, -1, 1]) {
|
|
const d = new Date(target);
|
|
d.setDate(d.getDate() + offset * dir);
|
|
const date = d.toISOString().split("T")[0].replace(/-/g, "");
|
|
const url = `../data/innsbruck/2024/raw/phenocam/${date}.jpg`;
|
|
try {
|
|
const res = await fetch(url, { method: 'HEAD' });
|
|
if (res.ok) {
|
|
document.getElementById("phenocamimage").src = url;
|
|
document.getElementById("phenocamdate").textContent = `${date.slice(0,4)}-${date.slice(4,6)}-${date.slice(6,8)}`;
|
|
return;
|
|
}
|
|
} catch {}
|
|
}
|
|
}
|
|
document.getElementById("phenocamimage").src = "";
|
|
document.getElementById("phenocamdate").textContent = "";
|
|
}
|
|
|
|
async function updateImages() {
|
|
const date = dateFromDays(parseInt(slider.value));
|
|
dateDisplay.textContent = date;
|
|
const params = new URLSearchParams();
|
|
params.set("date", date);
|
|
history.replaceState({}, "", `?${params}`);
|
|
drawTimeseries();
|
|
drawGreennessTimeseries();
|
|
drawPhenocamGreennessTimeseries();
|
|
drawCombinedGreennessTimeseries();
|
|
for (const source of ["s2", "fusion", "s3"]) {
|
|
const filename = await findFile(date, source);
|
|
if (filename) {
|
|
try {
|
|
await loadGeotiff(source, filename);
|
|
} catch (e) {
|
|
console.error(`Error loading ${source}:`, e);
|
|
}
|
|
}
|
|
}
|
|
await loadPhenoCam(date);
|
|
}
|
|
|
|
const urlParams = new URLSearchParams(location.search);
|
|
const urlDate = urlParams.get("date");
|
|
if (urlDate) slider.value = daysFromDate(urlDate);
|
|
slider.addEventListener("input", updateImages);
|
|
loadTimeseries().then(updateImages);
|
|
</script>
|
|
</body>
|
|
</html>
|