-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mapping.py
58 lines (45 loc) · 1.26 KB
/
create_mapping.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
import json
import csv
from collections import defaultdict
def add_name(entries):
entries.append({
"osm_key": "name",
"predicate": "osm:name"
})
def add_maxspeed(entries):
mapping = {}
with open('speeds.json') as f:
speeds = json.load(f)
for entry in speeds["data"]:
value = entry["value"]
try:
parsed_value = float(value)
mapping[value] = parsed_value
except:
if len(value.split('mph')) == 2:
parsed_value = int(round(float(value.split('mph')[0]) * 1.609344))
mapping[value] = parsed_value
entries.append({
"osm_key": "maxspeed",
"predicate": "osm:maxspeed",
"mapping": mapping
})
def read_entities():
entity_keys = defaultdict(dict)
with open('entities.csv') as csvfile:
next(csvfile)
spamreader = csv.reader(csvfile)
for row in spamreader:
key = row[0].split('=')[0]
entity_keys[key]["osm_key"] = key.strip()
entity_keys[key]["predicate"] = row[1].strip()
value = row[0].split('=')[1]
mapped_value = row[2].strip()
if 'mapping' not in entity_keys[key]:
entity_keys[key]["mapping"] = {}
entity_keys[key]["mapping"][value] = mapped_value
return list(entity_keys.values())
result = read_entities()
add_maxspeed(result)
add_name(result)
print(json.dumps(result, indent=2, sort_keys=True))