-
Notifications
You must be signed in to change notification settings - Fork 2
/
osm_parser.py
201 lines (171 loc) · 6.35 KB
/
osm_parser.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
193
194
195
196
197
198
199
200
201
class Node:
def __init__(self, iD, lat, lon, tags, version=None, changeset=None, user=None, timestamp=None):
self.iD = iD
self.lat = lat
self.lon = lon
self.tags = tags # []
self.version = version # int
self.changeset = changeset # int
self.user = user
self.timestamp = timestamp
def __str__(self):
return str(self.iD) + " @lat: " + str(self.lat) + " @lon: " + str(self.lon) + " $tags " + str(len(self.tags))
def type(self):
return 'node'
class Way:
def __init__(self, iD, nodes, tags, version=None, changeset=None, user=None, timestamp=None, fake_node=None):
self.iD = iD
self.nodes = nodes # []
self.tags = tags # {}
self.fake_node = None
self.version = version # int
self.changeset = changeset # int
self.user = user
self.timestamp = timestamp
def __str__(self):
return str(self.iD) + " $points " + str(len(self.nodes)) + " $tags " + str(len(self.tags))
def type(self):
return 'way'
def fake_it(self):
#nodes = [ (y,x) ]
count = 0
y_count = 0
x_count = 0
for i in self.nodes:
y_count += float(i[0])
x_count += float(i[1])
count += 1
y_s = y_count / count
x_s = x_count / count
self.fake_node = (y_s, x_s)
return y_s, x_s
def fake_instance(self):
return Node(self.iD, self.fake_node[0], self.fake_node[1], self.tags, user='fake')
class osmParser:
def __init__(self, path="export.osm", nodes=None, ways=None):
self.path = path
import xml.etree.ElementTree as XML
plik = XML.parse(self.path)
root = plik.getroot()
nodes_list = []
ways_list = []
for child in root:
if child.tag == "note" or child.tag == "meta":
pass
elif child.tag == "node":
iD = child.attrib["id"]
lat = child.attrib["lat"]
lon = child.attrib["lon"]
try:
ver = int(child.attrib["version"])
chan = child.attrib["changeset"]
user = child.attrib["user"]
time = child.attrib["timestamp"]
except:
#print("No version data")
pass
tags = {}
# if len(list(child)) != 0:
for tag in child:
if tag.attrib["k"] == "amenity":
if tag.attrib["v"] == "bicycle_rental":
tags["is_rental"] = "yes"
elif tag.attrib["k"] in ["capacity", "name", "network", "operator", "ref", "website", "source"]:
k = tag.attrib["k"]
v = tag.attrib["v"]
tags[k] = v
try:
c = Node(iD, lat, lon, tags, ver, chan, user, time)
except:
c = Node(iD, lat, lon, tags)
nodes_list.append(c)
# else:
# break
elif child.tag == "way":
iD = child.attrib["id"]
try:
ver = int(child.attrib["version"])
chan = child.attrib["changeset"]
user = child.attrib["user"]
time = child.attrib["timestamp"]
except:
pass
nodes = []
tags = {}
for instance in child:
if instance.tag == "nd":
nodes.append(instance.attrib["ref"])
else:
if instance.attrib["k"] == "amenity":
if instance.attrib["v"] == "bicycle_rental":
tags["is_rental"] = "yes"
elif instance.attrib["k"] in ["capacity", "name", "network", "operator", "ref", "website", "source"]:
k = instance.attrib["k"]
v = instance.attrib["v"]
tags[k] = v
try:
w = Way(iD, nodes, tags, version=ver,
changeset=chan, user=user, timestamp=time)
except:
w = Way(iD, nodes, tags)
ways_list.append(w)
self.nodes = nodes_list
self.ways = ways_list
def __str__(self):
return "Found " + str(len(self.nodes)) + " nodes and " + str(len(self.ways)) + " ways."
def find(self, iD, mode='n'):
'''Returns data from iD searched'''
if mode == 'n':
for i in self.nodes:
if i.iD == iD:
return i
elif mode == 'w':
for i in self.ways:
if i.iD == iD:
return i
else:
raise ValueError
def clear_nodes(self):
new_nodes = []
for i in self.nodes:
if len(i.tags) == 0:
pass
else:
new_nodes.append(i)
self.nodes = new_nodes
def fill_ways(self):
'''Fills up ways with data from nodes'''
for way in self.ways:
new_nodes = []
for node in way.nodes:
p = self.find(node)
node = (p.lat, p.lon)
new_nodes.append(node)
way.nodes = new_nodes
def fake_all(self):
'''Run fake_it for all ways in Class'''
for way in self.ways:
way.fake_it()
fi = way.fake_instance()
self.nodes.append(fi)
def remove_fakes(self):
'''Remove all fake nodes'''
new_nodes = []
for i in self.nodes:
if i.user == 'fake':
pass
else:
new_nodes.append(i)
self.nodes = new_nodes
def dumb_nodes(self):
print("DUMBING ALL NODES:................")
for i in self.nodes:
print(i)
# + ";TAGS: " + print(key, value) for key, value in i.tags.items())
print("ID: " + i.iD + ";LAT: " + i.lat + ";LON: " + i.lon)
print("DUMBED NODES#")
def dumb_ways(self):
print("DUMBING ALL WAYS:................")
for i in self.ways:
print(i)
print("DUMBED WAYS#")