/usr/share/sumo/tools/route/routecheck.py is in sumo-tools 0.15.0~dfsg-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | #!/usr/bin/python
"""
@file routecheck.py
@author Michael Behrisch
@author Jakob Erdmann
@author Yun-Pang Wang
@date 2007-03-09
@version $Id: routecheck.py 11671 2012-01-07 20:14:30Z behrisch $
This script does simple checks for the routes on a given network.
Warnings will be issued if there is an unknown edge in the route,
if the route is disconnected,
or if the route definition does not use the edges attribute.
If one specifies -f or --fix all route files will be fixed
(if possible). At the moment this means adding an intermediate edge
if just one link is missing in a disconnected route, or adding an edges
attribute if it is missing.
All changes are documented within the output file which has the same name
as the input file with an additional .fixed suffix.
If the route file uses the old format (<route>edge1 edge2</route>)
this is changed without adding comments. The same is true for camelCase
changes of attributes.
SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
Copyright (C) 2007-2012 DLR (http://www.dlr.de/) and contributors
All rights reserved
"""
import os, string, sys, StringIO
from xml.sax import saxutils, make_parser, handler
from optparse import OptionParser
from collections import defaultdict
camelCase = {"departlane": "departLane",
"departpos": "departPos",
"departspeed": "departSpeed",
"arrivallane": "arrivalLane",
"arrivalpos": "arrivalPos",
"arrivalspeed": "arrivalSpeed",
"maxspeed": "maxSpeed",
"bus_stop": "busStop",
"vclass": "vClass",
"fromtaz": "fromTaz",
"totaz": "toTaz",
"no": "number",
"vtype": "vType",
"vtypeDistribution": "vTypeDistribution",
"tripdef": "trip"}
deletedKeys = defaultdict(list)
deletedKeys["route"] = ["edges", "multi_ref"]
class NetReader(handler.ContentHandler):
def __init__(self):
self._nb = {}
def startElement(self, name, attrs):
if name == 'edge' and (not attrs.has_key('function') or attrs['function'] != 'internal'):
self._nb[attrs['id']] = set()
elif name == 'connection':
if attrs['from'] in self._nb and attrs['to'] in self._nb:
self._nb[attrs['from']].add(attrs['to'])
def hasEdge(self, edge):
return edge in self._nb
def isNeighbor(self, orig, dest):
return dest in self._nb[orig]
def getIntermediateEdge(self, orig, dest):
for inter in self._nb[orig]:
if dest in self._nb[inter]:
return inter
return ''
class RouteReader(handler.ContentHandler):
def __init__(self, net, outFileName):
self._vID = ''
self._routeID = ''
self._routeString = ''
self._addedString = ''
self._net = net
if outFileName:
self._out = open(outFileName, 'w')
else:
self._out = None
self._fileOut = None
self._isRouteValid = True
self._changed = False
def startDocument(self):
if self._out:
print >> self._out, '<?xml version="1.0"?>'
def endDocument(self):
if self._out:
self._out.close()
if not self._changed:
os.remove(self._out.name)
def condOutputRedirect(self):
if self._out and not self._fileOut:
self._fileOut = self._out
self._out = StringIO.StringIO()
def endOutputRedirect(self):
if self._fileOut:
if not self._isRouteValid:
self._changed = True
self._fileOut.write("<!-- ")
self._fileOut.write(self._out.getvalue())
if not self._isRouteValid:
self._fileOut.write(" -->")
if self._addedString != '':
self._fileOut.write("<!-- added edges: " + self._addedString + "-->")
self._addedString = ''
self._out.close()
self._out = self._fileOut
self._fileOut = None
def startElement(self, name, attrs):
if name == 'vehicle' and not attrs.has_key('route'):
self.condOutputRedirect()
self._vID = attrs['id']
if name == 'route':
self.condOutputRedirect()
if attrs.has_key('id'):
self._routeID = attrs['id']
else:
self._routeID = "for vehicle " + self._vID
self._routeString = ''
if attrs.has_key('edges'):
self._routeString = attrs['edges']
else:
self._changed = True
print "Warning: No edges attribute in route " + self._routeID
elif self._routeID:
print "Warning: This script does not handle nested '%s' elements properly." % name
if self._out:
if name in camelCase:
name = camelCase[name]
self._changed = True
self._out.write('<' + name)
if options.fix_length and attrs.has_key('length') and not attrs.has_key('minGap'):
length = float(attrs["length"])
minGap = 2.5
if attrs.has_key('guiOffset'):
minGap = float(attrs["guiOffset"])
attrs = dict(attrs)
attrs["length"] = str(length - minGap)
attrs["minGap"] = str(minGap)
self._changed = True
for (key, value) in attrs.items():
if key in camelCase:
key = camelCase[key]
self._changed = True
if not key in deletedKeys[name]:
self._out.write(' %s="%s"' % (key, saxutils.escape(value)))
if name != 'route':
if name in ["ride", "stop", "walk"]:
self._out.write('/')
self._out.write('>')
def endElement(self, name):
if name in ["ride", "stop", "walk"]:
return
if name == 'route':
self._isRouteValid = self.testRoute()
if self._out:
self._out.write(' edges="%s"/>' % self._routeString)
self._routeID = ''
self._routeString = ''
if self._vID == '':
self.endOutputRedirect()
elif name == 'vehicle' and self._vID != '':
self._vID = ''
if self._out:
self._out.write('</vehicle>')
self.endOutputRedirect()
elif self._out:
self._out.write('</%s>' % camelCase.get(name, name))
def characters(self, content):
if self._routeID != '':
self._routeString += content
elif self._out:
self._out.write(saxutils.escape(content))
def testRoute(self):
if self._routeID != '':
returnValue = True
edgeList = self._routeString.split()
if len(edgeList) == 0:
print "Warning: Route %s is empty" % self._routeID
return False
if net == None:
return True
doConnectivityTest = True
cleanedEdgeList = []
for v in edgeList:
if self._net.hasEdge(v):
cleanedEdgeList.append(v)
else:
print "Warning: Unknown edge " + v + " in route " + self._routeID
returnValue = False
while doConnectivityTest:
doConnectivityTest = False
for i, v in enumerate(cleanedEdgeList):
if i < len(cleanedEdgeList) - 1 and not self._net.isNeighbor(v, cleanedEdgeList[i+1]):
print "Warning: Route " + self._routeID + " disconnected between " + v + " and " + cleanedEdgeList[i+1]
interEdge = self._net.getIntermediateEdge(v, cleanedEdgeList[i+1])
if interEdge != '':
cleanedEdgeList.insert(i+1, interEdge)
self._changed = True
self._addedString += interEdge + " "
self._routeString = string.join(cleanedEdgeList)
doConnectivityTest = True
break
returnValue = False
return returnValue
return False
def ignorableWhitespace(self, content):
if self._out:
self._out.write(content)
def processingInstruction(self, target, data):
if self._out:
self._out.write('<?%s %s?>' % (target, data))
optParser = OptionParser(usage=os.path.basename(__file__) + " [options] <routes>*")
optParser.add_option("-v", "--verbose", action="store_true",
default=False, help="tell me what you are doing")
optParser.add_option("-f", "--fix", action="store_true",
default=False, help="fix errors into '.fixed' file")
optParser.add_option("-l", "--fix-length", action="store_true",
default=False, help="fix vehicle type's length and guiOffset attributes")
optParser.add_option("-i", "--inplace", action="store_true",
default=False, help="replace original files")
optParser.add_option("-n", "--net", help="network to check connectivity")
(options, args) = optParser.parse_args()
if len(args) == 0:
optParser.print_help()
sys.exit()
parser = make_parser()
net = None
if options.net:
net = NetReader()
parser.setContentHandler(net)
parser.parse(options.net)
parser.setContentHandler(RouteReader(net, ''))
if options.fix_length:
deletedKeys["vType"] += ['guiOffset']
for f in args:
ffix = f + '.fixed'
if options.fix:
if options.verbose:
print "fixing " + f
parser.setContentHandler(RouteReader(net, ffix))
else:
if options.verbose:
print "checking " + f
parser.parse(f)
if options.fix and os.path.exists(ffix) and options.inplace:
os.rename(ffix, f)
|