您的位置:首页 > 编程语言 > Python开发

Python读取excel数据进行SUMO仿真

2016-10-06 20:12 483 查看
在sumo仿真学习过程中,对方提出需求:提供路口交通流数据,按照数据进行仿真。

 


提供数据有:交叉口每个方向车辆数据(5分钟刷新一次),路口的Vissim地图等。

我们要根据提供的信息来进行基于SUMO的仿真。我们的步骤是:

1、获取net.xml

2、获取rou.xml

3、进行仿真

 

1、获取net.xml

因为对方提供的是vissm的路网数据。格式为.inpx。虽然说sumo提供了指令可以实现从.inpx到net.xml的实现,但是对于复杂网络,转换效果并不理想。我们还是直接从OpenStreetMap来下载路网数据。这里我们截取我们进行仿真的区域地图。然后通过netconvert将.osm地图数据转化为.net.xml数据。

Netconvert –osm-file filename.osm –o filename.net.xml

 

2、获取rou.xml

这里我们使用flow定义来实现五分钟内的车流。这里要做的就是从excel表中将数据筛选出,然后写入rou.xml文件。



算了一下将近3000条数据,人工筛选简直是作死。那么程序筛选。分析给的数据格式,我们首先将给的车辆数据和车辆行驶方向挂钩。比如:

北左1:S_E(即北->东方向),北直2:S_N(即北->南方向)………以此类推

然后分析excel表,从第9行开始,其中0、5、10 、13 、18列为总数,不读取,其他为0 的也不读取。时间按照每行增加。

相关代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 读取excel数据
from __future__ import print_function
import xlrd

def generate_routefile():

data = xlrd.open_workbook('hefei_jinsong3_liuliang_2016-08-25.xls')
table = data.sheets()[0]
nrows = table.nrows
rouNum = 0
beginTime = 0
endTime = 3000
rouReal = (0,'jinsong3_hefei_S_E','jinsong3_hefei_S_N','jinsong3_hefei_S_N','jinsong3_hefei_S_N',
0,'jinsong3_hefei_E_E','jinsong3_hefei_E_W','jinsong3_hefei_E_W','jinsong3_hefei_E_S',
0,'jinsong3_hefei_N_W','jinsong3_hefei_N_S',0,'jinsong3_hefei_W_W','jinsong3_hefei_W_S',
'jinsong3_hefei_W_E','jinsong3_hefei_W_N',0)
#这里的语句相当于routes=open("data/cross.rou.xml","w"),打开文件并赋值
with open("hefei_jinsong3_liuliang_2016-08-25.rou.xml", "w") as routes:
#将以下内容写入rou.xml文件中
print("""<?xml version="1.0" encoding="UTF-8"?>
<routes>
<route id="jinsong3_hefei_E_S" edges="384486711#8 384501552#3"/>
<route id="jinsong3_hefei_E_W" edges="384486711#8 384486711#9 384486711#10"/>
<route id="jinsong3_hefei_E_N" edges="384486711#8 384486711#9 384501549#2 384501549#3"/>
<route id="jinsong3_hefei_E_E" edges="384486711#8 384486711#9 384501549#2 384486722#5 384486722#6"/>
<route id="jinsong3_hefei_S_N" edges="384501549#1 384501549#2 384501549#3"/>
<route id="jinsong3_hefei_S_W" edges="384501549#1 384486711#10"/>
<route id="jinsong3_hefei_S_E" edges="384501549#1 384501549#2 384486722#5 384486722#6"/>
<route id="jinsong3_hefei_S_S" edges="384501549#1 384501549#2 384486722#5 384501552#2 384501552#3"/>
<route id="jinsong3_hefei_W_N" edges="384486722#4 384501549#3"/>
<route id="jinsong3_hefei_W_E" edges="384486722#4 384486722#5 384486722#6"/>
<route id="jinsong3_hefei_W_S" edges="384486722#4 384486722#5 384501552#2 384501552#3"/>
<route id="jinsong3_hefei_W_W" edges="384486722#4 384486722#5 384501552#2 384486711#9 384486711#10"/>
<route id="jinsong3_hefei_N_E" edges="384501552#1 384486722#6"/>
<route id="jinsong3_hefei_N_S" edges="384501552#1 384501552#2 384501552#3"/>
<route id="jinsong3_hefei_N_W" edges="384501552#1 384501552#2 384486711#9 384486711#10"/>
<route id="jinsong3_hefei_N_N" edges="384501552#1 384501552#2 384486711#9 384501549#2 384501549#3"/>""", file=routes)

for i in range(92,nrows):
for j in range(18):
if table.row_values(i)[j] > 0:
if j in [0,5,10,13]:continue
print('        <flow id="js3_hf_%s" route="%s" begin="%d" end="%d" number="%d" departlane = "random"/>' %
(rouNum, str(rouReal[j]), beginTime , endTime,table.row_values(i)[j]), file=routes)
rouNum += 1
beginTime = endTime
endTime += 3000
#至此,写完了rou.xml文件,这里全部都是随机生成的
print("</routes>", file=routes)

if __name__ == '__main__':
generate_routefile()


最终得到的rou文件内容(部分)如下:



3、仿真

然后编辑配置文件,运行进行仿真即可。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: