TransBigData 公交地铁网络指南
安装
bash
pip install transbigdata networkx geopandas
地铁网络建模
1. 构建网络 - metro_network()
python
import transbigdata as tbd
import geopandas as gpd
# 加载线路和站点数据
line = gpd.read_file('metro_lines.shp') # 需包含 linename, speed, stoptime
stop = gpd.read_file('metro_stops.shp')
# 构建网络图
G = tbd.metro_network(
line,
stop,
transfertime=5 # 换乘时间(分钟)
)
数据要求:
- •
line: 包含linename(线路名)、speed(运行速度)、stoptime(停站时间) - •
stop: 站点位置数据
2. 最短路径查询 - get_shortest_path()
python
path = tbd.get_shortest_path(
G,
stop,
ostation='福田', # 起点站名
dstation='罗湖' # 终点站名
)
# 返回: 站点名列表
3. K 条最短路径 - get_k_shortest_paths()
python
paths = tbd.get_k_shortest_paths(
G,
stop,
ostation='福田',
dstation='罗湖',
k=3 # 前3条最短路径
)
4. 路径耗时计算 - get_path_traveltime()
python
travel_time = tbd.get_path_traveltime(G, path) # 返回分钟
5. 线路分割 - split_subwayline()
将地铁线路按站点分割为线段。
python
line_segments = tbd.split_subwayline(line, stop)
公交 GPS 数据处理
6. 到站信息识别 - busgps_arriveinfo()
从公交 GPS 轨迹识别到站/离站时间。
python
arrive_info = tbd.busgps_arriveinfo(
bus_gps_data,
line, # 公交线路 GeoDataFrame
stop, # 公交站点 GeoDataFrame
col=['VehicleNum', 'GPSTime', 'Lng', 'Lat'],
stopbuffer=200, # 站点缓冲区半径(米)
mintime=300 # 最小停留时间(秒)
)
7. 单程时间计算 - busgps_onewaytime()
python
oneway_time = tbd.busgps_onewaytime(
arrive_info,
start='起点站',
end='终点站',
col=['VehicleNum', 'StopName', 'ArriveTime', 'LeaveTime']
)
完整示例:地铁网络分析
python
import pandas as pd
import geopandas as gpd
import transbigdata as tbd
from shapely.geometry import Point
# 1. 准备数据
# 线路数据
line_data = gpd.GeoDataFrame({
'linename': ['1号线', '1号线', '2号线', '2号线'],
'speed': [60, 60, 55, 55], # km/h
'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')
# 2. 构建网络
G = tbd.metro_network(line_data, stop_data, transfertime=5)
# 3. 查询最短路径
path = tbd.get_shortest_path(G, stop_data, ostation='A站', dstation='C站')
print(f"最短路径: {' → '.join(path)}")
# 4. 计算耗时
time = tbd.get_path_traveltime(G, path)
print(f"预计耗时: {time:.1f} 分钟")
# 5. 查询多条路径
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} 分钟")
完整示例:公交运行分析
python
import pandas as pd
import geopandas as gpd
import transbigdata as tbd
# 1. 加载数据
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')
# 2. 识别到站信息
arrive_info = tbd.busgps_arriveinfo(
bus_gps,
bus_line,
bus_stop,
col=['VehicleNum', 'GPSTime', 'Lng', 'Lat'],
stopbuffer=100
)
# 3. 计算单程运行时间
oneway = tbd.busgps_onewaytime(
arrive_info,
start='起点站',
end='终点站'
)
# 4. 分析运行时间分布
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