Bulletproof data loader: isolated layer updates and strict coordinate validation
Deploy TrassenPlaner / deploy (push) Waiting to run
Details
Deploy TrassenPlaner / deploy (push) Waiting to run
Details
This commit is contained in:
parent
0160def674
commit
d5edb1e992
115
index.html
115
index.html
|
|
@ -2761,74 +2761,95 @@
|
||||||
try {
|
try {
|
||||||
console.log("Starte Daten-Abruf vom Server...");
|
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) => {
|
const sanitize = (gj) => {
|
||||||
if (!gj || !gj.features) return { type: "FeatureCollection", features: [] };
|
if (!gj || !gj.features) return { type: "FeatureCollection", features: [] };
|
||||||
return {
|
return {
|
||||||
...gj,
|
...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
|
// 1. Eigentümer
|
||||||
const ownersRes = await fetch(`${API_BASE}/owners`);
|
try {
|
||||||
if (ownersRes.ok) {
|
const res = await fetch(`${API_BASE}/owners`);
|
||||||
let geojson = sanitize(await ownersRes.json());
|
if (res.ok) {
|
||||||
// Transformation falls die DB UTM-Koordinaten liefert (z.B. > 1000)
|
let gj = sanitize(await res.json());
|
||||||
if (geojson.features.length > 0) {
|
if (gj.features.length > 0) {
|
||||||
const firstCoord = geojson.features[0].geometry?.coordinates?.[0]?.[0]?.[0] || 0;
|
// Check for UTM transform
|
||||||
if (Math.abs(firstCoord) > 1000) {
|
const c = gj.features[0].geometry.coordinates;
|
||||||
console.log("Erkannte UTM-Koordinaten in DB, transformiere...");
|
const test = Array.isArray(c[0]) ? (Array.isArray(c[0][0]) ? c[0][0][0] : c[0][0]) : c[0];
|
||||||
geojson = transformGeoJSON(geojson, "EPSG:25832", "EPSG:4326");
|
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;
|
} catch (e) { console.error("Owners load error:", e); }
|
||||||
updateOwnerLayer();
|
|
||||||
|
|
||||||
if (layers.owners.getBounds().isValid()) {
|
|
||||||
map.fitBounds(layers.owners.getBounds());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Varianten
|
// 2. Varianten
|
||||||
const variantsRes = await fetch(`${API_BASE}/variants`);
|
try {
|
||||||
if (variantsRes.ok) {
|
const res = await fetch(`${API_BASE}/variants`);
|
||||||
const variants = await variantsRes.json();
|
if (res.ok) {
|
||||||
if (variants && variants.length > 0) {
|
const v = await res.json();
|
||||||
variants.forEach(sv => {
|
v.forEach(sv => {
|
||||||
const local = state.variants.find(lv => lv.id === sv.id);
|
const l = state.variants.find(lv => lv.id === sv.id);
|
||||||
if (local) {
|
if (l) { l.routes = sv.routes || []; l.name = sv.name || l.name; }
|
||||||
local.routes = sv.routes || [];
|
|
||||||
local.name = sv.name || local.name;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
updateRouteLayers();
|
updateRouteLayers();
|
||||||
}
|
}
|
||||||
}
|
} catch (e) { console.error("Variants load error:", e); }
|
||||||
|
|
||||||
// 3. Nutzungen
|
// 3. Nutzungen
|
||||||
const usageRes = await fetch(`${API_BASE}/usage`);
|
try {
|
||||||
if (usageRes.ok) {
|
const res = await fetch(`${API_BASE}/usage`);
|
||||||
state.usage = sanitize(await usageRes.json());
|
if (res.ok) {
|
||||||
updateUsageLayer();
|
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
|
// 4. WEA
|
||||||
const weaRes = await fetch(`${API_BASE}/wea`);
|
try {
|
||||||
if (weaRes.ok) {
|
const res = await fetch(`${API_BASE}/wea`);
|
||||||
state.wea = sanitize(await weaRes.json());
|
if (res.ok) {
|
||||||
updateWEALayer();
|
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
|
// 5. Infrastruktur
|
||||||
const infraRes = await fetch(`${API_BASE}/infrastructure`);
|
try {
|
||||||
if (infraRes.ok) {
|
const res = await fetch(`${API_BASE}/infrastructure`);
|
||||||
state.infrastructure = sanitize(await infraRes.json());
|
if (res.ok) {
|
||||||
updateInfrastructureLayer();
|
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) {
|
} catch (err) {
|
||||||
console.error("Fehler beim Laden der Server-Daten:", err);
|
console.error("Kritischer Fehler beim Laden der Datenbank:", err);
|
||||||
} finally {
|
} finally {
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
renderVariants();
|
renderVariants();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue