From 685bfb1f3bc308749888ba4d316dcb52d078e102 Mon Sep 17 00:00:00 2001 From: Johannes Baumeister Date: Thu, 25 Jun 2026 09:20:09 +0200 Subject: [PATCH] feat: filter ALKIS layers by project and auto-zoom to project boundaries at startup --- app.js | 17 +++++++++++++- server.js | 69 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 26 deletions(-) diff --git a/app.js b/app.js index e4c6835..f02787c 100644 --- a/app.js +++ b/app.js @@ -1218,7 +1218,8 @@ document.addEventListener('DOMContentLoaded', async () => { // ALKIS aus Datenbank IMMER laden console.log("Lade ALKIS-Layer aus Datenbank..."); - const alkisResp = await fetch('/api/layers/alkis').catch(err => { + const projId = getProjektId(); + const alkisResp = await fetch(`/api/layers/alkis?project=${encodeURIComponent(projId)}`).catch(err => { console.error("Netzwerkfehler beim Laden des ALKIS-Layers:", err); return null; }); @@ -1228,6 +1229,20 @@ document.addEventListener('DOMContentLoaded', async () => { console.log(`ALKIS API: ${data.features ? data.features.length : 0} Features erhalten.`); await processALKISData(data, "Eigentümer (ALKIS DB)"); + // Auto-zoom to project parcels bounds if features exist + const alkisLayer = overlays["Eigentümer (ALKIS DB)"]; + if (alkisLayer && data.features && data.features.length > 0) { + try { + const bounds = alkisLayer.getBounds(); + if (bounds.isValid()) { + state.map.fitBounds(bounds); + console.log("Erfolgreich auf ALKIS-Flächen gezoomt."); + } + } catch (zoomErr) { + console.error("Fehler beim Zoomen auf ALKIS-Flächen:", zoomErr); + } + } + cleanupLocalOwnerLayers(); } else { const errorText = alkisResp ? await alkisResp.text() : "Server nicht erreichbar"; diff --git a/server.js b/server.js index 817a6e1..0532cc9 100644 --- a/server.js +++ b/server.js @@ -210,41 +210,60 @@ app.get('/api/sicherung/:projekt_id', async (req, res) => { // NEU: API zum Laden des ALKIS-Layers als GeoJSON app.get('/api/layers/alkis', async (req, res) => { - log("Lade ALKIS-Layer aus Datenbank..."); + const projectParam = req.query.project || req.query.projekt_id || req.query.projekt; + log(`Lade ALKIS-Layer aus Datenbank für Projekt: ${projectParam || 'alle'}`); + const client = await pool.connect(); try { - const result = await pool.query( - `SELECT jsonb_build_object( - 'type', 'FeatureCollection', - 'features', jsonb_agg(features.feature) - ) - FROM ( + let query = ` SELECT jsonb_build_object( 'type', 'Feature', - 'id', id, - 'geometry', ST_AsGeoJSON(ST_Transform(geom, 4326))::jsonb, + 'id', a.id, + 'geometry', ST_AsGeoJSON(ST_Transform(a.geom, 4326))::jsonb, 'properties', jsonb_build_object( - 'id', id, - 'vorname', "VNA", - 'nachname', "GNA", - 'FSK', "FSK", - 'PLZ', "PLZ", - 'ORP', "ORP", - 'STR', "STR", - 'HSN', "HSN", - 'FLN', "FLN", - 'ZAE', "FSN__ZAE", - 'NEN', "FSN__NEN", - 'status', status, - 'notiz', notiz + 'id', a.id, + 'vorname', a."VNA", + 'nachname', a."GNA", + 'FSK', a."FSK", + 'PLZ', a."PLZ", + 'ORP', a."ORP", + 'STR', a."STR", + 'HSN', a."HSN", + 'FLN', a."FLN", + 'ZAE', a."FSN__ZAE", + 'NEN', a."FSN__NEN", + 'status', a.status, + 'notiz', a.notiz ) ) AS feature - FROM geodaten.flaecheneigentuemer_alkis - ) features` - ); + FROM geodaten.flaecheneigentuemer_alkis a + `; + const params = []; + + if (projectParam) { + const projId = await resolveProjectId(client, projectParam); + if (projId) { + query += ` JOIN geodaten.flaecheneigentuemer_alkis_zuweisung z ON a."FSK" = z.fsk WHERE z.projekt_id = $1`; + params.push(projId); + } + } + + const sql = ` + SELECT jsonb_build_object( + 'type', 'FeatureCollection', + 'features', COALESCE(jsonb_agg(features.feature), '[]'::jsonb) + ) + FROM ( + ${query} + ) features + `; + + const result = await client.query(sql, params); res.json(result.rows[0].jsonb_build_object); } catch (err) { log(`FEHLER beim Laden des ALKIS-Layers: ${err.message}`); res.status(500).json({ error: err.message }); + } finally { + client.release(); } });