| name | transbigdata-metroline |
| description | 使用 TransBigData 构建公交地铁网络拓扑和路径分析。适用于地铁网络建模、最短路径查询、换乘分析、公交 GPS 到站识别等任务。 |
TransBigData 公交地铁网络指南
安装
pip install transbigdata networkx geopandas
地铁网络建模
1. 构建网络 - metro_network()
import transbigdata as tbd
import geopandas as gpd
line = gpd.read_file('metro_lines.shp')
stop = gpd.read_file('metro_stops.shp')
G = tbd.metro_network(
line,
stop,
transfertime=5
)
数据要求:
line: 包含 linename(线路名)、speed(运行速度)、stoptime(停站时间)
stop: 站点位置数据
2. 最短路径查询 - get_shortest_path()
path = tbd.get_shortest_path(
G,
stop,
ostation='福田',
dstation='罗湖'
)
3. K 条最短路径 - get_k_shortest_paths()
paths = tbd.get_k_shortest_paths(
G,
stop,
ostation='福田',
dstation='罗湖',
k=3
)
4. 路径耗时计算 - get_path_traveltime()
travel_time = tbd.get_path_traveltime(G, path)
5. 线路分割 - split_subwayline()
将地铁线路按站点分割为线段。
line_segments = tbd.split_subwayline(line, stop)
公交 GPS 数据处理
6. 到站信息识别 - busgps_arriveinfo()
从公交 GPS 轨迹识别到站/离站时间。
arrive_info = tbd.busgps_arriveinfo(
bus_gps_data,
line,
stop,
col=['VehicleNum', 'GPSTime', 'Lng', 'Lat'],
stopbuffer=200,
mintime=300
)
7. 单程时间计算 - busgps_onewaytime()
oneway_time = tbd.busgps_onewaytime(
arrive_info,
start='起点站',
end='终点站',
col=['VehicleNum', 'StopName', 'ArriveTime', 'LeaveTime']
)
完整示例:地铁网络分析
import pandas as pd
import geopandas as gpd
import transbigdata as tbd
from shapely.geometry import Point
line_data = gpd.GeoDataFrame({
'linename': ['1号线', '1号线', '2号线', '2号线'],
'speed': [60, 60, 55, 55],
'stoptime': [0.5, 0.5, 0.5, 0.5]
})
stop_data = gpd.GeoDataFrame({
'stopname': ['A站', 'B站', 'C站', 'D站'],
'linename': ['1号线', '1号线,2号线', '2号线', '1号线'],
'geometry': [Point(114.0, 22.5), Point(114.05, 22.52),
Point(114.1, 22.55), Point(114.08, 22.48)]
}, crs='EPSG:4326')
G = tbd.metro_network(line_data, stop_data, transfertime=5)
path = tbd.get_shortest_path(G, stop_data, ostation='A站', dstation='C站')
print(f"最短路径: {' → '.join(path)}")
time = tbd.get_path_traveltime(G, path)
print(f"预计耗时: {time:.1f} 分钟")
paths = tbd.get_k_shortest_paths(G, stop_data, ostation='A站', dstation='C站', k=3)
for i, p in enumerate(paths, 1):
t = tbd.get_path_traveltime(G, p)
print(f"方案{i}: {' → '.join(p)},耗时 {t:.1f} 分钟")
完整示例:公交运行分析
import pandas as pd
import geopandas as gpd
import transbigdata as tbd
bus_gps = pd.read_csv('bus_gps.csv')
bus_gps['GPSTime'] = pd.to_datetime(bus_gps['GPSTime'])
bus_line = gpd.read_file('bus_line.shp')
bus_stop = gpd.read_file('bus_stop.shp')
arrive_info = tbd.busgps_arriveinfo(
bus_gps,
bus_line,
bus_stop,
col=['VehicleNum', 'GPSTime', 'Lng', 'Lat'],
stopbuffer=100
)
oneway = tbd.busgps_onewaytime(
arrive_info,
start='起点站',
end='终点站'
)
print(f"平均单程时间: {oneway['duration'].mean():.1f} 分钟")
print(f"最短: {oneway['duration'].min():.1f} 分钟")
print(f"最长: {oneway['duration'].max():.1f} 分钟")
使用建议
-
网络构建:
- 确保站点数据包含换乘站信息
transfertime 通常设为 3-5 分钟
-
公交GPS分析:
stopbuffer 根据站点密度调整,通常 100-200 米
- 数据需要先按车辆和时间排序
-
数据准备:
- 可从 OpenStreetMap 或高德地图 API 获取线路站点数据
- 使用
getbusdata() 函数获取公交数据
参考文档
完整文档: https://transbigdata.readthedocs.io/en/latest/metroline.html