Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/import/citybrain/citybrain_flow.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_flow.py
15
# @author Jakob Erdmann
16
# @date 2021-05-07
17
18
import os
19
import sys
20
21
if 'SUMO_HOME' in os.environ:
22
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))
23
sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools', 'route'))
24
import sumolib # noqa
25
from sort_routes import sort_departs # noqa
26
27
28
def get_options(args=None):
29
optParser = sumolib.options.ArgumentParser(description="Import citybrains traffic demand")
30
optParser.add_argument("-f", "--flow-file", dest="flowfile",
31
help="citybrains flow file to import")
32
optParser.add_argument("-o", "--output", dest="output",
33
help="define the output sumo route filename")
34
optParser.add_argument("-p", "--prefix", dest="prefix",
35
default="", help="prefix generated flow ids")
36
options = optParser.parse_args(args=args)
37
if not options.flowfile or not options.output:
38
optParser.print_help()
39
sys.exit(1)
40
41
return options
42
43
44
def main(options):
45
# unsorted temporary file
46
tmpfile = options.output + ".tmp"
47
48
with open(tmpfile, "w") as outf:
49
sumolib.writeXMLHeader(outf, "$Id$", "routes") # noqa
50
outf.write(' <vType id="DEFAULT_VEHTYPE" length="4" minGap="1"/>\n\n')
51
flowLine = 0
52
flowIndex = 0
53
for i, line in enumerate(open(options.flowfile)):
54
if i > 0:
55
if flowLine == 0:
56
flowID = "%s%s" % (options.prefix, flowIndex)
57
begin, end, period = line.split()
58
outf.write(' <flow id="%s" begin="%s" end="%s" period="%s">\n' % (
59
flowID, begin, end, period))
60
elif flowLine == 2:
61
edges = line.strip()
62
outf.write(' <route edges="%s"/>\n' % edges)
63
outf.write(' </flow>\n')
64
flowIndex += 1
65
flowLine = (flowLine + 1) % 3
66
outf.write('</routes>\n')
67
68
with open(options.output, "w") as outf:
69
sort_departs(tmpfile, outf)
70
os.remove(tmpfile)
71
72
73
if __name__ == "__main__":
74
if not main(get_options()):
75
sys.exit(1)
76
77