Path: blob/main/tools/import/citybrain/citybrain_infostep.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_infostep.py14# @author Jakob Erdmann15# @date 2021-05-071617from __future__ import print_function18import os19import sys2021if 'SUMO_HOME' in os.environ:22sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))23import sumolib # noqa242526def get_options(args=None):27optParser = sumolib.options.ArgumentParser(description="Import citybrains step info")28optParser.add_argument("-i", "--info-file", dest="infofile",29help="citybrains step info file to import")30optParser.add_argument("-o", "--output", dest="output",31help="define the output sumo route filename")32optParser.add_argument("-l", "--lastpos", dest="lastpos", action="store_true",33default=False, help="use departPos 'last' to include more vehicles")34optParser.add_argument("--length", default=4, help="default vehicle length")35optParser.add_argument("--mingap", default=1, help="default vehicle mingap")36options = optParser.parse_args(args=args)37if not options.infofile or not options.output:38optParser.print_help()39sys.exit(1)4041return options424344def main(options):45# unsorted temporary file4647with open(options.output, "w") as outf:48sumolib.writeXMLHeader(outf, "$Id$", "routes") # noqa49outf.write(' <vType id="DEFAULT_VEHTYPE" length="%s" minGap="%s"/>\n\n' % (50options.length, options.mingap))51vehLine = 052vehID = None53pos = None54edges = []55speed = None56# startTime = None57ttFF = None58lane = None59seenVehs = 060writtenVehs = 061for i, line in enumerate(open(options.infofile)):62if i > 0:63if vehLine == 0:64vehID = line.split()[-1]65elif vehLine == 1:66pos = float(line.split()[-1])67elif vehLine == 2:68# assume 3-lane roads69lane = 2 - (int(float(line.split()[-1])) % 100)70elif vehLine == 4:71edges = [str(int(float(e))) for e in line.split(":")[-1].split()]72elif vehLine == 5:73speed = line.split()[-1]74# elif vehLine == 6:75# startTime = line.split()[-1]76elif vehLine == 7:77ttFF = line.split()[-1]7879seenVehs += 180# skip vehicles on their arrival edge81if len(edges) > 1:82if pos == 0 or options.lastpos:83pos = "last"84writtenVehs += 185print(' <vehicle id="%s" depart="0" departPos="%s" departSpeed="%s" departLane="%s">' %86(vehID, pos, speed, lane), file=outf)87outf.write(' <route edges="%s"/>\n' % ' '.join(edges))88# outf.write(' <param key="startTime" value="%s"/>\n' % startTime)89outf.write(' <param key="ttFF" value="%s"/>\n' % ttFF)90outf.write(' </vehicle>\n')9192vehLine = (vehLine + 1) % 1093outf.write('</routes>\n')9495print("loaded %s vehicles and wrote %s" % (seenVehs, writtenVehs))969798if __name__ == "__main__":99if not main(get_options()):100sys.exit(1)101102103