Skip to content

Instantly share code, notes, and snippets.

@aborruso
Last active September 18, 2019 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aborruso/b13c29fc322ca4ef23e6c79021d6f76c to your computer and use it in GitHub Desktop.
Save aborruso/b13c29fc322ca4ef23e6c79021d6f76c to your computer and use it in GitHub Desktop.
Query Wikidata per avere il codice ISTAT comunale a partire dal nome del comune

La query di base è

SELECT ?ISTAT
WHERE 
{
  ?item wdt:P31 wd:Q747074.
  ?item rdfs:label ?itemLabel. 
  FILTER(LCASE(?itemLabel)="nomecomuneinminuscolo"@it). 
  ?item wdt:P635 ?ISTAT .
}

Per Messina è

SELECT ?ISTAT
WHERE 
{
  ?item wdt:P31 wd:Q747074.
  ?item rdfs:label ?itemLabel. 
  FILTER(LCASE(?itemLabel)="messina"@it). 
  ?item wdt:P635 ?ISTAT .
}

Qui per vederla in azione in modalità visuale https://w.wiki/8Yu

Per avere l'output in JSON tramite una chiamata via URL:

https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query=SELECT ?ISTAT WHERE {?item wdt:P31 wd:Q747074. ?item rdfs:label ?itemLabel. FILTER(LCASE(?itemLabel)="messina"@it). ?item wdt:P635 ?ISTAT .}

A cui però c'è da applicare l'URL encoding:

https://query.wikidata.org/bigdata/namespace/wdq/sparql?format=json&query=SELECT%20?ISTAT%20WHERE%20{?item%20wdt:P31%20wd:Q747074.%20?item%20rdfs:label%20?itemLabel.%20FILTER(LCASE(?itemLabel)=%22messina%22@it).%20?item%20wdt:P635%20?ISTAT%20.}

Che in output da:

{
  "head": {
    "vars": [
      "ISTAT"
    ]
  },
  "results": {
    "bindings": [
      {
        "ISTAT": {
          "type": "literal",
          "value": "083048"
        }
      }
    ]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment