Tools/Sumolib
generated on 2014-06-12 00:24:11.136666 from the wiki page for Tools/Sumolib for SUMO 0.21.0
sumolib is a set of python modules for working with sumo networks, simulation output and other simulation artefacts. For a detailed list of available functions see the pydoc generated documentation. You can [browse the code here].
importing sumolib in a script
To use the library, the <SUMO_HOME>/tools directory must be on the python load path. This is typically done with a stanza like this:
import os, sys
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
usage examples
# import the library import sumolib
# parse the net
net = sumolib.net.readNet('myNet.net.xml')
# retrieve the coordinate of a node base on its ID
print net.getNode('myNodeID').getCoord()
# retrieve the successor node ID of an edge
nextNodeID = net.getEdge('myEdgeID').getToNode().getID()
# compute the average edge length in a plain xml edge file
lengthSum = 0.0
edgeCount = 0
for edge in sumolib.output.parse('myNet.edg.xml', ['edge']):
lengthSum += float(edge.length)
edgeCount += 1
avgLength = lengthSum / edgeCount
# compute the median edge length in a plain xml edge file using the Statistics module edgeStats = sumolib.miscutils.Statistics("edge lengths") for edge in sumolib.output.parse('myNet.edg.xml', ['edge']): edgeStats.add(float(edge.length)) avgLength = edgeStats.median()
This page was last modified on 11 April 2014, at 12:54.