/usr/share/sumo/tools/net/0123to0130.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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | #!/usr/bin/python
"""
@file 0123to0130.py
@author Daniel Krajzewicz
@author Jakob Erdmann
@author Michael Behrisch
@date 2007
@version $Id: 0123to0130.py 11671 2012-01-07 20:14:30Z behrisch $
Changes xml network files from version 0.12.3 to version 0.13.0.
SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
Copyright (C) 2009-2012 DLR (http://www.dlr.de/) and contributors
All rights reserved
"""
import os, string, sys, glob
import xml
from xml.sax import parse, handler
from optparse import OptionParser
from collections import defaultdict
# attributes sorting lists
a = {}
a['net'] = ('version', )
a['edge'] = ( 'id', 'from', 'to', 'name', 'priority', 'type', 'function', 'spread_type', 'shape' )
a['lane'] = ( 'id', 'index', 'vclasses', 'allow', 'disallow', 'maxspeed', 'maxSpeed', 'length', 'endOffset', 'width', 'shape' )
a['junction'] = ( 'id', 'type', 'x', 'y', 'incLanes', 'intLanes', 'shape' )
a['logicitem'] = ('response', 'foes', 'cont' )
a['succlane'] = ('via', 'tl', 'linkno', 'dir', 'state' )
a['connection'] = ('from', 'to', 'fromLane', 'toLane', 'via', 'tl', 'linkIdx', 'dir', 'state' )
a['row-logic'] = a['ROWLogic'] = ( 'id', 'requestSize' )
a['tl-logic'] = a['tlLogic'] = ( 'id', 'type', 'programID', 'offset' )
a['location'] = ( 'netOffset', 'convBoundary', 'origBoundary', 'projParameter' )
a['phase'] = ( 'duration', 'state', 'min_dur', 'max_dur' )
a['district'] = ( 'id', 'shape', 'edges' )
a['dsink'] = ( 'id', 'weight' )
a['dsource'] = ( 'id', 'weight' )
a['taz'] = ( 'id', 'shape', 'edges' )
a['tazSink'] = ( 'id', 'weight' )
a['tazSource'] = ( 'id', 'weight' )
a['roundabout'] = ( 'nodes', 'dummy' )
a['request'] = ( 'index', 'response', 'foes', 'cont' )
a['succ'] = ( 'edge', 'lane', 'junction')
# attributes which are optional
b = defaultdict(dict)
b['edge']['type'] = ''
b['edge']['function'] = 'normal'
b['succlane']['tl'] = ''
b['succlane']['linkno'] = ''
b['succlane']['linkIndex'] = ''
b['junction']['shape'] = ''
# elements which are single (not using opening/closing tag)
SINGLE = ( 'roundabout', 'logicitem', 'phase', 'succlane', 'dsource', 'dsink', 'location',
'lane', 'timed_event', 'connection', 'request' )
# remove these
REMOVED = ( 'lanes', 'logic', 'succ', 'row-logic', 'ROWLogic', 'logicitem')
# renamed elements
RENAMED_TAGS = {'tl-logic': 'tlLogic',
'row-logic': 'ROWLogic',
'district':'taz',
'dsource':'tazSource',
'dsink':'tazSink',
'succlane':'connection'}
renamedAttrs = {'min_dur': 'minDur',
'max_dur': 'maxDur',
'spread_type': 'spreadType',
'maxspeed':'speed',
'maxSpeed':'speed',
'linkIdx':'linkIndex',
'linkno':'linkIndex'}
renamedValues = {'state': {'t': 'o'}, 'type':{'DEAD_END': 'dead_end'} }
def getBegin(file):
fd = open(file)
content = "";
for line in fd:
if '<net' in line or '<districts' in line or '<add' in line:
fd.close()
return content
if '<?xml version' in line:
continue
content = content + line
fd.close()
return ""
class NetConverter(handler.ContentHandler):
def __init__(self, outFileName, begin, has_no_destination):
self._out = open(outFileName, "w")
self._out.write('<?xml version="1.0" encoding="iso-8859-1"?>\n')
self._out.write(begin)
self._tree = []
self._content = ""
self._am_parsing_rowlogic = None # parsing old element
self._logicitems = defaultdict(list) # maps junction ids to items
self._laneCount = 0
self._has_no_destination = has_no_destination
def isSingle(self, name):
if name in SINGLE:
return True
if name == "junction" and self._single_junction:
return True
return False
def remove(self, name):
if name == "succ" and self._has_no_destination:
return False
if name in REMOVED:
return True
else:
return False
def rename(self, name):
if name == "succlane" and self._has_no_destination:
return False
if name in RENAMED_TAGS:
return True
else:
return False
def checkWrite(self, what):
self._out.write(what.encode('iso-8859-1'))
def indent(self):
self._out.write(" " * (4*len(self._tree)))
def endDocument(self):
self.checkWrite("\n")
self._out.close()
def startElement(self, name, attrs):
# special preparations
if name == "succ":
self._succ_from = attrs["edge"]
self._succ_fromIdx = attrs["lane"].split('_')[-1]
if name == "row-logic" or name == "ROWLogic":
self._am_parsing_rowlogic = attrs["id"]
if name == "logicitem":
self._logicitems[self._am_parsing_rowlogic].append(attrs)
if name == "edge":
self._laneCount = 0
if name == "junction":
self._single_junction = (attrs["id"][0] == ":"
or attrs["type"] == 'DEAD_END'
or attrs["type"] == 'dead_end')
# skip removed
if self.remove(name):
return
# compress empty lines
if self._content.find("\n\n")>=0:
self.checkWrite("\n")
self._content = ""
# open tag
self.indent()
if self.rename(name):
self.checkWrite("<" + RENAMED_TAGS[name])
else:
self.checkWrite("<" + name)
# special attribute handling
if name == "succlane":
if self._has_no_destination:
# keep original
self.checkWrite(' lane="%s"' % attrs["lane"])
else:
# upgrade
self.checkWrite(' from="%s"' % self._succ_from)
sepIndex = attrs['lane'].rindex('_')
toEdge = attrs['lane'][:sepIndex]
toIdx = attrs['lane'][sepIndex+1:]
self.checkWrite(' to="%s"' % toEdge)
self.checkWrite(' fromLane="%s"' % self._succ_fromIdx)
self.checkWrite(' toLane="%s"' % toIdx)
# write attributes
if name == "net":
self.checkWrite(' version="0.13" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.sf.net/xsd/net_file.xsd"')
else:
if name in a:
for key in a[name]:
val = None
if key in renamedAttrs and attrs.has_key(renamedAttrs[key]):
key = renamedAttrs[key]
if attrs.has_key(key):
val = attrs[key]
if key in renamedValues:
val = renamedValues[key].get(val, val)
if name == "succlane" and (key == "linkno" or key == "linkIndex") and attrs["tl"] == '':
val = ''
if name not in b or key not in b[name] or val!=b[name][key]:
self.checkWrite(' ' + renamedAttrs.get(key, key) + '="%s"' % val)
elif name == "lane" and key == "index":
self.checkWrite(' %s="%s"' % (key,self._laneCount))
self._laneCount += 1
elif name == "connection" and key == "fromLane":
self.checkWrite(' fromLane="%s" toLane="%s"' % tuple(attrs["lane"].split(":")))
# close tag
if self.isSingle(name):
self.checkWrite("/>\n")
else:
self.checkWrite(">\n")
self._tree.append(name)
# transfer items from removed element ROWLogic
if name == "junction":
for logicitem_attrs in self._logicitems[attrs["id"]]:
self.indent()
self.checkWrite("<request")
self.checkWrite(' index="%s"' % logicitem_attrs["request"])
for key in a["logicitem"]:
if logicitem_attrs.has_key(key): # cont is optional
self.checkWrite(' ' + key + '="%s"' % logicitem_attrs[key])
self.checkWrite("/>\n")
def endElement(self, name):
# special preparations
if name == "row-logic" or name == "ROWLogic":
self._am_parsing_rowlogic = None
# skip removed
if self.remove(name):
return
# optionaly write closing tag
self._tree.pop()
if name=="net":
self.checkWrite("\n")
if not self.isSingle(name):
self.indent()
if self.rename(name):
self.checkWrite("</" + RENAMED_TAGS[name] + ">")
else:
self.checkWrite("</" + name + ">")
if name!="net":
self.checkWrite("\n")
def characters(self, content):
self._content += content
def processingInstruction(self, target, data):
self.checkWrite('<?%s %s?>' % (target, data))
def changeFile(fname):
if options.verbose:
print "Patching " + fname + " ..."
if (("_deprecated_" in fname and not "net.netconvert" in fname) or
(os.path.join('tools','net','0') in fname)):
print "Skipping file (looks like intentionally deprecated input): " + fname
return
has_no_destination = False
if "SUMO_NO_DESTINATION" in open(fname).read():
print "Partial conversion (cannot convert SUMO_NO_DESTINATION): " + fname
has_no_destination = True
net = NetConverter(fname+".chg", getBegin(fname), has_no_destination)
try:
parse(fname, net)
except xml.sax._exceptions.SAXParseException:
print "Could not parse '%s' maybe it contains non-ascii chars?" % fname
if options.inplace:
os.remove(fname)
os.rename(fname+".chg", fname)
def changeEdgeFile(fname):
if options.verbose:
print "Patching " + fname + " ..."
out = open(fname+".chg", 'w')
for line in open(fname):
if "<lane" in line:
line = line.replace(" id", " index")
if "<edge" in line:
line = line.replace(" fromnode", " from").replace(" tonode", " to")
line = line.replace(" spread_type", " spreadType").replace(" maxspeed", " maxSpeed")
line = line.replace(" nolanes", " numLanes")
out.write(line)
out.close()
if options.inplace:
os.remove(fname)
os.rename(fname+".chg", fname)
def changeConnectionFile(fname):
if options.verbose:
print "Patching " + fname + " ..."
out = open(fname+".chg", 'w')
for line in open(fname):
if "<connection" in line and "lane" in line:
line = line.replace(" lane", " fromLane")
for i in range(10):
line = line.replace(':%s"' % i, '" toLane="%s"' % i)
out.write(line)
out.close()
if options.inplace:
os.remove(fname)
os.rename(fname+".chg", fname)
def walkDir(srcRoot):
for root, dirs, files in os.walk(srcRoot):
for name in files:
if name.endswith(".net.xml") or name in ["net.netconvert", "net.netgen",
"tls.scenario", "net.scenario"]:
changeFile(os.path.join(root, name))
if options.edges and name.endswith(".edg.xml"):
changeEdgeFile(os.path.join(root, name))
elif options.connections and name.endswith(".con.xml"):
changeConnectionFile(os.path.join(root, name))
for ignoreDir in ['.svn', 'foreign']:
if ignoreDir in dirs:
dirs.remove(ignoreDir)
optParser = OptionParser()
optParser.add_option("-v", "--verbose", action="store_true",
default=False, help="tell me what you are doing")
optParser.add_option("-i", "--inplace", action="store_true",
default=False, help="replace original files")
optParser.add_option("-e", "--edges", action="store_true",
default=False, help="include edge XML files")
optParser.add_option("-c", "--connections", action="store_true",
default=False, help="include connection XML files")
(options, args) = optParser.parse_args()
if len(args) == 0:
print "Usage: " + sys.argv[0] + " <net>+"
sys.exit()
for arg in args:
for fname in glob.glob(arg):
if os.path.isdir(fname):
walkDir(fname)
else:
if options.edges and fname.endswith(".edg.xml"):
changeEdgeFile(fname)
elif options.connections and fname.endswith(".con.xml"):
changeConnectionFile(fname)
else:
changeFile(fname)
|