Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/buildFullGraph.py
169673 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2011-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 buildFullGraph.py
15
# @author Jakob Erdmann
16
# @date 2024-05-02
17
18
"""
19
Extend network with direct connection between all edges that permit the given vClass
20
"""
21
from __future__ import absolute_import
22
from __future__ import print_function
23
24
import sys
25
import os
26
import subprocess
27
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
28
import sumolib # noqa
29
from sumolib.options import ArgumentParser # noqa
30
31
32
def parse_args():
33
optParser = ArgumentParser()
34
optParser.add_option("net", category="input", type=optParser.net_file,
35
help="the network to patch")
36
optParser.add_option("-o", "--output-file", dest="outfile", category="output", type=optParser.net_file,
37
help="the output network file")
38
optParser.add_option("--vclass",
39
help="class for which the fully connected graph should be built")
40
optParser.add_option("--speed", type=float, default=100/3.6,
41
help="speed of added edges")
42
optParser.add_option("--numlanes", type=int, default=1,
43
help="number of lanes for added edges")
44
optParser.add_option("--width", type=float,
45
help="width of added edges")
46
options = optParser.parse_args()
47
if not options.outfile:
48
options.outfile = "patched." + options.net
49
50
return options
51
52
53
def main(options):
54
net = sumolib.net.readNet(options.net)
55
baseEdges = [e for e in net.getEdges() if e.allows(options.vclass)]
56
prefix = options.net
57
if prefix.endswith(".net.xml.gz"):
58
prefix = prefix[:-11]
59
elif prefix.endswith(".net.xml"):
60
prefix = prefix[:-8]
61
62
edgePatch = prefix + ".patch.edg.xml"
63
conPatch = prefix + ".patch.con.xml"
64
with open(edgePatch, 'w') as outfe, open(conPatch, 'w') as outfc:
65
sumolib.writeXMLHeader(outfe, "$Id$", "edges", options=options)
66
sumolib.writeXMLHeader(outfc, "$Id$", "connections", options=options)
67
for e1 in baseEdges:
68
for e2 in baseEdges:
69
if e1 != e2 and e2 not in e1.getOutgoing():
70
newEID = "%s_%s" % (e1.getToNode().getID(), e2.getFromNode().getID())
71
width = options.width if options.width is not None else e1.getLanes()[-1].getWidth()
72
outfe.write(' <edge id="%s" from="%s" to="%s" speed="%s" numLanes="%s" width="%s" allow="%s"/>\n' % ( # noqa
73
newEID, e1.getToNode().getID(), e2.getFromNode().getID(),
74
options.speed, options.numlanes, width, options.vclass))
75
outfc.write(' <connection from="%s" to="%s" fromLane="0" toLane="0"/>\n' % (e1.getID(), newEID))
76
outfc.write(' <connection from="%s" to="%s" fromLane="0" toLane="0"/>\n' % (newEID, e2.getID()))
77
78
outfe.write("</edges>\n")
79
outfc.write("</connections>\n")
80
81
NETCONVERT = sumolib.checkBinary('netconvert')
82
subprocess.call([NETCONVERT,
83
'-s', options.net,
84
'-e', edgePatch,
85
'-x', conPatch,
86
'-o', options.outfile])
87
88
89
if __name__ == "__main__":
90
main(parse_args())
91
92