Tools/Sumolib

generated on 2015-09-02 00:02:07.970150 from the wiki page for Tools/Sumolib for SUMO 0.24.0

sumolib is a set of python modules for working with sumo networks, simulation output and other simulation artifacts. 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()
 # locate nearby edges based on the geo-coordinate
 # (requires module pyproj to be installed)
 radius = 0.1
 x, y = net.convertLonLat(lon, lat)
 edges = net.getNeighboringEdges(x, y, radius)
 # pick the closest edge
 if len(edges) > 0:
   distancesAndEdges = sorted([(dist, edge) for edge, dist in edges])
   dist, closestEdge = distancesAndEdges[0]

This page was last modified on 26 November 2014, at 21:11.