Skip to content

Commit ffc3d57

Browse files
pigrecoclaude
andcommitted
Aggiorna versione a 1.4.2: funzione get_particella_info per calcolatore campi
Nuova funzione personalizzata get_particella_info($geometry) registrata nel gruppo 'Catasto' del calcolatore di campi. Interroga il WFS e restituisce un array con: riferimento catastale, foglio, label, codice comune, geometria WKT, sezione e allegato. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2b2a023 commit ffc3d57

3 files changed

Lines changed: 141 additions & 1 deletion

File tree

get_particella_wfs.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
/***************************************************************************
4+
WFS Catasto Agenzia delle Entrate CC BY 4.0
5+
-------------------
6+
copyright : (C) 2025 by Totò Fiandaca
7+
email : pigrecoinfinito@gmail.com
8+
***************************************************************************/
9+
"""
10+
11+
from qgis.core import *
12+
from qgis.utils import qgsfunction
13+
import urllib.request
14+
import urllib.parse
15+
from xml.etree import ElementTree as ET
16+
17+
def format_wkt(wkt, decimals=6):
18+
"""
19+
Formatta una stringa WKT con il numero specificato di decimali.
20+
"""
21+
import re
22+
23+
def format_number(match):
24+
num = float(match.group(0))
25+
return f"{num:.{decimals}f}"
26+
27+
formatted = re.sub(r'\d+\.\d+', format_number, wkt)
28+
formatted = formatted.replace(",", ", ")
29+
formatted = formatted.replace("), ", "),\n")
30+
31+
return formatted
32+
33+
@qgsfunction(args='auto', group='Catasto', usesgeometry=True)
34+
def get_particella_info(geom, feature, parent):
35+
"""
36+
<h1>Catasto Agenzia delle Entrate CC BY 4.0:</h1>
37+
La funzione restituisce le informazioni WFS Catasto disponibili nella particella sottostante.
38+
39+
<h2>Parametri</h2>
40+
<ul>
41+
<li>geometry: geometria del punto (viene passata automaticamente)</li>
42+
</ul>
43+
44+
<h2>Returns</h2>
45+
<ul>
46+
<li>ARRAY: informazioni della particella</li>
47+
</ul>
48+
49+
<h2>Esempio</h2>
50+
<pre>get_particella_info($geometry)[0]--> M011_0019C0.131</pre>
51+
<pre>get_particella_info($geometry)[1]--> 0019</pre>
52+
<pre>get_particella_info($geometry)[2]--> 131</pre>
53+
<pre>get_particella_info($geometry)[3]--> M011</pre>
54+
<pre>get_particella_info($geometry)[4]--> geometria WKT</pre>
55+
<pre>get_particella_info($geometry)[5]--> _ (sezione censuaria)</pre>
56+
<pre>get_particella_info($geometry)[6]--> C (allegato)</pre>
57+
"""
58+
try:
59+
# Verifica che la geometria sia un punto
60+
if geom.type() != QgsWkbTypes.PointGeometry:
61+
return ['ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR']
62+
63+
# Prendi le coordinate del punto
64+
point = geom.asPoint()
65+
x = point.x()
66+
y = point.y()
67+
68+
# Base URL con i parametri base che sappiamo funzionare
69+
uri = (f"pagingEnabled='true' "
70+
f"preferCoordinatesForWfsT11='false' "
71+
f"restrictToRequestBBOX='1' "
72+
f"srsname='EPSG:6706' "
73+
f"typename='CP:CadastralParcel' "
74+
f"url='https://wfs.cartografia.agenziaentrate.gov.it/inspire/wfs/owfs01.php' "
75+
f"version='2.0.0' "
76+
f"language='ita'")
77+
78+
# Crea un layer temporaneo per la richiesta
79+
layer = QgsVectorLayer(uri, "catasto_query", "WFS")
80+
81+
if not layer.isValid():
82+
return ['ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR']
83+
84+
# Crea il punto per il filtro spaziale
85+
point_geom = QgsGeometry.fromPointXY(QgsPointXY(x, y))
86+
request = QgsFeatureRequest().setFilterRect(point_geom.boundingBox())
87+
88+
# Recupera le features
89+
features = list(layer.getFeatures(request))
90+
91+
if features:
92+
# Prendi la prima particella trovata
93+
feat = features[0]
94+
ref = feat['NATIONALCADASTRALREFERENCE']
95+
admin = feat['ADMINISTRATIVEUNIT']
96+
label = feat['LABEL']
97+
foglio = ref[5:9] if len(ref) > 9 else 'N/D'
98+
99+
# Parsing NATIONALCADASTRALREFERENCE (formato CCCCZFFFFAS.particella)
100+
sezione = 'N/D'
101+
allegato = 'N/D'
102+
if ref and isinstance(ref, str):
103+
codice = ref.split(".")[0] # parte prima del punto
104+
if len(codice) == 11: # CCCCZFFFFAS = 11 caratteri
105+
sez = codice[4] # Z: sezione censuaria
106+
sezione = "" if sez == "_" else sez
107+
allegato = codice[9] # A: allegato
108+
109+
# Ottieni la geometria WKT e formattala
110+
geom_wkt = 'N/D'
111+
if feat.hasGeometry():
112+
geom_wkt = format_wkt(feat.geometry().asWkt())
113+
114+
# Restituisci la lista
115+
return [ref, foglio, label, admin, geom_wkt, sezione, allegato]
116+
else:
117+
return ['N/D', 'N/D', 'N/D', 'N/D', 'N/D', 'N/D', 'N/D']
118+
119+
except Exception as e:
120+
return ['ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR', 'ERROR']
121+
122+
# Esempio di utilizzo nel calcolatore di campi:
123+
# get_particella_info($geometry)[0] # per il riferimento catastale
124+
# get_particella_info($geometry)[4] # per la geometria WKT
125+
# get_particella_info($geometry)[5] # per la sezione censuaria
126+
# get_particella_info($geometry)[6] # per l'allegato

metadata.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
name=WFS Catasto Download Particelle BBox
77
qgisMinimumVersion=3.0
88
description=Download particelle catastali dal WFS dell'Agenzia delle Entrate tramite BBox, selezione poligono, linea con buffer o layer di punti.
9-
version=1.4.1
9+
version=1.4.2
1010
author=Salvatore Fiandaca
1111
email=pigrecoinfinito@gmail.com
1212

@@ -30,6 +30,10 @@ repository=https://github.com/pigreco/wfs_catasto_download_particelle_bbox
3030

3131
hasProcessingProvider=no
3232
changelog=
33+
1.4.2 - Nuova funzione per il calcolatore di campi: get_particella_info($geometry)
34+
nel gruppo 'Catasto'. Restituisce un array con riferimento catastale,
35+
foglio, label, codice comune, geometria WKT, sezione e allegato
36+
dalla particella WFS sottostante al punto.
3337
1.4.1 - Modalita' Seleziona Linea: possibilita' di disegnare una polilinea
3438
direttamente sulla mappa (click sinistro per aggiungere vertici,
3539
click destro per terminare, ESC per annullare).

wfs_catasto_download_particelle_bbox_p.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
QgsGeometry,
3131
QgsFeature,
3232
QgsField,
33+
QgsExpression,
3334
)
3435
from qgis.gui import QgsMapTool, QgsRubberBand
3536
from qgis.PyQt.QtCore import Qt, QVariant, QTimer
@@ -44,6 +45,7 @@
4445
from qgis.utils import iface as qgis_iface
4546

4647
from .wfs_catasto_download_particelle_bbox_d import AvvisoDialog, SceltaModalitaDialog
48+
from .get_particella_wfs import get_particella_info
4749

4850

4951
# =============================================================================
@@ -1720,8 +1722,16 @@ def initGui(self):
17201722
self.iface.addPluginToVectorMenu(self.menu, action)
17211723
self.actions.append(action)
17221724

1725+
# Registra la funzione personalizzata nel calcolatore di campi
1726+
QgsExpression.registerFunction(get_particella_info)
1727+
print("[OK] Funzione personalizzata 'get_particella_info' registrata")
1728+
17231729
def unload(self):
17241730
"""Rimuove azione dalla toolbar e dal menu."""
1731+
# Deregistra la funzione personalizzata
1732+
QgsExpression.unregisterFunction('get_particella_info')
1733+
print("[OK] Funzione personalizzata 'get_particella_info' deregistrata")
1734+
17251735
for action in self.actions:
17261736
self.iface.removePluginVectorMenu(self.menu, action)
17271737
self.iface.removeToolBarIcon(action)

0 commit comments

Comments
 (0)