Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/import/citybrain/citybrain_infostep.py
169679 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2010-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 citybrain_infostep.py
15
# @author Jakob Erdmann
16
# @date 2021-05-07
17
18
from __future__ import print_function
19
import os
20
import sys
21
22
if 'SUMO_HOME' in os.environ:
23
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
24
import sumolib # noqa
25
26
27
def get_options(args=None):
28
optParser = sumolib.options.ArgumentParser(description="Import citybrains step info")
29
optParser.add_argument("-i", "--info-file", dest="infofile",
30
help="citybrains step info file to import")
31
optParser.add_argument("-o", "--output", dest="output",
32
help="define the output sumo route filename")
33
optParser.add_argument("-l", "--lastpos", dest="lastpos", action="store_true",
34
default=False, help="use departPos 'last' to include more vehicles")
35
optParser.add_argument("--length", default=4, help="default vehicle length")
36
optParser.add_argument("--mingap", default=1, help="default vehicle mingap")
37
options = optParser.parse_args(args=args)
38
if not options.infofile or not options.output:
39
optParser.print_help()
40
sys.exit(1)
41
42
return options
43
44
45
def main(options):
46
# unsorted temporary file
47
48
with open(options.output, "w") as outf:
49
sumolib.writeXMLHeader(outf, "$Id$", "routes") # noqa
50
outf.write(' <vType id="DEFAULT_VEHTYPE" length="%s" minGap="%s"/>\n\n' % (
51
options.length, options.mingap))
52
vehLine = 0
53
vehID = None
54
pos = None
55
edges = []
56
speed = None
57
# startTime = None
58
ttFF = None
59
lane = None
60
seenVehs = 0
61
writtenVehs = 0
62
for i, line in enumerate(open(options.infofile)):
63
if i > 0:
64
if vehLine == 0:
65
vehID = line.split()[-1]
66
elif vehLine == 1:
67
pos = float(line.split()[-1])
68
elif vehLine == 2:
69
# assume 3-lane roads
70
lane = 2 - (int(float(line.split()[-1])) % 100)
71
elif vehLine == 4:
72
edges = [str(int(float(e))) for e in line.split(":")[-1].split()]
73
elif vehLine == 5:
74
speed = line.split()[-1]
75
# elif vehLine == 6:
76
# startTime = line.split()[-1]
77
elif vehLine == 7:
78
ttFF = line.split()[-1]
79
80
seenVehs += 1
81
# skip vehicles on their arrival edge
82
if len(edges) > 1:
83
if pos == 0 or options.lastpos:
84
pos = "last"
85
writtenVehs += 1
86
print(' <vehicle id="%s" depart="0" departPos="%s" departSpeed="%s" departLane="%s">' %
87
(vehID, pos, speed, lane), file=outf)
88
outf.write(' <route edges="%s"/>\n' % ' '.join(edges))
89
# outf.write(' <param key="startTime" value="%s"/>\n' % startTime)
90
outf.write(' <param key="ttFF" value="%s"/>\n' % ttFF)
91
outf.write(' </vehicle>\n')
92
93
vehLine = (vehLine + 1) % 10
94
outf.write('</routes>\n')
95
96
print("loaded %s vehicles and wrote %s" % (seenVehs, writtenVehs))
97
98
99
if __name__ == "__main__":
100
if not main(get_options()):
101
sys.exit(1)
102
103