-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit2.py
157 lines (132 loc) · 5.53 KB
/
audit2.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
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
#this script parses an osm xml file, performs audits and prints values
#of data that is expected to change during the xml to csv conversion
# problems in the data
import xml.etree.cElementTree as ET
import re
OSMFILE = "map.osm"
DEBUG = True
#matches last word in street type (st, rd...)
street_type_re = re.compile(r'(\b\S+\.?)$', re.IGNORECASE)
#matches 2nd to last word in street type
street_type_re2 = re.compile(r'(\b\S+\.?) \S+$', re.IGNORECASE)
#matches direction at end of street type name
#directional_re = re.compile(r'((\s[NESW][EW]?)|(\Dth|\Sst))$', re.IGNORECASE)
directional_re = re.compile(r'\s(North|East|South|West)$', re.IGNORECASE)
street_name_mapping = {"av": "Avenue", "av.": "Avenue", "ave": "Avenue",
"ave.": "Avenue", "blvd": "Boulevard",
"blvd.": "Boulevard", "cir": "Circle",
"ct": "Court", "ct.": "Court",
"dr": "Drive","dr.": "Drive",
"hwy": "Highway", "hwy.": "Highway",
"ln": "Lane", "pkwy": "Parkway", "pl": "Place",
"rd": "Road", "rd.": "Road",
"st": "Street", "st.": "Street",
"ter": "Terrace",
"wy": "Way"
}
directional_mapping = {"N.": "N", "North": "N", "S.": "S", "South": "S",
"E.": "E", "East": "E", "W.": "West", "West": "W",
"NE.": "NE", "Northeast": "NE", "NW.": "NW",
"Northwest": "NW", "SE.": "SE", "Southeast": "SE",
"SW.": "SW", "Southwest": "SW"
}
#update the street type, 1st the direction then the type
def update_street_name(name):
in_str = name
#check last position for direction
d = street_type_re.search(name)
if d:
#check for direction
street_end = None
street_end = directional_mapping.get(d.group(), "")
#is a direction
if street_end :
p = re.compile(d.group())
name = p.sub(street_end, name)
#if this had a direction check 2nd to last word for street type
m = street_type_re2.search(name)
if m:
#check for street type
street_end = None
street_end = street_name_mapping.get(m.group(1).lower(), "")
#is a street type
if street_end:
p = re.compile(m.group(1))
name = p.sub(street_end, name)
#check the last word for street type
m = street_type_re.search(name)
if m:
#check for street type
street_end = None
street_end = street_name_mapping.get(m.group(1).lower(), "")
#mapping found
if street_end:
p = re.compile(m.group(1))
name = p.sub(street_end, name)
#if in_str != name:
#print("Returning: ", in_str, " --> ", name)
#return updated string
return name
def update_postcode(postcode):
in_str = postcode
if len(postcode) > 5:
postcode = postcode[:-5]
#if in_str != postcode:
#print("Returning: ", in_str, " --> ", postcode)
return postcode
def update_house_number(house_number):
in_str = house_number
#check if number contains a house_number.
if not house_number.isdigit():
house_number = house_number.upper()
#if in_str != house_number:
#print("Returning: ", in_str, " --> ", house_number)
return house_number
def update_city(city):
in_str = city
#print("checking:", in_str)
if city.lower() == 'palm habror':
city = 'Palm Harbor'
#if in_str != city:
#print("Returning: ", in_str, " --> ", city)
return city
def audit(osmfile):
osm_file = open(osmfile, "r", encoding="utf-8")
#parse file perform audits on specific tags
for event, elem in ET.iterparse(osm_file, events=("start",)):
if elem.tag == "node" or elem.tag == "way":
for tag in elem.iter("tag"):
k = tag.attrib['k']
v = tag.attrib['v']
#set initial value
in_val = v
if k == 'addr:street':
v = update_street_name(v)
#set value returned and print if it is different
#then initial
out_val = v
if out_val != in_val:
print("street: ", v)
elif k == 'addr:housenumber':
v = update_house_number(v)
#set value returned and print if it is different
#then initial
out_val = v
if out_val != in_val:
print(" elem:", elem.tag, " housenumber: ", v)
elif k == 'addr:city':
v = update_city(v)
#set value returned and print if it is different
#then initial
out_val = v
if out_val != in_val:
print(" elem:", elem.tag, " city: ", v)
elif k == 'addr:postcode':
v = update_postcode(v)
#set value returned and print if it is different
#then initial
out_val = v
if out_val != in_val:
print(" elem:", elem.tag, " postcode: ", v)
osm_file.close()
audit(OSMFILE)