Skip to content

Instantly share code, notes, and snippets.

@valerio-bozzolan
Last active November 6, 2018 18:20
Show Gist options
  • Save valerio-bozzolan/b12b0625619f257b58dabefa1474b7f4 to your computer and use it in GitHub Desktop.
Save valerio-bozzolan/b12b0625619f257b58dabefa1474b7f4 to your computer and use it in GitHub Desktop.
Elenco dei comuni italiani da Wikidata
# http://tinyurl.com/y8xefcy9
#
# Comuni d'Italia con coordinate, codice ISTAT e codice Wikidata
# Author: Valerio Bozzolan
# Date: 6 novembre 2018
# Query license: public domain
# Original query: https://gist.github.com/valerio-bozzolan/b12b0625619f257b58dabefa1474b7f4
#defaultView:Map
SELECT
( SAMPLE( ?regioneLabel ) AS ?regioneLabel )
( SAMPLE( ?provinciaLabel ) AS ?provinciaLabel )
( SAMPLE( ?comuneLabel ) AS ?comuneLabel )
( SAMPLE( ?position ) AS ?position )
( SAMPLE( ?regione ) AS ?regione )
( SAMPLE( ?provincia ) AS ?provincia )
?comune
( SAMPLE( ?regioneIstat ) AS ?regioneIstat )
( SAMPLE( ?provinciaIstat ) AS ?provinciaIstat )
( SAMPLE( ?comuneIstat ) AS ?comuneIstat )
WHERE
{
{
# i comuni italiani
?comune wdt:P31 wd:Q747074.
} UNION {
# ed in comuni "sparsi" (WTF? asd)
?comune wdt:P31 wd:Q954172.
}
# solo i comuni con codice ISTAT e posizione
?comune wdt:P635 ?comuneIstat;
wdt:P625 ?position.
# non voglio i comuni defunti
MINUS { ?comune pq:P582 ?comuneAsd }
{
# il comune e' nell'unita' amministrativa provincia
?comune wdt:P131 ?provincia.
?provincia wdt:P635 ?provinciaIstat;
wdt:P31 wd:Q15089;
wdt:P131 ?regione.
# non voglio provincie defunte
MINUS { ?provincia pq:P582 ?provinciaAsd1 }
?provincia rdfs:label ?provinciaLabel.
FILTER( LANG( ?provinciaLabel ) = "it" ).
} UNION {
# oppure il comune e' nell'unita' amministrativa citta' metropolitana
?comune wdt:P131 ?provincia.
?provincia wdt:P635 ?provinciaIstat;
wdt:P31 wd:Q15110;
wdt:P131 ?regione;
# non voglio provincie defunte
MINUS { ?provincia pq:P582 ?provinciaAsd2 }
?provincia rdfs:label ?provinciaLabel.
FILTER( LANG( ?provinciaLabel ) = "it" ).
} UNION {
# oppure il comune e' direttamente nell'unita' amministrativa regione
?comune wdt:P131 ?regione
}
?regione wdt:P635 ?regioneIstat.
# regione / regione a statuto speciale
{
?regione wdt:P31 wd:Q16110.
} UNION {
?regione wdt:P31 wd:Q1710033.
}
# il SERVICE di Wikidata mi fa paura. asd
?comune rdfs:label ?comuneLabel.
?regione rdfs:label ?regioneLabel.
FILTER( LANG( ?comuneLabel ) = "it" ).
FILTER( LANG( ?regioneLabel ) = "it" ).
}
GROUP BY ?comune
ORDER BY ?regioneLabel ?provinciaLabel ?comuneLabel
@valerio-bozzolan
Copy link
Author

Grazie a @iamsabas su Telegram per aver sottolineato l'esistenza dei "comuni sparsi". asd

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