Add stable cascading cadastral search and fullscreen map UI
This commit is contained in:
1 parent
ed8120a7d0
commit
8997f1b728
8 files changed
+8804
-438
No files matched your search
+41
-133
@@ -18,14 +18,14 @@ const lmeState = {
|
||||
lastPosition: null,
|
||||
lastWfsLookupAt: 0,
|
||||
wfsLookupInFlight: false,
|
||||
duckDb: null,
|
||||
duckDbConnection: null
|
||||
fileImportBound: false,
|
||||
resizeBound: false
|
||||
};
|
||||
|
||||
window.lmeMap = {
|
||||
init,
|
||||
search,
|
||||
locateParcelArea,
|
||||
locateParcelByPoint,
|
||||
exportCadastreView,
|
||||
fitImported,
|
||||
clearImported,
|
||||
@@ -41,7 +41,9 @@ window.lmeMap = {
|
||||
centerOnGps,
|
||||
toggleTrail,
|
||||
setNativeGpsPosition,
|
||||
setGpsError
|
||||
setGpsError,
|
||||
resize,
|
||||
showInfo: setInfo
|
||||
};
|
||||
|
||||
function init() {
|
||||
@@ -74,6 +76,12 @@ function init() {
|
||||
refreshCadastreLayer();
|
||||
bindFileImport();
|
||||
|
||||
if (!lmeState.resizeBound) {
|
||||
window.addEventListener("resize", resize);
|
||||
window.addEventListener("orientationchange", resize);
|
||||
lmeState.resizeBound = true;
|
||||
}
|
||||
|
||||
lmeState.map.on("click", async event => {
|
||||
const info = await queryFeatureInfo(event.latlng);
|
||||
if (info) {
|
||||
@@ -112,36 +120,30 @@ async function search(text) {
|
||||
setInfo(place.display_name);
|
||||
}
|
||||
|
||||
async function locateParcelArea(region, municipality, municipalityCode, sheet, parcel, label) {
|
||||
if (!sheet || !parcel || (!municipality && !municipalityCode)) {
|
||||
setInfo("Inserisci Comune o codice catastale, Foglio e Particella.");
|
||||
async function locateParcelByPoint(latitude, longitude, parcel, label) {
|
||||
const lat = Number(latitude);
|
||||
const lon = Number(longitude);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon)) {
|
||||
setInfo("Coordinate della particella non valide.");
|
||||
return;
|
||||
}
|
||||
|
||||
setInfo("Cerco la particella nell'indice catastale...");
|
||||
setInfo(`Recupero il confine ufficiale: ${label}`);
|
||||
try {
|
||||
const match = await findParcelIndexPoint(region, municipality, municipalityCode, sheet, parcel);
|
||||
if (!match) {
|
||||
const query = [municipality, region, "Italia"].filter(Boolean).join(", ");
|
||||
await search(query);
|
||||
setInfo("Particella non trovata nell'indice. Ho aperto il Comune: usa tap/GPS per interrogare AdE.");
|
||||
return;
|
||||
}
|
||||
|
||||
const meters = 8;
|
||||
const deltaLat = meters / 111320;
|
||||
const deltaLon = meters / (111320 * Math.cos(match.lat * Math.PI / 180));
|
||||
const geojson = await fetchOfficialParcelsByBbox(match.lat - deltaLat, match.lon - deltaLon, match.lat + deltaLat, match.lon + deltaLon);
|
||||
const deltaLon = meters / (111320 * Math.cos(lat * Math.PI / 180));
|
||||
const geojson = await fetchOfficialParcelsByBbox(lat - deltaLat, lon - deltaLon, lat + deltaLat, lon + deltaLon);
|
||||
const normalizedParcel = normalizeParcel(parcel);
|
||||
const feature = (geojson.features || []).find(item => {
|
||||
const props = item.properties || {};
|
||||
return String(props.LABEL || "").trim().toUpperCase() === normalizedParcel.toUpperCase()
|
||||
|| String(props.NATIONALCADASTRALREFERENCE || "").toUpperCase().endsWith(`.${normalizedParcel.toUpperCase()}`);
|
||||
}) || (geojson.features || []).find(item => geometryContainsPoint(item.geometry, [match.lon, match.lat])) || geojson.features?.[0];
|
||||
}) || (geojson.features || []).find(item => geometryContainsPoint(item.geometry, [lon, lat])) || geojson.features?.[0];
|
||||
|
||||
if (!feature) {
|
||||
lmeState.map.setView([match.lat, match.lon], 20);
|
||||
setInfo("Punto interno trovato, ma il WFS AdE non ha restituito il poligono in quel momento.");
|
||||
lmeState.map.setView([lat, lon], 20);
|
||||
setInfo("Posizione trovata, ma il servizio WFS dell'Agenzia non ha restituito il confine. Riprova tra poco.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -151,125 +153,25 @@ async function locateParcelArea(region, municipality, municipalityCode, sheet, p
|
||||
}).addTo(lmeState.currentParcelLayer);
|
||||
fitLayer(layer);
|
||||
|
||||
const ref = feature.properties?.NATIONALCADASTRALREFERENCE || feature.properties?.INSPIREID_LOCALID || `${match.comune}_${match.foglio}.${match.particella}`;
|
||||
const ref = feature.properties?.NATIONALCADASTRALREFERENCE || feature.properties?.INSPIREID_LOCALID || label;
|
||||
setText("parcelState", `Particella selezionata: ${ref}`);
|
||||
setInfo(`Trovata ${ref}. Ora puoi attivare il GPS e camminare dentro il confine evidenziato.`);
|
||||
} catch (error) {
|
||||
setInfo(`Ricerca catastale non completata: ${error.message}`);
|
||||
setInfo(`Servizio catastale temporaneamente non disponibile: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function findParcelIndexPoint(region, municipality, municipalityCode, sheet, parcel) {
|
||||
const conn = await getDuckDbConnection();
|
||||
const code = (municipalityCode || "").trim().toUpperCase();
|
||||
const name = (municipality || "").trim().toUpperCase().replaceAll("'", "''");
|
||||
const normalizedSheet = normalizeSheet(sheet);
|
||||
const normalizedParcel = normalizeParcel(parcel).replaceAll("'", "''");
|
||||
|
||||
let indexWhere = "";
|
||||
if (code) {
|
||||
indexWhere = `comune = '${code.replaceAll("'", "''")}'`;
|
||||
} else {
|
||||
indexWhere = `upper(DENOMINAZIONE_IT) = '${name}'`;
|
||||
}
|
||||
|
||||
const indexUrl = "https://raw.githubusercontent.com/ondata/dati_catastali/main/S_0000_ITALIA/anagrafica/index.parquet";
|
||||
const indexRows = await queryDuckDb(conn, `
|
||||
SELECT comune, file, DENOMINAZIONE_IT
|
||||
FROM '${indexUrl}'
|
||||
WHERE ${indexWhere}
|
||||
LIMIT 5
|
||||
`);
|
||||
|
||||
if (!indexRows.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const row = chooseMunicipalityRow(indexRows, region);
|
||||
const dataUrl = `https://raw.githubusercontent.com/ondata/dati_catastali/main/S_0000_ITALIA/anagrafica/${row.file}`;
|
||||
const parcelRows = await queryDuckDb(conn, `
|
||||
SELECT INSPIREID_LOCALID, comune, foglio, particella, x, y
|
||||
FROM '${dataUrl}'
|
||||
WHERE comune = '${row.comune}'
|
||||
AND foglio = '${normalizedSheet}'
|
||||
AND upper(particella) = upper('${normalizedParcel}')
|
||||
LIMIT 1
|
||||
`);
|
||||
|
||||
if (!parcelRows.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parcelRow = parcelRows[0];
|
||||
return {
|
||||
inspireId: parcelRow.INSPIREID_LOCALID,
|
||||
comune: parcelRow.comune,
|
||||
foglio: parcelRow.foglio,
|
||||
particella: parcelRow.particella,
|
||||
lon: Number(parcelRow.x) / 1000000,
|
||||
lat: Number(parcelRow.y) / 1000000
|
||||
};
|
||||
}
|
||||
|
||||
function chooseMunicipalityRow(rows, region) {
|
||||
const wanted = (region || "").trim().toUpperCase();
|
||||
if (!wanted) {
|
||||
return rows[0];
|
||||
}
|
||||
return rows.find(row => String(row.file || "").toUpperCase().includes(wanted)) || rows[0];
|
||||
}
|
||||
|
||||
async function getDuckDbConnection() {
|
||||
if (lmeState.duckDbConnection) {
|
||||
return lmeState.duckDbConnection;
|
||||
}
|
||||
|
||||
const duckdb = await import("https://cdn.jsdelivr.net/npm/@duckdb/duckdb-wasm/+esm");
|
||||
const bundles = duckdb.getJsDelivrBundles();
|
||||
const bundle = await duckdb.selectBundle(bundles);
|
||||
const workerUrl = URL.createObjectURL(new Blob([`importScripts("${bundle.mainWorker}");`], { type: "text/javascript" }));
|
||||
const worker = new Worker(workerUrl);
|
||||
const logger = new duckdb.ConsoleLogger();
|
||||
const db = new duckdb.AsyncDuckDB(logger, worker);
|
||||
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);
|
||||
URL.revokeObjectURL(workerUrl);
|
||||
const conn = await db.connect();
|
||||
|
||||
try {
|
||||
await conn.query("INSTALL httpfs; LOAD httpfs;");
|
||||
} catch {
|
||||
// Some DuckDB-WASM bundles already include HTTP support.
|
||||
}
|
||||
|
||||
lmeState.duckDb = db;
|
||||
lmeState.duckDbConnection = conn;
|
||||
return conn;
|
||||
}
|
||||
|
||||
async function queryDuckDb(conn, sql) {
|
||||
const table = await conn.query(sql);
|
||||
return table.toArray().map(row => {
|
||||
if (typeof row.toJSON === "function") {
|
||||
return row.toJSON();
|
||||
}
|
||||
|
||||
const output = {};
|
||||
for (const key of Object.keys(row)) {
|
||||
output[key] = row[key];
|
||||
}
|
||||
return output;
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeSheet(sheet) {
|
||||
const value = String(sheet || "").trim().toUpperCase();
|
||||
return /^\d+$/.test(value) ? value.padStart(4, "0") : value;
|
||||
}
|
||||
|
||||
function normalizeParcel(parcel) {
|
||||
return String(parcel || "").trim().toUpperCase();
|
||||
}
|
||||
|
||||
function resize() {
|
||||
if (!lmeState.map) {
|
||||
return;
|
||||
}
|
||||
window.setTimeout(() => lmeState.map.invalidateSize({ pan: false }), 50);
|
||||
}
|
||||
|
||||
function exportCadastreView() {
|
||||
const center = lmeState.map.getCenter();
|
||||
const zoom = lmeState.map.getZoom();
|
||||
@@ -280,12 +182,15 @@ function exportCadastreView() {
|
||||
}
|
||||
|
||||
function bindFileImport() {
|
||||
const input = document.getElementById("gisFileInput");
|
||||
if (!input) {
|
||||
if (lmeState.fileImportBound) {
|
||||
return;
|
||||
}
|
||||
|
||||
input.addEventListener("change", async event => {
|
||||
document.addEventListener("change", async event => {
|
||||
if (event.target?.id !== "gisFileInput") {
|
||||
return;
|
||||
}
|
||||
|
||||
const file = event.target.files?.[0];
|
||||
if (!file) {
|
||||
return;
|
||||
@@ -308,6 +213,7 @@ function bindFileImport() {
|
||||
setInfo(`File non leggibile: ${error.message}`);
|
||||
}
|
||||
});
|
||||
lmeState.fileImportBound = true;
|
||||
}
|
||||
|
||||
async function readGisFile(file) {
|
||||
@@ -596,6 +502,8 @@ async function updateOfficialParcelFromPoint(latitude, longitude, accuracy) {
|
||||
|
||||
const ref = feature.properties?.NATIONALCADASTRALREFERENCE || feature.properties?.INSPIREID_LOCALID || feature.properties?.LABEL || "particella AdE";
|
||||
setText("parcelState", `Dentro particella AdE: ${ref}`);
|
||||
} catch (error) {
|
||||
setInfo(`Aggiornamento particella GPS non disponibile: ${error.message}`);
|
||||
} finally {
|
||||
lmeState.wfsLookupInFlight = false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user