-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcorrect-names.py
executable file
·115 lines (93 loc) · 3.85 KB
/
correct-names.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
"""
Unfortunately some of the area names are not converted
correctly in our toolchain of gdal | iconv (| topojson).
This script tries to straighten that out.
"""
import csv
import glob
import json
import re
from os.path import basename
csv_files = glob.glob('LOR-Schluesselsystematik/*.csv')
multiple_spaces = re.compile(r'\s+')
only_uppercase = re.compile(r'^[A-Z\s\d]+$')
def clean_name(name):
"""
Clean names that have too much spacing, inconsistent whitespace
around slashes or inconsistent capitalization
"""
cleaned = multiple_spaces.sub(' ', name)
# fix spacing around dashes and slashes:
cleaned = '/'.join([w.strip() for w in cleaned.split('/')])
cleaned = '-'.join([w.strip() for w in cleaned.split('-')])
return cleaned if only_uppercase.match(cleaned) else cleaned.title()
# we build a dictionary where we can look up the correct names:
id_mappings = {}
for f in csv_files:
prefix = basename(f)[:2]
reader = csv.reader(open(f, 'r'), delimiter=';')
# skip the first 4 columns
for _ in range(4):
next(reader)
current_pgr = '00'
current_bzr = '00'
current_plr = '00' # for the sake of readability
for row in reader:
pgr = row[4].strip()
if pgr != '' and pgr != current_pgr:
current_pgr = pgr.zfill(2)
id_mappings[prefix + current_pgr] = clean_name(row[5])
bzr = row[7].strip()
if bzr != '' and bzr != current_bzr:
current_bzr = bzr.zfill(2)
name = clean_name(row[8])
id_mappings[prefix + current_pgr + current_bzr] = name
plr = row[10].strip()
if plr != '':
current_plr = plr.zfill(2)
name = clean_name(row[11])
id_mappings[prefix + current_pgr + current_bzr + current_plr] = name # noqa
# 🔧🔧🔧
json_files = glob.glob('berlin-lor*json')
for j in json_files:
print('🚂 Processing {}'.format(j))
data = json.load(open(j, 'r'))
if '.geojson' in j:
for i, feature in enumerate(data['features']):
keys = feature['properties'].keys()
name_key = [k for k in keys if '_NAME' in k][0]
given_name = feature['properties'][name_key]
# some shapes don't have a name (wtf?)
if not given_name:
print('🤔 Entry {} doesn\'t have a name, skipping it'.format(i)) # noqa
continue
try:
correct_name = id_mappings[feature['properties']['SCHLUESSEL']]
except:
print('🤔 Unknown key ({}) for entry {}'.format(
feature['properties']['SCHLUESSEL'], i))
if (given_name != correct_name):
# print('🔧 Fixing {} to {}'.format(given_name, correct_name))
data['features'][i]['properties'][name_key] = correct_name
else:
x = list(data['objects'].keys())[0]
for i, geom in enumerate(data['objects'][x]['geometries']):
keys = geom['properties'].keys()
name_key = [k for k in keys if '_NAME' in k][0]
given_name = geom['properties'][name_key]
# some shapes don't have a name (wtf?)
if not given_name:
print('🤔 Entry {} doesn\'t have a name, skipping it'.format(i)) # noqa
continue
try:
correct_name = id_mappings[geom['properties']['SCHLUESSEL']]
except:
print('🤔 Unknown key ({}) for entry {}'.format(
feature['properties']['SCHLUESSEL'], i))
if (given_name != correct_name):
# print('🔧 Fixing {} to {}'.format(given_name, correct_name))
data['objects'][x]['geometries'][i]['properties'][name_key] = correct_name # noqa
json.dump(data, open(j, 'w'))
print('✏️ Wrote result to {}'.format(j))
print()