Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/route/vehicle2flow.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2013-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 vehicle2flow.py
15
# @author Michael Behrisch
16
# @date 2012-11-15
17
18
"""
19
This script replaces all vehicle definitions in a route file by
20
flow definitions, adding an XML entity for the repeat interval for
21
easy later modification.
22
"""
23
from __future__ import absolute_import
24
import sys
25
import re
26
from optparse import OptionParser
27
28
29
def parse_args():
30
USAGE = "Usage: " + sys.argv[0] + " <routefile> [options]"
31
optParser = OptionParser()
32
optParser.add_option("-o", "--outfile", help="name of output file")
33
optParser.add_option(
34
"-r", "--repeat", default=1000, type="float", help="repeater interval")
35
optParser.add_option(
36
"-e", "--end", default=2147483, type="float", help="end of the flow")
37
optParser.add_option("-w", "--with-entities", action="store_true",
38
default=False, help="store repeat and end as entities")
39
options, args = optParser.parse_args()
40
try:
41
options.routefile = args[0]
42
except Exception:
43
sys.exit(USAGE)
44
if options.outfile is None:
45
options.outfile = options.routefile + ".rou.xml"
46
return options
47
48
49
def main():
50
options = parse_args()
51
with open(options.routefile) as f:
52
with open(options.outfile, 'w') as outf:
53
for line in f:
54
if options.with_entities:
55
if "<routes " in line or "<routes>" in line:
56
outf.write("""<!DOCTYPE routes [
57
<!ENTITY RepeatInterval "%s">
58
<!ENTITY RepeatEnd "%s">
59
]>
60
""" % (options.repeat, options.end))
61
line = re.sub(
62
r'<vehicle(.*)depart( ?= ?"[^"]*")', r'<flow\1begin\2 end="&RepeatEnd;" ' +
63
'period="&RepeatInterval;"', line)
64
else:
65
line = re.sub(
66
r'<vehicle(.*)depart( ?= ?"[^"]*")', r'<flow\1begin\2 end="%s" period="%s"' %
67
(options.end, options.repeat), line)
68
line = re.sub(r'</vehicle>', '</flow>', line)
69
outf.write(line)
70
71
72
if __name__ == "__main__":
73
main()
74
75