Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/import/vissim/vissim_parseBusStops.py
169679 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file vissim_parseBusStops.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2009-05-27
18
19
"""
20
21
Parses bus stops and bus routes given in the Vissim file (first parameter).
22
23
The read bus lines are saved as <OUTPUT_PREFIX>_busses.rou.xml
24
The read routes are saved as <OUTPUT_PREFIX>_stops.add.xml
25
26
(Starting?) edges of the route may be renamed by setting them within "edgemap"
27
variable (see below).
28
"""
29
from __future__ import absolute_import
30
from __future__ import print_function
31
import os
32
import sys
33
if 'SUMO_HOME' in os.environ:
34
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
35
import sumolib # noqa
36
37
edgemap = {}
38
edgemap["203"] = "203[0]"
39
edgemap["78"] = "78[0]"
40
edgemap["77"] = "77[0]"
41
edgemap["203"] = "203[0]"
42
43
44
def getName(vals, beg):
45
name = vals[beg]
46
while name.count('"') != 2:
47
beg = beg + 1
48
name = name + " " + vals[beg]
49
return name.replace('"', '')
50
51
52
def parseBusStop(bs):
53
vals = bs.split()
54
id = int(vals[1])
55
i = vals.index("NAME")
56
name = getName(vals, i + 1)
57
i = vals.index("STRECKE", i)
58
strecke = vals[i + 1]
59
i = vals.index("SPUR", i)
60
spur = int(vals[i + 1]) - 1
61
i = vals.index("BEI", i)
62
von = float(vals[i + 1])
63
i = vals.index("LAENGE", i)
64
bis = von + float(vals[i + 1])
65
return (id, name, strecke, spur, von, bis)
66
67
68
def parseBusRoute(br, stops):
69
vals = br.split()
70
id = vals[1]
71
i = vals.index("NAME")
72
name = getName(vals, i + 1)
73
i = vals.index("EINFAHRT", i)
74
startKante = vals[i + 2]
75
i = vals.index("ZIEL", i)
76
ziel = vals[i + 2]
77
zeiten = []
78
endI = vals.index("HALTESTELLE", i)
79
i = vals.index("STARTZEITEN", i)
80
i = i + 1
81
while i > 0 and i < endI:
82
zeiten.append(int(float(vals[i])))
83
i = i + 5
84
stops = []
85
while i > 0 and i < len(vals):
86
try:
87
i = vals.index("HALTESTELLE", i)
88
i = i + 1
89
stops.insert(0, int(vals[i]))
90
except Exception:
91
i = len(vals) + 1
92
return (id, name, startKante, ziel, zeiten, stops)
93
94
95
def sorter(idx):
96
def t(i, j):
97
if i[idx] < j[idx]:
98
return -1
99
elif i[idx] > j[idx]:
100
return 1
101
else:
102
return 0
103
104
105
# MAIN
106
if __name__ == '__main__':
107
op = sumolib.options.ArgumentParser()
108
op.add_option("vissimNet", category="input", type=op.file,
109
help="provide the vissim file path")
110
op.add_option("outputPrefix", category="input",
111
help="provide the output prefix")
112
args = op.parse_args()
113
114
print("Parsing Vissim input...")
115
fd = open(args.vissimNet)
116
haveStop = False
117
haveRoute = False
118
currentItem = ""
119
stopsL = []
120
routesL = []
121
for line in fd:
122
# process bus stops ("HALTESTELLE")
123
if line.find("HALTESTELLE") == 0:
124
if haveStop:
125
stopsL.append(" ".join(currentItem.split()))
126
haveStop = True
127
currentItem = ""
128
elif line[0] != ' ':
129
if haveStop:
130
stopsL.append(" ".join(currentItem.split()))
131
haveStop = False
132
if haveStop:
133
currentItem = currentItem + line
134
# process bus routes ("LINIE")
135
if line.find(" LINIE") == 0:
136
if haveRoute:
137
routesL.append(" ".join(currentItem.split()))
138
haveRoute = True
139
currentItem = ""
140
elif len(line) > 2 and line[0] != ' ' and line[1] != ' ':
141
if haveRoute:
142
routesL.append(" ".join(currentItem.split()))
143
haveRoute = False
144
if haveRoute:
145
currentItem = currentItem + line
146
147
# build stops map
148
sm = {}
149
for bs in stopsL:
150
(id, name, strecke, spur, von, bis) = parseBusStop(bs)
151
sm[id] = (id, name, strecke, spur, von, bis)
152
153
# process bus routes
154
# build departure times
155
emissions = []
156
for br in routesL:
157
(pid, name, startKante, ziel, zeiten, stops) = parseBusRoute(br, sm)
158
edges = []
159
edges.append(startKante)
160
for s in stops:
161
if sm[s][2] not in edges:
162
edges.append(sm[s][2])
163
if ziel not in edges:
164
edges.append(ziel)
165
for i in range(0, len(edges)):
166
if edges[i] in edgemap:
167
edges[i] = edgemap[edges[i]]
168
for t in zeiten:
169
id = str(pid) + "_" + str(t)
170
emissions.append((int(t), id, edges, stops))
171
172
# sort emissions
173
print("Sorting routes...")
174
emissions.sort(sorter(0))
175
# write routes
176
print("Writing bus routes...")
177
fdo = open(args.outputPrefix + "_busses.rou.xml", "w")
178
fdo.write("<routes>\n")
179
for emission in emissions:
180
if len(emission[2]) < 2:
181
continue
182
fdo.write(' <vehicle id="' + emission[1] + '" depart="' + str(emission[
183
0]) + '" type="bus" color="0,1,0"><route>' + " ".join(emission[2]) + '</route>\n')
184
for s in emission[3]:
185
fdo.write(
186
' <stop bus_stop="' + str(s) + '_0" duration="20"/>\n')
187
fdo.write(' </vehicle>\n')
188
fdo.write("</routes>\n")
189
190
# process bus stops
191
print("Writing bus stops...")
192
fdo = open(args.outputPrefix + "_stops.add.xml", "w")
193
fdo.write("<add>\n")
194
for bs in stopsL:
195
(id, name, strecke, spur, von, bis) = parseBusStop(bs)
196
fdo.write(' <busStop id="' + str(id) + '" lane="' + strecke + "_" +
197
str(spur) + '" from="' + str(von) + '" to="' + str(bis) + '" lines="--"/>\n')
198
fdo.write("</add>\n")
199
fdo.close()
200
201