-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSPARQL_geoadmin_wikidata
67 lines (56 loc) · 1.97 KB
/
SPARQL_geoadmin_wikidata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from SPARQLWrapper import SPARQLWrapper, JSON
endpoint_url = "https://ld.geo.admin.ch/query"
query = """
PREFIX schema: <http://schema.org/>
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX st: <https://ld.geo.admin.ch/def/>
select ?Municipality ?Name ?Population ?bfs ?date ?wikidata_id
where{
?Municipality gn:featureCode gn:A.ADM3 .
?Municipality schema:name ?Name .
?Municipality gn:population?Population .
?Municipality st:bfsNumber ?bfs .
?Municipality dct:issued ?date .
?Municipality gn:parentADM1 ?InCanton .
?InCanton schema:name ?CantonName .
#?Municipality geo:hasGeometry ?Geometry .
#?Geometry geo:asWKT ?WKT .
#FILTER (?date = '2019-01-01'^^xsd:date)
FILTER (?CantonName = 'Zürich')
{SELECT DISTINCT (xsd:integer(?bfsWD) AS ?bfs)?wikidata_id ?Image WHERE {
SERVICE <https://query.wikidata.org/bigdata/namespace/wdq/sparql>
{
?wikidata_id wdt:P771 ?bfsWD .
?wikidata_id wdt:P31 wd:Q70208 .
#OPTIONAL { ?wikidata_id wdt:P18 ?Image. } .
}
}
}
}
"""
def get_results(endpoint_url, query):
sparql = SPARQLWrapper(endpoint_url, agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36")
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
result = results["results"]["bindings"]
population = []
for p in result:
mon = {
'Municipality':p['Municipality']['value'],
'Name':p['Name']['value'],
'Population':p['Population']['value'],
'bfs':p['bfs']['value'],
'date':p['date']['value'],
'wikidata_id':p['wikidata_id']['value'],
'wikidata_id': p['wikidata_id']['value'].replace('http://www.wikidata.org/entity/', '')#,
#'date': p['date']['value'],
}
population.append(mon)
pop = pd.DataFrame(population)
pop.head