forked from darauble/Locus-Map-DIY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-routes.py
186 lines (138 loc) · 4.54 KB
/
parse-routes.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
import osmium
import os
import sys
REL_TYPES = ["historic", "mtb", "bicycle", "foot", "hiking"]
HIKING_NETWORKS = ["rwn", "lwn", "nwn", "iwn"]
CYCLING_NETWORKS = ["rcn", "lcn", "ncn", "icn"]
all_ways = {}
class RelationsHandler(osmium.SimpleHandler):
def __init__(self, all_ways):
osmium.SimpleHandler.__init__(self)
self.all_ways = all_ways
def relation(self, r):
if "route" in r.tags and r.tags.get("route") in REL_TYPES:
#print("Relation:")
relation = {
"name": "",
"route": "",
"network": "",
}
for k, v in r.tags:
#print("\t", k, "=", v)
relation[k] = v
#print("\t", r.members)
for m in r.members:
#print(m.type)
if m.type == "w":
#print("\tway: %s" % m.ref)
if m.ref not in self.all_ways:
self.all_ways[m.ref] = []
self.all_ways[m.ref].append(relation)
if relation["network"] not in HIKING_NETWORKS and relation["network"] not in CYCLING_NETWORKS:
if relation["route"] == "hiking" or relation["route"] == "foot" or relation["route"] == "historic":
if relation["network"] == "lt:regional":
relation["network"] = "rwn"
else:
relation["network"] = "lwn"
else:
if relation["network"] == "lt:regional":
relation["network"] = "rcn"
else:
relation["network"] = "lcn"
if relation["name"] != "" and relation["network"] != "":
print("\tRelation:")
print("\t\tname: %s" % relation["name"])
print("\t\troute: %s" % relation["route"])
print("\t\tnetwork: %s" % relation["network"])
if "osmc:symbol" in relation:
osmc = relation["osmc:symbol"].split(":")
if len(osmc) >= 3:
relation["osmc"] = "yes"
relation["osmc_color"] = osmc[0]
relation["osmc_background"] = osmc[1]
relation["osmc_foreground"] = osmc[2]
print("\t\tosmc:symbol: %s" % relation["osmc:symbol"])
del relation["osmc:symbol"]
if "ref" in relation:
print("\t\tref: %s" % relation["ref"])
else:
relation["ref"] = relation["name"]
for k, v in relation.items():
if k not in ["name", "route", "network"]:
print("\t\t%s: %s" % (k, v))
class WayHandler(osmium.SimpleHandler):
def __init__(self, all_ways, way_writer):
osmium.SimpleHandler.__init__(self)
self.all_ways = all_ways
self.way_writer = way_writer
self.way_id = -800000000000
def way(self, w):
if w.id in self.all_ways:
#self.way_writer.add_way(w)
rel_count = len(self.all_ways[w.id])
#for relation in self.all_ways[w.id]:
for i in range(0, rel_count):
relation = self.all_ways[w.id][i]
if (
(rel_count == 2 and i == 1)
or
(rel_count > 2 and (relation["route"] == "bicycle" or relation["route"] == "mtb"))
):
way_nodes = []
for node in w.nodes:
way_nodes.append(node)
new_nodes = []
for i in range(len(way_nodes)-1, -1, -1):
new_nodes.append(way_nodes[i])
way = w.replace(id=self.way_id, nodes=new_nodes, tags=relation)
else:
way = w.replace(id=self.way_id, tags=relation)
self.way_writer.add_way(way)
self.way_id = self.way_id + 1
file_name = sys.argv[1]
bbox_name = sys.argv[1] + ".bbox"
output_tmp = sys.argv[1] + ".tmp.xml"
output_name = sys.argv[2]
print("Reading header...")
reader = osmium.io.Reader(file_name)
header = reader.header()
box = header.box()
print("\t", header.box())
reader.close()
print("...done")
print("Writing bounding box...")
with open(bbox_name, "wb") as f:
bounding_param = " <bounds minlon=\"%s\" minlat=\"%s\" maxlon=\"%s\" maxlat=\"%s\" origin=\"Darau, Blė parser\"/>" % (
min(box.bottom_left.lon, box.top_right.lon), min(box.top_right.lat, box.bottom_left.lat),
max(box.bottom_left.lon, box.top_right.lon), max(box.top_right.lat, box.bottom_left.lat)
) + os.linesep
f.write(bounding_param.encode("utf-8"))
print("...done")
print("Parsing relations...")
rel_parser = RelationsHandler(all_ways)
rel_parser.apply_file(file_name)
print("...done, found ways: %d" % len(all_ways))
print("Extracting relation ways...")
if os.path.exists(output_tmp):
os.remove(output_tmp)
if os.path.exists(output_name):
os.remove(output_name)
way_writer = osmium.SimpleWriter(output_tmp)
way_parser = WayHandler(all_ways, way_writer)
way_parser.apply_file(file_name)
way_writer.close()
print("...done")
print("Hell, adding bounding box...")
with open(output_name, "w") as output:
with open(output_tmp, "r") as tmp:
i = 0
for l in tmp:
output.write(l)
i = i + 1
if i == 2:
with open(bbox_name) as bbox:
for bl in bbox:
output.write(bl)
os.remove(output_tmp)
os.remove(bbox_name)
print("...done")