Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/output/countLaneChanges.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) 2014-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 countLaneChanges.py
16
# @author Jakob Erdmann
17
# @date 2014-09-16
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
import os
22
import sys
23
from xml.sax import parse, handler
24
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
25
from sumolib.options import ArgumentParser # noqa
26
27
28
class DumpReader(handler.ContentHandler):
29
30
"""Reads the dump file"""
31
32
def __init__(self):
33
self._edge = None
34
self._lane = None
35
self.vehicles = {} # vehicle -> last edge id
36
self.changes = 0
37
38
def startElement(self, name, attrs):
39
if name == 'edge':
40
self._edge = attrs['id']
41
elif name == 'lane':
42
self._lane = attrs['id']
43
elif name == 'vehicle':
44
veh = attrs['id']
45
prevEdge, prevLane = self.vehicles.get(veh, (None, None))
46
if self._edge == prevEdge and self._lane != prevLane:
47
self.changes += 1
48
self.vehicles[veh] = (self._edge, self._lane)
49
50
51
def parse_args():
52
optParser = ArgumentParser()
53
optParser.add_argument("dumpfile", help="dump file path")
54
return optParser.parse_args()
55
56
57
def countLaneChanges(dumpfile):
58
dr = DumpReader()
59
parse(dumpfile, dr)
60
print(dr.changes, dr.changes / float(len(dr.vehicles)))
61
62
63
if __name__ == "__main__":
64
countLaneChanges(parse_args().dumpfile)
65
66