-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogham.py
70 lines (58 loc) · 2.3 KB
/
ogham.py
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
68
69
70
# python [path]get-pip.py (https://www.liquidweb.com/kb/install-pip-windows/)
# pip install sparqlwrapper
# pip install geojson
# pip install geomet https://github.com/geomet/geomet
from SPARQLWrapper import SPARQLWrapper, JSON
import geojson
from geomet import wkt
import json
endpoint_url = "https://query.wikidata.org/sparql"
query = """SELECT ?label ?geo ?item WHERE {
?item wdt:P31 wd:Q2016147;
wdt:P361 wd:Q67978809;
wdt:P195 ?collection.
OPTIONAL { ?item wdt:P625 ?geo. }
OPTIONAL {
?item rdfs:label ?label.
FILTER((LANG(?label)) = "en")
}
OPTIONAL {
?collection rdfs:label ?collectionLabel.
FILTER((LANG(?collectionLabel)) = "en")
}
}
ORDER BY (?label)"""
def get_results(endpoint_url, query):
sparql = SPARQLWrapper(endpoint_url, agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11")
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
results = get_results(endpoint_url, query)
# geojson stuff
features = []
'''
for result in results["results"]["bindings"]:
#print(result)
#print(result["label"]["value"])
#print(result["geo"]["value"])
#print(result["item"]["value"])
#labels.append(result["label"]["value"])
#geoms.append(result["geo"]["value"].replace("Point", "POINT"))
#geomsgj.append(wkt.loads(result["geo"]["value"].replace("Point", "POINT")))
#items.append(result["item"]["value"])
feature = { 'type': 'Feature', 'properties': { 'label': result["label"]["value"], 'item': result["item"]["value"] }, 'geometry': wkt.loads(result["geo"]["value"].replace("Point", "POINT")) }
features.append(feature)
geojson = {'type': 'FeatureCollection', 'features': features }
'''
for result in results["results"]["bindings"]:
properties = {}
for var in results["head"]["vars"]:
properties[var] = result[var]["value"]
print(properties)
if "Point" in result["geo"]["value"]:
feature = { 'type': 'Feature', 'properties': properties, 'geometry': wkt.loads(result["geo"]["value"].replace("Point", "POINT")) }
features.append(feature)
geojson = {'type': 'FeatureCollection', 'features': features }
print(json.dumps(geojson, sort_keys=True, indent=4))
with open('D:/tmp/data.geojson', 'w') as f:
json.dump(geojson, f)