Tools/Sumolib
译者注:这部分的内容比较多,这里只是举例几个简单的例子。并且Sumolib在使用中的用处非常大,很多模块可以大大减少我们的工作量。
sumolib文件下的文件夹列表
sumolib文件加下的py文件
sumolib是一系列处理sumo网路文件、仿真输出和其他仿真文件的python模块。可以查看响应的文档pydoc generated documentation。也可以查看源码browse the code here。
1、导入sumolib到脚本中
为了使用这个库,<SUMO_HOME>/tools目录必须加载到处理路径中。一般的python代码中都有一段代码:
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 environmentvariable 'SUMO_HOME'")
2、使用示例
2.1导入路网,检索节点和道路
# import the library导入库 import sumolib # parse the net解析net net = sumolib.net.readNet('myNet.net.xml') # retrieve the coordinate of a node base on its ID根据节点的ID获得物理位置 print net.getNode('myNodeID').getCoord() # retrieve the successor node ID of an edge获得一条道路的后继节点ID nextNodeID = net.getEdge('myEdgeID').getToNode().getID()
2.2计算平均挥着中间的道路长度
# compute the average length计算平均长度 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 length using the Statistics module #使用Statistics模块计算中间长度 edgeStats = sumolib.miscutils.Statistics("edge lengths") for edge in sumolib.output.parse('myNet.edg.xml', ['edge']): edgeStats.add(float(edge.length)) avgLength = edgeStats.median()
2.3根据物理位置定位临近的道路
# (requires module pyproj to be installed) #需要安装pyproj radius = 0.1 x, y = net.convertLonLatXY(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]
2.4解析rou.xml中的全部道路
for route in sumolib.output.parse_fast("myRoutes.rou.xml", 'route', ['edges']): edge_ids = route.edges.split() # do something with the vector of edge ids可以使用道路ID的矢量数组