Skip to content

Instantly share code, notes, and snippets.

@pigreco
Last active March 17, 2024 14:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pigreco/86589dddf5a59b3a7650267d5af237bd to your computer and use it in GitHub Desktop.
Save pigreco/86589dddf5a59b3a7650267d5af237bd to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
/***************************************************************************
WMS Catasto Agenzia delle Entrate CC BY 4.0
-------------------
copyright : (C) 2020 by Giulio Fattori
email : xxxxx.xxxxxxx@xxxxx.it
***************************************************************************/
"""
from qgis.core import *
from qgis.gui import *
import requests
@qgsfunction(args='auto', group='Custom')
def get_parcel_info2(xx, yy, EPSG, feature, parent):
"""
<h1>WMS Catasto Agenzia delle Entrate CC BY 4.0:</h1><br>
La funzione, tramite una richiesta GetFeatureInfo, restituisce le informazioni utili sulla particella che ricade sotto il pixel di mio interesse:
<h2>Esempio:</h2>
<ul>
<li>get_parcel_info(355461.5,4222490.7,'EPSG:3045') -> 'IT.AGE.PLA.G273_0033A0.673'</li>
<li>get_parcel_info("fieldX", "fieldY",'EPSG:3045') -> 'IT.AGE.PLA.G273_0033A0.673'</li>
<li>get_parcel_info("fieldX", "fieldY",@project_crs) -> 'IT.AGE.PLA.G273_0033A0.673'</li>
</ul>
<h2>NB: le coordinate X e Y devono essere espresse nel EPSG utilizzato (gli EPSG disponibili sono:6706,4258,25832/3/4,3044/5/6)</h2>
"""
req = "https://wms.cartografia.agenziaentrate.gov.it/inspire/wms/ows01.php?REQUEST=GetFeatureInfo&SERVICE=WMS&SRS="+EPSG+"&STYLES=&VERSION=1.1&FORMAT=image/png&BBOX="+str(xx-1)+","+str(yy-1)+","+str(xx+1)+","+str(yy+1)+"&HEIGHT=9&WIDTH=9&LAYERS=CP.CadastralParcel&QUERY_LAYERS=CP.CadastralParcel&INFO_FORMAT=text/html&X=5&Y=5"
r = requests.get(req, auth=('user', 'pass'))
a = r.text.partition("InspireId localId</th><td>")[2]
b = a.partition("</td>")[0]
return b
@pigreco
Copy link
Author

pigreco commented Jun 28, 2021

@antatta

Si potrebbe usare un'unica regex fatta così:

codice: regexp_matches( "catasto" ,'^(.+).(.+).(.+).(.{4})(.)(.+).(.+)')[3]
sezione: regexp_matches( "catasto" ,'^(.+).(.+).(.+).(.{4})(.)(.+).(.+)')[4]
foglio: regexp_matches( "catasto" ,'^(.+).(.+).(.+).(.{4})(.)(.+).(.+)')[5]
particella: regexp_matches( "catasto" ,'^(.+).(.+).(.+).(.{4})(.)(.+).(.+)')[6]

quasi, suggerirei così:

regexp_matches( "catasto" ,'^(.+)\\.(.+)\\.(.+)\\.(.+)_(.+)\\.(.+)$')[x]

con x che varia da 0 a 5

image

@antatta
Copy link

antatta commented Jun 30, 2021

mmmm direi che si può fare ancora meglio con:

regexp_matches( "catasto" ,'^(.+)\\.(.+)\\.(.+)\\.(.{4})(.)(.+)\\.(.+)')[x]

con x che varia da 0 a 6 e così si prende anche la sezione (se esiste, altrimenti '_')

image
image

PS: ti stimo, fai un ottimo lavoro di divulgazione

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment