Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/route2trips.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
4
# This program and the accompanying materials are made available under the
5
# terms of the Eclipse Public License 2.0 which is available at
6
# https://www.eclipse.org/legal/epl-2.0/
7
# This Source Code may also be made available under the following Secondary
8
# Licenses when the conditions for such availability set forth in the Eclipse
9
# Public License 2.0 are satisfied: GNU General Public License, version 2
10
# or later which is available at
11
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
14
# @file route2trips.py
15
# @author Michael Behrisch
16
# @author Daniel Krajzewicz
17
# @date 2008-03-19
18
19
"""
20
This script converts SUMO routes back into SUMO trips which serve
21
as input to one of the routing applications.
22
It reads the routes from a file given as first parameter
23
and outputs the trips to stdout.
24
"""
25
from __future__ import print_function
26
from __future__ import absolute_import
27
import os
28
import sys
29
30
from xml.sax import parse, handler
31
32
SUMO_HOME = os.environ.get('SUMO_HOME',
33
os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..'))
34
sys.path.append(os.path.join(SUMO_HOME, 'tools'))
35
import sumolib # noqa
36
37
38
class RouteReader(handler.ContentHandler):
39
40
def __init__(self, attrList, outfile, vias, calledBy=""):
41
self._vType = ''
42
self._vID = ''
43
self._vDepart = 0
44
self._routeID = ''
45
self._routeString = ''
46
self._routes = {}
47
self._attrList = attrList
48
self._vehicleAttrs = None
49
self.outfile = outfile
50
self.vias = vias
51
self.calledBy = calledBy
52
53
def startElement(self, name, attrs):
54
if name == 'vehicle':
55
self._vehicleAttrs = dict(attrs)
56
self._vID = attrs['id']
57
if 'route' in attrs:
58
self._routeString = self._routes[attrs['route']]
59
del self._vehicleAttrs['route']
60
elif name == 'route':
61
if not self._vID:
62
self._routeID = attrs['id']
63
self._routeString = ''
64
if 'edges' in attrs:
65
self._routeString = attrs['edges']
66
elif name == 'vType':
67
# XXX does not handle child elements (for carFollowing, the next case copies the input)
68
print(' <vType %s>' % (' '.join(['%s="%s"' % (
69
key, value) for key, value in sorted(dict(attrs).items())])),
70
file=self.outfile)
71
elif name[0:12] == 'carFollowing':
72
print(' <%s %s />' % (name, ' '.join(['%s="%s"' % (
73
key, value) for key, value in sorted(dict(attrs).items())])),
74
file=self.outfile)
75
elif name == 'routes':
76
sumolib.writeXMLHeader(
77
self.outfile,
78
"$Id$%s" % self.calledBy, # noqa
79
"routes")
80
81
def endElement(self, name):
82
if name == 'route':
83
if not self._vID:
84
self._routes[self._routeID] = self._routeString
85
self._routeString = ''
86
self._routeID = ''
87
elif name == 'vehicle':
88
edges = self._routeString.split()
89
self._vehicleAttrs["from"] = edges[0]
90
self._vehicleAttrs["to"] = edges[-1]
91
via = self.vias.get(self._vID, "")
92
if self._attrList:
93
print(' <trip %s%s/>' % (' '.join(['%s="%s"' % (key,
94
self._vehicleAttrs[key]) for key in self._attrList]),
95
via),
96
file=self.outfile)
97
else:
98
del self._vehicleAttrs['id']
99
items = sorted(['%s="%s"' % (key, val)
100
for key, val in self._vehicleAttrs.items()])
101
print(' <trip id="%s" %s%s/>' % (self._vID, ' '.join(items), via),
102
file=self.outfile)
103
self._vID = ''
104
self._routeString = ''
105
elif name == 'routes':
106
print("</routes>", file=self.outfile)
107
elif name == 'vType':
108
print(" </vType>", file=self.outfile)
109
110
def characters(self, content):
111
self._routeString += content
112
113
114
def main(argv, outfile=None, vias={}, calledBy=""):
115
routefile = argv[0]
116
attrList = argv[1:]
117
if outfile is None:
118
parse(routefile, RouteReader(attrList, sys.stdout, vias, calledBy))
119
else:
120
with open(outfile, 'w') as outf:
121
parse(routefile, RouteReader(attrList, outf, vias, calledBy))
122
123
124
if __name__ == "__main__":
125
if len(sys.argv) < 2:
126
sys.exit("Usage: " + sys.argv[0] + " <routes> [<attribute>*]")
127
main(sys.argv[1:])
128
129