timetable = Timetable()
timetable.load_file('E:/Code/Data/EuropeRailway/Europe_Railway_Schedule.csv',
                    mode='ALL',
                    encoding='utf-8')  ### ALL 指的是全部车型, 还可以特指HSR,但是欧洲不支持
#G_schedule = timetable.get_DiGraph(mode="all")
G_HSRTimetable = timetable.get_graph()

#### 2.3 去掉时刻表中没有位置的点
for n in list(G_HSRTimetable.nodes()):
    if n not in dict_HSRLocs:
        G_HSRTimetable.remove_node(n)

### 2.4 车站间 car driving时间的数据   2019.09.27添加
G_stationPair = lib_graph.createG_fromFile(
    'E:/Code/Data/EuropeRailway/Europe_StationPairAccessibility_200km.csv',
    source='source',
    target='target',
    attr=['distanceLBS_km', 'duration_min'])
##### 挑出来 20min 之内的可互达的车站
for e in list(G_stationPair.edges):
    if G_stationPair[e[0]][e[1]]['duration_min'] > 20:  ### 门限1,20 mins
        G_stationPair.remove_edge(e[0], e[1])
for n in list(G_stationPair.nodes):
    if len(list(nx.neighbors(G_stationPair, n))) == 0:
        G_stationPair.remove_node(n)

###############################################################################
#################### program start ############################################
"""
限制条件:  1. 20min车程内
           2. 相似度在0.02以下
示例#2
0
#### 5. 传递节点间的连通关系,,确定 格点id 之间的连通关系
dict_links = {}
for row in df_links.itertuples():
    s_grid = dict_Nodegrid[row.source]
    t_grid = dict_Nodegrid[row.target]
    spd = row.maxspeed
    if s_grid == t_grid:
        continue
    link = tuple(sorted([s_grid, t_grid]))
    dict_links[link] = max(spd,
                           dict_links[link]) if link in dict_links else spd

#### 6. 保存到文件,一个_nodes文件, 节点:id-lon/lat
f_out_nodes = 'china-gridded-nodes.csv'
lib_graph.saveLocation_toFile(dict_gridNewlocations, f_out_nodes)
#       一个_ways文件爱呢, 路径: fid,tid, maxspeed
f_out_links = 'china-gridded-links.csv'
out = [[*od, dict_links[od]] for od in dict_links]
df = pd.DataFrame(out, columns=['source', 'target', 'maxspeed'])
df.to_csv(f_out_links)

ttt.duration('Finished creating simplified network')

G = lib_graph.createG_fromFile(f_out_links,
                               source='source',
                               target='target',
                               attr='maxspeed')
lib_graph.plot_G(G, dict_gridNewlocations)

ttt.duration('Finished plotting')
##### 2.2 高铁时刻表,加载有向图
timetable = Timetable()
timetable.load_file(
    '/run/media/pengfei/OTHERS/Code/Data/ChinaRailway/China_Railway_Schedule_20190701.csv'
)
###### 2.3 去掉时刻表中没有位置的点
G_schedule = timetable.get_TravelT(mode="all")
for n in list(G_schedule.nodes()):
    if n not in dict_HSRLocs:
        G_schedule.remove_node(n)

#### 3.1 加载 车站在三小时car driving 时间内可达的格点的图(graph)
f_HSRGrid = '/run/media/pengfei/OTHERS/Code/Data/ChinaRailway/CalculateGridTravelTTeacherGiveME/StationGridAccessibility_Adapt.csv'
G_HSRGrid = lib_graph.createG_fromFile(
    f_HSRGrid,
    source='station',
    target='grid',
    attr=['distanceLBS_m', 'distance_m', 'duration_s'])

##### 感觉三小时太长了,换成1.0小时
for e in list(G_HSRGrid.edges):
    if G_HSRGrid[e[0]][e[1]]['duration_s'] > 1.0 * 60 * 60:
        G_HSRGrid.remove_edge(e[0], e[1])
for n in list(G_HSRGrid.nodes):
    if len(list(nx.neighbors(G_HSRGrid, n))) == 0:
        G_HSRGrid.remove_node(n)
##### 顺便区分一下格点
grids = set()  ### 注意这还是 字符串型
for n in list(G_HSRGrid.nodes):
    if n not in dict_HSRLocs:  ### 分辨了格点和车站
        grids.add(n)
G_flights = flights.get_DiGraph()
for name in list(G_flights.nodes):  ### 有五个机场没有位置信息,删除了
    if name not in dict_AirportLocs:
        G_flights.remove_node(name)
##### 2.3 两个时刻表合并
G_schedule = nx.compose(G_Rail, G_flights)  ## 二者连成一张图
##### 2.4 为节点添加类型标示
G_schedule = add_type2G(G_schedule, dict_HSRLocs, dict_AirportLocs)
"""
    3. 机场/车站 与 网格点之间驾车时间的数据    机场--网格:2hours   车站--网格: 1hour
    数据类型: 无向图 Graph
