Python读取excel数据进行SUMO仿真
2016-10-06 20:12
483 查看
在sumo仿真学习过程中,对方提出需求:提供路口交通流数据,按照数据进行仿真。
提供数据有:交叉口每个方向车辆数据(5分钟刷新一次),路口的Vissim地图等。
我们要根据提供的信息来进行基于SUMO的仿真。我们的步骤是:
1、获取net.xml
2、获取rou.xml
3、进行仿真
Netconvert –osm-file filename.osm –o filename.net.xml
算了一下将近3000条数据,人工筛选简直是作死。那么程序筛选。分析给的数据格式,我们首先将给的车辆数据和车辆行驶方向挂钩。比如:
北左1:S_E(即北->东方向),北直2:S_N(即北->南方向)………以此类推
然后分析excel表,从第9行开始,其中0、5、10 、13 、18列为总数,不读取,其他为0 的也不读取。时间按照每行增加。
相关代码:
最终得到的rou文件内容(部分)如下:
提供数据有:交叉口每个方向车辆数据(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、仿真
然后编辑配置文件,运行进行仿真即可。相关文章推荐
- Python从excel读取数据,并使用scipy进行散点的平滑曲线化方法
- poi读取Excel文件并进行数据类型转换实例
- python使用xlrd模块对excel进行读取
- MFC对Excel数据进行读取
- Delphi中使用python脚本读取Excel数据
- Delphi中使用python脚本读取Excel数据
- python读取excel中的数据
- C# 读取Excel数据 并对日期是数字的进行处理
- 使用Python与openpyxl进行表格处理(一)——读取数据
- Python导出数据到Excel可读取的CSV文件的方法
- 用Python读取excel中的数据
- Windows下使用Python读取Excel表格数据
- EXCEL:读取文本文件到EXCEL并对数据列进行格式化
- python之读取Excel数据
- C# 读取Excel数据 并对日期是数字的进行处理
- python读取excel数据
- python 在excel文件中写入date日期数据,以及读取excel日期数据,如何在python中正确显示date日期。
- 从Excel中读取数据(python-xlrd)
- 读书笔记--python数据可视化--002_读取Excel文件数据
- 读取Excel数据绑定到Gridview进行显示