Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/vehLanes.py
169674 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file vehLanes.py
16
# @author Jakob Erdmann
17
# @date 2012-11-20
18
19
from __future__ import absolute_import
20
import os
21
import sys
22
from collections import defaultdict
23
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..'))
24
from sumolib.output import parse # noqa
25
from sumolib.options import ArgumentParser # noqa
26
27
28
def parse_args():
29
optParser = ArgumentParser()
30
optParser.add_argument("netstate", help="Netstate Dump File")
31
optParser.add_argument("out", help="Output file")
32
return optParser.parse_args()
33
34
35
def trackLanes(netstate, out):
36
# veh_id -> values
37
laneTimes = defaultdict(list)
38
laneChanges = defaultdict(lambda: 0)
39
lastEdge = defaultdict(lambda: None)
40
arrivals = {}
41
running = set()
42
43
with open(out, 'w') as f:
44
f.write("<vehLanes>\n")
45
46
for timestep in parse(netstate, 'timestep'):
47
seen = set()
48
if timestep.edge is not None:
49
for edge in timestep.edge:
50
if edge.lane is not None:
51
for lane in edge.lane:
52
if lane.vehicle is not None:
53
for vehicle in lane.vehicle:
54
seen.add(vehicle.id)
55
if vehicle.id not in running or laneTimes[vehicle.id][-1][1] != lane.id:
56
laneTimes[vehicle.id].append(
57
(timestep.time, lane.id))
58
running.add(vehicle.id)
59
if lastEdge[vehicle.id] == edge.id:
60
laneChanges[vehicle.id] += 1
61
lastEdge[vehicle.id] = edge.id
62
for veh_id in running:
63
if veh_id not in seen:
64
arrivals[veh_id] = timestep.time
65
running = running - set(arrivals.keys())
66
67
for veh_id, lt in laneTimes.items():
68
f.write(' <vehicle id="%s" laneTimes="%s" arrival="%s" laneChanges="%s"/>\n' % (
69
veh_id,
70
' '.join(['%s,%s' % (t, l) for t, l in lt]),
71
arrivals.get(veh_id),
72
laneChanges[veh_id]))
73
f.write("</vehLanes>\n")
74
75
76
if __name__ == "__main__":
77
options = parse_args()
78
trackLanes(options.netstate, options.out)
79
80