Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/findAllRoutes.py
169660 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2010-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 findAllRoutes.py
15
# @author Michael Behrisch
16
# @date 2020-11-02
17
18
"""
19
determine all possible routes between source and destination edges
20
"""
21
from __future__ import absolute_import
22
from __future__ import print_function
23
24
import sys
25
import sumolib
26
27
28
def get_options(args=None):
29
argParser = sumolib.options.ArgumentParser()
30
argParser.add_argument("-n", "--net-file", category="input", help="the SUMO net filename")
31
argParser.add_argument("-o", "--output-file", category="output", help="the route output filename")
32
argParser.add_argument("-s", "--source-edges", category="input", help="comma separated list of source edge ids")
33
argParser.add_argument("-t", "--target-edges", category="input", help="comma separated list of target edge ids")
34
options = argParser.parse_args(args=args)
35
if options.net_file is None or options.output_file is None:
36
argParser.print_help()
37
sys.exit()
38
return options
39
40
41
def main(options):
42
net = sumolib.net.readNet(options.net_file)
43
if options.source_edges is None:
44
stubs = [[e] for e in net.getEdges()]
45
else:
46
stubs = [[net.getEdge(e)] for e in options.source_edges.split(",")]
47
if options.target_edges is None:
48
targets = set(net.getEdges())
49
else:
50
targets = set([net.getEdge(e) for e in options.target_edges.split(",")])
51
with open(options.output_file, 'w') as outf:
52
sumolib.xml.writeHeader(outf, root="routes")
53
idx = 0
54
while stubs:
55
s = stubs.pop(0)
56
if s[-1] in targets:
57
print(' <route id="%s" edges="%s"/>' % (idx, " ".join([e.getID() for e in s])), file=outf)
58
idx += 1
59
else:
60
seen = set(s)
61
for edge in sorted(s[-1].getOutgoing(), key=lambda e: e.getID()):
62
if edge not in seen:
63
stubs.append(s + [edge])
64
outf.write('</routes>\n')
65
66
67
if __name__ == "__main__":
68
main(get_options())
69
70