This repository was archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix.py
More file actions
171 lines (142 loc) · 4.87 KB
/
fix.py
File metadata and controls
171 lines (142 loc) · 4.87 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
import re
import audit
street_type_re = re.compile(r'\b\S+\.?$', re.IGNORECASE)
EXPECTED = ["avinguda", "carrer", "camí", "carretera", "gran", "jardi",
"jardins", "mas", "moll", "parc", "passadís",
"passatge", "passeig", "plaça".decode('utf-8'), "polígon",
"rambla", "ronda", "travessera", "via"]
LANG_MAPPING = {"acceso": "Accés",
"avenida": "Avinguda",
"calle": "Carrer",
"camino": "Camí",
"paseo": "Passeig",
"plaza": "Plaça".decode('utf-8'),
"vía": "Via"
}
MAPPING = {"av": "Avinguda",
"av.": "Avinguda",
"avda": "Avinguda",
"avda.": "Avinguda",
"c": "Carrer",
"c.": "Carrer",
"c/": "Carrer",
"carrar": "Carrer",
"carrerde": "Carrer",
"carrerl": "Carrer",
"cl": "Carrer",
"cr": "Carrer",
"cra": "Carretera",
"ctra": "Carretera",
"ctra.": "Carretera",
"carreralt": "Carretera",
"Pasatge": "Passatge",
"pg.": "Passeig",
"pg": "Passeig",
"pas": "Passeig",
"paaseig": "Passeig",
"pl.": "Plaça".decode('utf-8'),
"pl": "Plaça".decode('utf-8'),
"pla": "Plaça".decode('utf-8'),
"placa": "Plaça".decode('utf-8'),
"polígono": "Polígon",
"rbla.": "Rambla",
"rembla": "Rambla",
"Vía": "Via"
}
NON_CASE = ["de", "del", "la", "les", "el", "els", "i"]
uncaught_st = set()
# n_xxx keep track of the number of fixes applied to the data set
n_fix_lang = 0
def map_node(element, node_attr_fields):
node_attribs = {}
for attr in element.attrib:
if attr in node_attr_fields:
node_attribs[attr] = element.attrib[attr]
return node_attribs
def map_way(element, way_attr_fields):
way_attribs = {}
for attr in element.attrib:
if attr in way_attr_fields:
way_attribs[attr] = element.attrib[attr]
return way_attribs
def get_tags(element, unique_id,
problem_chars,
default_tag_type):
tags = []
# NODE/WAY_TAGS_FIELDS
for tag in element.iter("tag"):
t = {}
# NODE/WAY_TAGS_FIELDS[0]:id
# id maps to the top level node/way id attribute value
t["id"] = unique_id
# NODE/WAY_TAGS_FIELDS[1]:key
# if there's no ":" key maps to the full "k" attribute
# if there's ":" key only maps to the characters after the colon
# NODE/WAY_TAGS_FIELDS[3]:type
# type maps to the characters before the colon in the tag
# type equals "regular" if there's no ":"
k = tag.attrib["k"]
m = problem_chars.search(k)
if not m:
if ":" not in k:
t["key"] = k
t["type"] = default_tag_type
else:
cut = k.find(":") + 1
t["key"] = k[cut:]
t["type"] = k[:cut - 1]
else:
t["type"] = default_tag_type
# NODE/WAY_TAGS_FIELDS[2]:value
v = tag.attrib["v"]
if audit.is_postcode(tag):
if len(v) != 5:
v = None
elif v[:2] != "08":
v = None
if audit.is_street_name(tag):
st_type = get_street_type(v)
st_name = v[len(st_type) + 1:]
if st_type.lower() in LANG_MAPPING:
v = fix_lang(st_type, st_name)
elif st_type.lower() in EXPECTED:
st_type = fix_case(st_type)
st_name = fix_case(st_name)
v = st_type + " " + st_name
elif st_type.lower() in MAPPING:
st_type = MAPPING[st_type.lower()]
st_name = fix_case(st_name)
v = st_type + " " + st_name
else:
st_type = fix_case(st_type)
uncaught_st.add(st_type)
# value maps to the full "v" attribute
t["value"] = v
tags.append(t)
return tags
def get_street_type(v):
street_type = v.split(' ', 1)[0]
return street_type
def fix_case(s):
d = []
s_array = s.split()
for e in s_array:
if e in NON_CASE:
d.append(e)
continue
if "\'" in e and len(e) > 3:
p = e.find("\'")
if not e.endswith("\'"):
temp = e[p-1].lower() + e[p] + e[p+1].upper() + e[p+2:].lower()
d.append(temp)
continue
else:
temp = e[0].upper() + e[1:].lower()
d.append(temp)
s_fix_case = " ".join(d)
return s_fix_case
def fix_lang(st_type, st_name):
st_type_fix_lang = LANG_MAPPING[st_type.lower()]
street_fix_lang = st_type_fix_lang + ' ' + st_name
return street_fix_lang