"""
##### 3.1 车站<-->网格 1 hours
f_HSRGrid = OS + 'Code/Data/ChinaRailway/China_StationGridAccessibility_1h_5mins.csv'  # 分辨率是1/12度
G_HSRGrid = lib_graph.createG_fromFile(f_HSRGrid,
                                       source='station',
                                       target='grid',
                                       attr=['distanceLBS_m', 'duration_s'])
##### 3.2 机场<-->网格 2 hours
f_AirGrid = OS + 'Code/空铁联合换乘/data/China_AirportGridAccessibility_2h_5mins.csv'
G_AirGrid = lib_graph.createG_fromFile(f_AirGrid,
                                       source='station',
                                       target='grid',
                                       attr=['distanceLBS_m', 'duration_s'])
##### 3.3 合并
G_PortGrid = nx.compose(G_HSRGrid, G_AirGrid)  ## 二者连成一张图
##### 2.4 为节点添加类型标示
G_PortGrid = add_type2G(G_PortGrid, dict_HSRLocs, dict_AirportLocs)
"""
    4. 车站<-->车站   机场<-->机场    车站<-->机场   驾车时间的数据 ( 需要自设门限然后截取)
"""
示例#5
0
from lib_timetable import Timetable, FlightTimetable
import lib_grid
import lib_graph
import lib_plot
import networkx as nx
from shapely.geometry import Point
import geopandas as gpd
import geoplot as gplt
import lib_geo
import geoplot.crs as gcrs
import numpy as np
import lib_time

#### 1. 加载第0步已经做好的数据 (HSR:50km, Airport:150km)
G_AirGrids = lib_graph.createG_fromFile('graph_GridAir.csv',
                                        source='source',
                                        target='target',
                                        attr='')
G_HSRGrids = lib_graph.createG_fromFile('graph_GridHSR.csv',
                                        source='source',
                                        target='target',
                                        attr='')
#### 1.1 加载机场、车站位置数据
dict_AirportLocs = lib_file.pickle_load('dict_AirportLocs.pkl')
dict_HSRLocs = lib_file.pickle_load('dict_HSRLocs.pkl')

#### 2 由时刻表建图,获取联通关系 ## 属性上有车次
##### 2.1 飞机时刻表建图

air_timetable = FlightTimetable()
air_timetable.load_file('E:/Code/Data/ChinaAir/China_Air_Schedule_clean.csv')
G_airTimetable = air_timetable.get_graph()
    OS + 'Code/Data/Sebastian_ScheduleDataChina20191114/stations.csv',
    ID='ID',
    lon='lon',
    lat='lat')
dict_AirportLocs = lib_graph.getLocation_fromFile(
    OS + 'Code/Data/Sebastian_ScheduleDataChina20191114/airports.csv',
    ID='ID',
    lon='lon',
    lat='lat')
dict_PortLocs = copy.deepcopy(dict_HSRLocs)
for k in dict_AirportLocs:
    dict_PortLocs[k] = dict_AirportLocs[k]
## 异站换乘driving time
G_PortPort = lib_graph.createG_fromFile(
    OS + 'Code/Data/Sebastian_ScheduleDataChina20191114/drivingTime.csv',
    source='source',
    target='target',
    attr=['contype', 'distance_km', 'duration_s'])
## 车站到网格的驾车时间
G_PortGrid = lib_file.pickle_load('G_PortGrid.pkl')
###############################################
### 时刻表建图
timetable = TimetableNew()
timetable.load_trainFile(
    OS + 'Code/Data/Sebastian_ScheduleDataChina20191114/trains.txt')
timetable.load_flightFile(
    OS + 'Code/Data/Sebastian_ScheduleDataChina20191114/flights.csv')
G_schedule = timetable.get_DiGraph(mode='all')
for name in G_schedule:  ## 为图节点添加属性
    G_schedule.node[name][
        'type'] = 'airport' if name in dict_AirportLocs else 'station'
import sys
sys.path.append(r'E:/pythonlibs')
import numpy as np
import pandas as pd
from rtree import index
import networkx as nx
from lib_timetable import Stations, Timetable
from lib_graph import createG_fromFile, getLocation_fromFile
from haversine import haversine
import lib_graph
from lib_plot import jointplotFromLists, ConvexHull


G_timetable = createG_fromFile('E:/OSM/OSMData/China/timetable_links.csv')
## 获取车站的位置, 不再使用精确的车站位置数据,而是使用简化的版本
#f_stations = "E:/Code/Data/ChinaRailway/China_Station_Location_City.xls"
f_nodes = 'china-gridded-nodes.csv'
stations_obj = Stations()
stations_obj.load_file(f_nodes)
dict_NameLoc = stations_obj.get_NameLoc_Index() # 创建一个 name:(lon,lat)的字典
dict_NameID = dict(zip(stations_obj.df['name'], stations_obj.df['ID']))


dict_IDLoc = lib_graph.getLocation_fromFile(f_nodes, ID='ID', lon='lon', lat='lat')
### 加载列车时刻表
fs = "E:/Code/Data/ChinaRailway/China_Railway_Schedule_20190701.csv"
timetable_obj = Timetable()
timetable_obj.load_file(fs)
df_timetable = timetable_obj.df
dict_pairT_timetable = timetable_obj.get_directTravelT()