-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm_handlers.py
192 lines (159 loc) · 6.75 KB
/
osm_handlers.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import osmium
import numpy as np
class OSMBridgeCounter(osmium.SimpleHandler):
def __init__(self) -> None:
super().__init__()
self.total = 0
self.footway = 0
def way(self, w):
if w.tags.get('bridge') == 'yes':
self.total += 1
# if w.tags.get('highway') == 'footway':
# self.footway += 1
class OSMParser(osmium.SimpleHandler):
def __init__(self) -> None:
super().__init__()
self.loops = 0
self.ways = {}
self.nodes = {}
self.values = None
def is_bridge(self, tags):
return tags.get('bridge') == 'yes' and tags.get('highway') != 'footway'
def way(self, w):
# If not on our first iteration, leave
if self.loops != 0:
return
# If we have a bridge, store its referenced node in the ways.
if self.is_bridge(w.tags):
self.ways[w.id] = [node.ref for node in w.nodes]
def node(self, n):
# if not on our second iteration, leave
if self.loops != 1:
return
# get our node id and check if it is referenced.
ref = n.id
x = np.where(self.values == ref)
# If the node is referenced, save its lat and long
if x[0].size != 0 or x[1].size != 0:
self.nodes[ref] = {'lat': n.location.lat, 'lon': n.location.lon}
def parse(self, file):
# Iteration 1: collect bridge ways
self.apply_file(file)
self.values = np.array(list(self.ways.values()))
self.loops += 1
# Iteration 2: collect referenced node lat/lon
self.apply_file(file)
def clean(self):
lats = []
lons = []
clean = []
for id, nodes in self.ways.items():
for node in nodes:
lats.append(float(self.nodes[node]['lat']))
lons.append(float(self.nodes[node]['lon']))
mid_lat = np.average(lats)
mid_lon = np.average(lons)
# print(mid_lat,mid_lon)
clean.append({'id':id, 'lat': round(mid_lat, 4), 'lon': round(mid_lon, 4)})
return clean
class OSMNBIMergerNominatim(osmium.SimpleHandler):
def __init__(self, writer, ways, debug=False) -> None:
super().__init__()
self.writer = writer
self.ways = ways
self.debug = debug # If True, OSM and matching values will be logged for each entry.
if self.debug:
self.log = open('out/osm_log', 'w')
def is_bridge(self, tags):
return tags.get('bridge') == 'yes' and tags.get('highway') != 'footway'
def way(self, w):
id = str(w.id)
if self.debug:
self.log.write(f'{w.tags.get("bridge")=="yes"}, {w.tags.get("highway")!="footway"}, {id in list(self.ways.keys())}\n')
# if self.is_bridge(w.tags) and id in list(self.ways.keys()):
# For now, we are ignoring bridge labels so we can assign more data.
# if w.tags.get('bridge') == 'yes' \
if w.tags.get('highway') != 'footway' \
and id in list(self.ways.keys()):
# print(id)
if self.debug: print("Match found!")
new_tags = {
'nbi:id':str(self.ways[id]['id-state']+","+self.ways[id]['id-no']+","+self.ways[id]['id-owner']),
'nbi:super-cond':self.ways[id]['super-cond'],
'nbi:sub-cond':self.ways[id]['sub-cond'],
'nbi:op-rating':self.ways[id]['op-rating'],
'nbi:op-method-code':self.ways[id]['op-method-code'],
'nbi:deck-rating':self.ways[id]['deck-rating'],
'nbi:inv-rating':self.ways[id]['inv-rating'],
}
orig_tags = dict(w.tags)
all_tags = orig_tags | new_tags
new = w.replace(tags=all_tags, version=w.version+1)
self.writer.add_way(new)
else:
self.writer.add_way(w)
def node(self, n):
self.writer.add_node(n)
def relation(self, r):
self.writer.add_relation(r)
def close(self):
if self.debug:
self.log.close()
### Use this for the Overpass Querying. It will remove redundant tag checking.
class OSMNBIMergerOP(osmium.SimpleHandler):
def __init__(self, writer, ways, debug=False) -> None:
super().__init__()
self.writer = writer
self.ways = ways
self.debug = debug # If True, OSM and matching values will be logged for each entry.
if self.debug:
self.log = open('out/osm_log', 'w')
print(list(self.ways.keys()))
def way(self, w):
id = str(w.id)
# for self.ways - Key: OSM_ID, Value: NBI_ID
if id in list(self.ways.keys()):
if self.debug: print("Match found!")
new_tags = {
'nbi:id':str(self.ways[id]['id-state']+","+self.ways[id]['id-no']+","+self.ways[id]['id-owner']),
'nbi:super-cond':self.ways[id]['super-cond'],
'nbi:sub-cond':self.ways[id]['sub-cond'],
'nbi:op-rating':self.ways[id]['op-rating'],
'nbi:op-method-code':self.ways[id]['op-method-code'],
'nbi:deck-rating':self.ways[id]['deck-rating'],
'nbi:inv-rating':self.ways[id]['inv-rating'],
}
orig_tags = dict(w.tags)
all_tags = orig_tags | new_tags
new = w.replace(tags=all_tags, version=w.version+1)
self.writer.add_way(new)
else:
self.writer.add_way(w)
def node(self, n):
self.writer.add_node(n)
def relation(self, r):
self.writer.add_relation(r)
def close(self):
if self.debug:
self.log.close()
import csv
class OSMNBIAnalyzer(osmium.SimpleHandler):
def __init__(self, ways, keys) -> None:
super().__init__()
self.ways = ways
self.csvfile = open("out/nbi-analysis.csv", 'w')
# creating a csv writer object
self.csvwriter = csv.writer(self.csvfile)
self.csvwriter.writerow(keys)
def is_bridge(self, tags):
return tags.get('bridge') == 'yes' and tags.get('highway') != 'footway'
def way(self, w):
id = str(w.id)
if self.is_bridge(w.tags) and id in list(self.ways.keys()):
print("Match found!")
self.ways[id][0].update({"osm-match": "True"})
self.ways[id][0].update({"osm-match-id": w.id})
self.ways[id][0].update({"osm-match-inf": "NONE"})
def close(self):
if self.debug:
self.log.close()