Path: blob/main/tools/import/vissim/vissim_parseBusStops.py
169679 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2009-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 vissim_parseBusStops.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @date 2009-05-271718"""1920Parses bus stops and bus routes given in the Vissim file (first parameter).2122The read bus lines are saved as <OUTPUT_PREFIX>_busses.rou.xml23The read routes are saved as <OUTPUT_PREFIX>_stops.add.xml2425(Starting?) edges of the route may be renamed by setting them within "edgemap"26variable (see below).27"""28from __future__ import absolute_import29from __future__ import print_function30import os31import sys32if 'SUMO_HOME' in os.environ:33sys.path.append(os.path.join(os.environ['SUMO_HOME'], 'tools'))34import sumolib # noqa3536edgemap = {}37edgemap["203"] = "203[0]"38edgemap["78"] = "78[0]"39edgemap["77"] = "77[0]"40edgemap["203"] = "203[0]"414243def getName(vals, beg):44name = vals[beg]45while name.count('"') != 2:46beg = beg + 147name = name + " " + vals[beg]48return name.replace('"', '')495051def parseBusStop(bs):52vals = bs.split()53id = int(vals[1])54i = vals.index("NAME")55name = getName(vals, i + 1)56i = vals.index("STRECKE", i)57strecke = vals[i + 1]58i = vals.index("SPUR", i)59spur = int(vals[i + 1]) - 160i = vals.index("BEI", i)61von = float(vals[i + 1])62i = vals.index("LAENGE", i)63bis = von + float(vals[i + 1])64return (id, name, strecke, spur, von, bis)656667def parseBusRoute(br, stops):68vals = br.split()69id = vals[1]70i = vals.index("NAME")71name = getName(vals, i + 1)72i = vals.index("EINFAHRT", i)73startKante = vals[i + 2]74i = vals.index("ZIEL", i)75ziel = vals[i + 2]76zeiten = []77endI = vals.index("HALTESTELLE", i)78i = vals.index("STARTZEITEN", i)79i = i + 180while i > 0 and i < endI:81zeiten.append(int(float(vals[i])))82i = i + 583stops = []84while i > 0 and i < len(vals):85try:86i = vals.index("HALTESTELLE", i)87i = i + 188stops.insert(0, int(vals[i]))89except Exception:90i = len(vals) + 191return (id, name, startKante, ziel, zeiten, stops)929394def sorter(idx):95def t(i, j):96if i[idx] < j[idx]:97return -198elif i[idx] > j[idx]:99return 1100else:101return 0102103104# MAIN105if __name__ == '__main__':106op = sumolib.options.ArgumentParser()107op.add_option("vissimNet", category="input", type=op.file,108help="provide the vissim file path")109op.add_option("outputPrefix", category="input",110help="provide the output prefix")111args = op.parse_args()112113print("Parsing Vissim input...")114fd = open(args.vissimNet)115haveStop = False116haveRoute = False117currentItem = ""118stopsL = []119routesL = []120for line in fd:121# process bus stops ("HALTESTELLE")122if line.find("HALTESTELLE") == 0:123if haveStop:124stopsL.append(" ".join(currentItem.split()))125haveStop = True126currentItem = ""127elif line[0] != ' ':128if haveStop:129stopsL.append(" ".join(currentItem.split()))130haveStop = False131if haveStop:132currentItem = currentItem + line133# process bus routes ("LINIE")134if line.find(" LINIE") == 0:135if haveRoute:136routesL.append(" ".join(currentItem.split()))137haveRoute = True138currentItem = ""139elif len(line) > 2 and line[0] != ' ' and line[1] != ' ':140if haveRoute:141routesL.append(" ".join(currentItem.split()))142haveRoute = False143if haveRoute:144currentItem = currentItem + line145146# build stops map147sm = {}148for bs in stopsL:149(id, name, strecke, spur, von, bis) = parseBusStop(bs)150sm[id] = (id, name, strecke, spur, von, bis)151152# process bus routes153# build departure times154emissions = []155for br in routesL:156(pid, name, startKante, ziel, zeiten, stops) = parseBusRoute(br, sm)157edges = []158edges.append(startKante)159for s in stops:160if sm[s][2] not in edges:161edges.append(sm[s][2])162if ziel not in edges:163edges.append(ziel)164for i in range(0, len(edges)):165if edges[i] in edgemap:166edges[i] = edgemap[edges[i]]167for t in zeiten:168id = str(pid) + "_" + str(t)169emissions.append((int(t), id, edges, stops))170171# sort emissions172print("Sorting routes...")173emissions.sort(sorter(0))174# write routes175print("Writing bus routes...")176fdo = open(args.outputPrefix + "_busses.rou.xml", "w")177fdo.write("<routes>\n")178for emission in emissions:179if len(emission[2]) < 2:180continue181fdo.write(' <vehicle id="' + emission[1] + '" depart="' + str(emission[1820]) + '" type="bus" color="0,1,0"><route>' + " ".join(emission[2]) + '</route>\n')183for s in emission[3]:184fdo.write(185' <stop bus_stop="' + str(s) + '_0" duration="20"/>\n')186fdo.write(' </vehicle>\n')187fdo.write("</routes>\n")188189# process bus stops190print("Writing bus stops...")191fdo = open(args.outputPrefix + "_stops.add.xml", "w")192fdo.write("<add>\n")193for bs in stopsL:194(id, name, strecke, spur, von, bis) = parseBusStop(bs)195fdo.write(' <busStop id="' + str(id) + '" lane="' + strecke + "_" +196str(spur) + '" from="' + str(von) + '" to="' + str(bis) + '" lines="--"/>\n')197fdo.write("</add>\n")198fdo.close()199200201