Skip to content

Commit

Permalink
addendum refs #14798
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed May 3, 2024
1 parent f39be16 commit e175a19
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
64 changes: 61 additions & 3 deletions tools/net/patchVClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import sys
import os
import subprocess
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import sumolib # noqa
from sumolib.options import ArgumentParser # noqa
from sumolib.xml import _open


def parse_args():
Expand All @@ -35,24 +37,80 @@ def parse_args():
optParser.add_option("-o", "--output-file", dest="outfile", category="output", type=optParser.net_file,
help="the output network file")
optParser.add_option("-d", "--delete-classes", dest="deleteClasses",
default="subway,cable_car,aircraft,wheelchair,scooter,drone,container",
help="list of classes to remove")
help=("list of classes to remove from both allow and disallow (for downward compatibility), " +
"default: subway,cable_car,aircraft,wheelchair,scooter,drone,container"))
optParser.add_option("-D", "--disallow", dest="disallow",
help="disallow the list of classes (remove from 'allow' or add to 'disallow'")
options = optParser.parse_args()

if options.deleteClasses and options.disallow:
raise ValueError("Only one of the options --delete-classes and --disallow may be set")
elif options.deleteClasses is None and options.disallow is None:
options.deleteClasses = "subway,cable_car,aircraft,wheelchair,scooter,drone,container"

if not options.outfile:
options.outfile = "patched." + options.net

return options


def main(options):
net = sumolib.net.readNet(options.net)
if options.disallow:
disallowVClasses(options, net)
else:
removeVClasses(options, net)

def removeVClasses(options,net):
classes = options.deleteClasses.split(',')
with open(options.outfile, 'w') as outf:
for line in open(options.net):
for line in _open(options.net):
for c in classes:
line = line.replace(c + " ", "")
line = line.replace(" " + c, "")
line = line.replace('allow="%s"' % c, 'disallow="all"')
line = line.replace('disallow="%s"' % c, "")
outf.write(line)


def disallowVClasses(options, net):
classes = set(options.disallow.split(','))
prefix = options.net
if prefix.endswith(".net.xml.gz"):
prefix = prefix[:-11]
elif prefix.endswith(".net.xml"):
prefix = prefix[:-8]
edgePatch = prefix + ".patch.edg.xml"

with open(edgePatch, 'w') as outfe:
sumolib.writeXMLHeader(outfe, "$Id$", "edges", schemaPath="edgediff_file.xsd", options=options)
for e in net.getEdges():
writeLanes = []
for lane in e.getLanes():
allowed = set(lane.getPermissions()).difference(classes)
if allowed != lane.getPermissions():
allowed = allowed.difference(sumolib.net.lane.SUMO_VEHICLE_CLASSES_DEPRECATED)
writeLanes.append((lane.getIndex(), " ".join(allowed)))
if writeLanes:
outfe.write(' <edge id="%s">\n' % e.getID())
for index, allow in writeLanes:
outfe.write(' <lane index="%s" allow="%s"/>\n' % (index, allow))
outfe.write(' </edge>\n')
outfe.write("</edges>\n")

with open(options.outfile, 'w') as outf:
for line in _open(options.net):
for c in classes:
line = line.replace(c + " ", "")
line = line.replace(" " + c, "")
outf.write(line)

NETCONVERT = sumolib.checkBinary('netconvert')
subprocess.call([NETCONVERT,
'-s', options.net,
'-e', edgePatch,
'-o', options.outfile])


if __name__ == "__main__":
main(parse_args())
10 changes: 10 additions & 0 deletions tools/sumolib/net/lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
"custom1",
"custom2"])

SUMO_VEHICLE_CLASSES_DEPRECATED = set([
"public_emergency",
"public_authority",
"public_army",
"public_transport",
"transport",
"lightrail",
"cityrail",
"rail_slow",
"rail_fast"])

def is_vehicle_class(s):
return s in SUMO_VEHICLE_CLASSES
Expand Down

0 comments on commit e175a19

Please sign in to comment.