Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/net/patchVClasses.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 patchVClasses.py
15
# @author Jakob Erdmann
16
# @date 2024-04-30
17
18
"""
19
Removes vclasses from the network so it can be loaded with older versions of SUMO
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("-d", "--delete-classes", dest="deleteClasses",
39
help=("list of classes to remove from both allow and disallow (for downward compatibility), " +
40
"default: subway,cable_car,aircraft,wheelchair,scooter,drone,container"))
41
optParser.add_option("-D", "--disallow", dest="disallow",
42
help="disallow the list of classes (remove from 'allow' or add to 'disallow'")
43
options = optParser.parse_args()
44
45
if options.deleteClasses and options.disallow:
46
raise ValueError("Only one of the options --delete-classes and --disallow may be set")
47
elif options.deleteClasses is None and options.disallow is None:
48
options.deleteClasses = "subway,cable_car,aircraft,wheelchair,scooter,drone,container"
49
50
if not options.outfile:
51
options.outfile = "patched." + options.net
52
53
return options
54
55
56
def main(options):
57
net = sumolib.net.readNet(options.net)
58
if options.disallow:
59
disallowVClasses(options, net)
60
else:
61
removeVClasses(options)
62
63
64
def removeVClasses(options):
65
classes = options.deleteClasses.split(',')
66
with sumolib.openz(options.outfile, 'w') as outf, sumolib.openz(options.net) as inf:
67
for line in inf:
68
for c in classes:
69
line = line.replace(c + " ", "")
70
line = line.replace(" " + c, "")
71
line = line.replace('allow="%s"' % c, 'disallow="all"')
72
line = line.replace('disallow="%s"' % c, "")
73
outf.write(line)
74
75
76
def disallowVClasses(options, net):
77
classes = set(options.disallow.split(','))
78
prefix = options.net
79
if prefix.endswith(".net.xml.gz"):
80
prefix = prefix[:-11]
81
elif prefix.endswith(".net.xml"):
82
prefix = prefix[:-8]
83
edgePatch = prefix + ".patch.edg.xml"
84
85
with open(edgePatch, 'w') as outfe:
86
sumolib.writeXMLHeader(outfe, "$Id$", "edges", schemaPath="edgediff_file.xsd", options=options)
87
for e in net.getEdges():
88
writeLanes = []
89
for lane in e.getLanes():
90
allowed = set(lane.getPermissions()).difference(classes)
91
if allowed != lane.getPermissions():
92
allowed = allowed.difference(sumolib.net.lane.SUMO_VEHICLE_CLASSES_DEPRECATED)
93
writeLanes.append((lane.getIndex(), " ".join(allowed)))
94
if writeLanes:
95
outfe.write(' <edge id="%s">\n' % e.getID())
96
for index, allow in writeLanes:
97
outfe.write(' <lane index="%s" allow="%s"/>\n' % (index, allow))
98
outfe.write(' </edge>\n')
99
outfe.write("</edges>\n")
100
101
with sumolib.openz(options.outfile, 'w') as outf, sumolib.openz(options.net) as inf:
102
for line in inf:
103
for c in classes:
104
line = line.replace(c + " ", "")
105
line = line.replace(" " + c, "")
106
outf.write(line)
107
108
NETCONVERT = sumolib.checkBinary('netconvert')
109
subprocess.call([NETCONVERT,
110
'-s', options.net,
111
'-e', edgePatch,
112
'-o', options.outfile])
113
114
115
if __name__ == "__main__":
116
main(parse_args())
117
118