Path: blob/main/tools/import/citybrain/citybrain_flow.py
169679 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2010-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file citybrain_flow.py14# @author Jakob Erdmann15# @date 2021-05-071617import os18import sys1920if 'SUMO_HOME' in os.environ:21sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))22sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools', 'route'))23import sumolib # noqa24from sort_routes import sort_departs # noqa252627def get_options(args=None):28optParser = sumolib.options.ArgumentParser(description="Import citybrains traffic demand")29optParser.add_argument("-f", "--flow-file", dest="flowfile",30help="citybrains flow file to import")31optParser.add_argument("-o", "--output", dest="output",32help="define the output sumo route filename")33optParser.add_argument("-p", "--prefix", dest="prefix",34default="", help="prefix generated flow ids")35options = optParser.parse_args(args=args)36if not options.flowfile or not options.output:37optParser.print_help()38sys.exit(1)3940return options414243def main(options):44# unsorted temporary file45tmpfile = options.output + ".tmp"4647with open(tmpfile, "w") as outf:48sumolib.writeXMLHeader(outf, "$Id$", "routes") # noqa49outf.write(' <vType id="DEFAULT_VEHTYPE" length="4" minGap="1"/>\n\n')50flowLine = 051flowIndex = 052for i, line in enumerate(open(options.flowfile)):53if i > 0:54if flowLine == 0:55flowID = "%s%s" % (options.prefix, flowIndex)56begin, end, period = line.split()57outf.write(' <flow id="%s" begin="%s" end="%s" period="%s">\n' % (58flowID, begin, end, period))59elif flowLine == 2:60edges = line.strip()61outf.write(' <route edges="%s"/>\n' % edges)62outf.write(' </flow>\n')63flowIndex += 164flowLine = (flowLine + 1) % 365outf.write('</routes>\n')6667with open(options.output, "w") as outf:68sort_departs(tmpfile, outf)69os.remove(tmpfile)707172if __name__ == "__main__":73if not main(get_options()):74sys.exit(1)757677