Bulletproof data loader: isolated layer updates and strict coordinate validation
Deploy TrassenPlaner / deploy (push) Waiting to run Details

This commit is contained in:
Johannes Baumeister 2026-04-16 01:49:11 +02:00
parent 0160def674
commit d5edb1e992
1 changed files with 68 additions and 47 deletions

View File

@ -2761,74 +2761,95 @@
try {
console.log("Starte Daten-Abruf vom Server...");
const isVal = (c) => Array.isArray(c) && c.length >= 2 && !isNaN(c[0]) && !isNaN(c[1]) && c[0] !== null && c[1] !== null;
const sanitize = (gj) => {
if (!gj || !gj.features) return { type: "FeatureCollection", features: [] };
return {
...gj,
features: gj.features.filter(f => f && f.geometry && f.geometry.coordinates)
features: gj.features.filter(f => {
if (!f || !f.geometry || !f.geometry.coordinates) return false;
// Recursive coordinate check for simple structures
if (f.geometry.type === 'Point') return isVal(f.geometry.coordinates);
return true; // Polygons are harder to check deeply, but usually safer
})
};
};
// 1. Eigentümer / Flurstücke
const ownersRes = await fetch(`${API_BASE}/owners`);
if (ownersRes.ok) {
let geojson = sanitize(await ownersRes.json());
// Transformation falls die DB UTM-Koordinaten liefert (z.B. > 1000)
if (geojson.features.length > 0) {
const firstCoord = geojson.features[0].geometry?.coordinates?.[0]?.[0]?.[0] || 0;
if (Math.abs(firstCoord) > 1000) {
console.log("Erkannte UTM-Koordinaten in DB, transformiere...");
geojson = transformGeoJSON(geojson, "EPSG:25832", "EPSG:4326");
// 1. Eigentümer
try {
const res = await fetch(`${API_BASE}/owners`);
if (res.ok) {
let gj = sanitize(await res.json());
if (gj.features.length > 0) {
// Check for UTM transform
const c = gj.features[0].geometry.coordinates;
const test = Array.isArray(c[0]) ? (Array.isArray(c[0][0]) ? c[0][0][0] : c[0][0]) : c[0];
if (Math.abs(test) > 1000) gj = transformGeoJSON(gj, "EPSG:25832", "EPSG:4326");
}
state.owners = gj;
updateOwnerLayer();
if (layers.owners.getBounds().isValid()) map.fitBounds(layers.owners.getBounds());
}
state.owners = geojson;
updateOwnerLayer();
if (layers.owners.getBounds().isValid()) {
map.fitBounds(layers.owners.getBounds());
}
}
} catch (e) { console.error("Owners load error:", e); }
// 2. Varianten
const variantsRes = await fetch(`${API_BASE}/variants`);
if (variantsRes.ok) {
const variants = await variantsRes.json();
if (variants && variants.length > 0) {
variants.forEach(sv => {
const local = state.variants.find(lv => lv.id === sv.id);
if (local) {
local.routes = sv.routes || [];
local.name = sv.name || local.name;
}
try {
const res = await fetch(`${API_BASE}/variants`);
if (res.ok) {
const v = await res.json();
v.forEach(sv => {
const l = state.variants.find(lv => lv.id === sv.id);
if (l) { l.routes = sv.routes || []; l.name = sv.name || l.name; }
});
updateRouteLayers();
}
}
} catch (e) { console.error("Variants load error:", e); }
// 3. Nutzungen
const usageRes = await fetch(`${API_BASE}/usage`);
if (usageRes.ok) {
state.usage = sanitize(await usageRes.json());
updateUsageLayer();
}
try {
const res = await fetch(`${API_BASE}/usage`);
if (res.ok) {
let gj = sanitize(await res.json());
// Transform usage if UTM
if (gj.features.length > 0) {
const c = gj.features[0].geometry.coordinates;
const test = Array.isArray(c[0]) ? (Array.isArray(c[0][0]) ? c[0][0][0] : c[0][0]) : c[0];
if (Math.abs(test) > 1000) gj = transformGeoJSON(gj, "EPSG:25832", "EPSG:4326");
}
state.usage = gj;
updateUsageLayer();
}
} catch (e) { console.error("Usage load error:", e); }
// 4. WEA
const weaRes = await fetch(`${API_BASE}/wea`);
if (weaRes.ok) {
state.wea = sanitize(await weaRes.json());
updateWEALayer();
}
try {
const res = await fetch(`${API_BASE}/wea`);
if (res.ok) {
let gj = sanitize(await res.json());
if (gj.features.length > 0) {
if (Math.abs(gj.features[0].geometry.coordinates[0]) > 1000) gj = transformGeoJSON(gj, "EPSG:25832", "EPSG:4326");
}
state.wea = gj;
updateWEALayer();
}
} catch (e) { console.error("WEA load error:", e); }
// 5. Infratruktur
const infraRes = await fetch(`${API_BASE}/infrastructure`);
if (infraRes.ok) {
state.infrastructure = sanitize(await infraRes.json());
updateInfrastructureLayer();
}
// 5. Infrastruktur
try {
const res = await fetch(`${API_BASE}/infrastructure`);
if (res.ok) {
let gj = sanitize(await res.json());
if (gj.features.length > 0) {
if (Math.abs(gj.features[0].geometry.coordinates[0]) > 1000) gj = transformGeoJSON(gj, "EPSG:25832", "EPSG:4326");
}
state.infrastructure = gj;
updateInfrastructureLayer();
}
} catch (e) { console.error("Infra load error:", e); }
console.log("Daten erfolgreich vom Server geladen.");
console.log("Daten-Ladevorgang abgeschlossen.");
} catch (err) {
console.error("Fehler beim Laden der Server-Daten:", err);
console.error("Kritischer Fehler beim Laden der Datenbank:", err);
} finally {
isLoading = false;
renderVariants();