2016-12-12 3 views
3

내 목표는 정점이 정점 (GTFS의 stops.txt)이고 가장자리가 트립 (GTFS의 stop_times.txt) 인 그래프로 GTFS 정류장 및 트립 정보를 변환하는 것입니다. 첫 번째 단계는 분명하다 :GTFS 데이터에서 iGraph 그래프 만들기 R

> library(igraph) 

#Reading in GTFS files 
> stops<-read.csv("stops.txt") 
> stop_times<-read.csv("stop_times.txt") 

나의 첫번째 본능 iGraph에서 graph_from_data_frame 기능을 사용하려면 간단하게, 그러나 심각한 단점이 있습니다 : DF 정말 원하는 구조로 구성되지 stop_times. 이 기법은 다음과 같다이다 : 나는 사실 (행 당 start_stop_id, end_stop_id, START_TIME, END_TIME을하지 좀하고 싶습니다하면서, 각 정류장에서 도착과 출발 시간과 stop_ids 포함되어 있음을 의미

>head(stop_times) 
    trip_id stop_id arrival_time departure_time stop_sequence shape_dist_traveled 
1 A895151 F04272  06:20:00  06:20:00   10     0 
2 A895151 F04184  06:22:00  06:22:00   20     648 
3 A895151 F04319  06:24:00  06:24:00   30    1224 
4 A895151 F04369  06:27:00  06:27:00   40    2779 
5 A895151 008264  06:31:00  06:31:00   50    5620 
6 A895151 F01520  06:33:00  06:33:00   60    6691 

"중지"하지만 정지에서 변환 된 "통과"). 그러나이 변환은 stop_times에있는 행을 반복하고 trip_id가 같은지 여부를 결정해야하므로 도전적입니다. 그렇다면 NULL을 삽입하거나 별도의 여행을 찾기 위해 시작일 데이터를 계산하십시오. 이것은 나에게 매우 혼란 스럽다.

이 두 데이터 프레임을 원하는 그래프로 결합하는 우아한 방법이 있습니까?

답변

2

'from'과 'to'는 다음 행의 값을 '현재'행까지 '이동'하여 생성 할 수 있습니다. 그리고 정지 정보가 단순히

에 합류 할 수

내가 예와 함께 설명하게, 지금 library(data.table)

## here I"m using Melbourne's GTFS ("http://transitfeeds.com/p/ptv/497/latest/download") 

#dt_stop_times <- lst[[6]]$stop_times 
#dt_stops <- lst[[7]]$stops 

#setDT(dt_stop_times) 
#setDT(dt_stops) 


## join on whatever stop information you want 
dt_stop_times <- dt_stop_times[ dt_stops, on = c("stop_id"), nomatch = 0] 

## set the order of stops for each group (in this case, each group is a trip_id) 
setorder(dt_stop_times, trip_id, stop_sequence) 

## create a new column by shifting the stop_id of the following row up 
dt_stop_times[, stop_id_to := shift(stop_id, type = "lead"), by = .(trip_id)] 

## you will have NAs at this point because the last stop doesn't go anywhere. 

## you can do the same operation on multiple columns at the same time 
dt_stop_times[, `:=`(stop_id_to = shift(stop_id, type = "lead"), 
        arrival_time_stop_to = shift(arrival_time, type = "lead"), 
        departure_time_stop_to = shift(departure_time, type = "lead")), 
       by = .(trip_id)] 

## now you have your 'from' and 'to' columns from which you can make your igraph 

## here's a subset of the result 
dt_stop_times[, .(trip_id, stop_id, stop_name_from = stop_name, arrival_time, stop_id_to, arrival_time_stop_to)] 

#       trip_id stop_id             stop_name_from arrival_time stop_id_to 
# 1:   1.T0.3-86-A-mjp-1.7.R 4174         71-RMIT/Plenty Rd (Bundoora)  25:42:00  4485 
# 2:   1.T0.3-86-A-mjp-1.7.R 4485       70-Janefield Dr/Plenty Rd (Bundoora)  25:43:00  4486 
# 3:   1.T0.3-86-A-mjp-1.7.R 4486        69-Taunton Dr/Plenty Rd (Bundoora)  25:44:00  4487 
# 4:   1.T0.3-86-A-mjp-1.7.R 4487       68-Greenhills Rd/Plenty Rd (Bundoora)  25:45:00  4488 
# 5:   1.T0.3-86-A-mjp-1.7.R 4488      67-Bundoora Square SC/Plenty Rd (Bundoora)  25:46:00  4489 
# ---                               
# 9415793: 9999.UQ.3-19-E-mjp-1.1.H 17871   7-Queen Victoria Market/Elizabeth St (Melbourne City)  23:25:00  17873 
# 9415794: 9999.UQ.3-19-E-mjp-1.1.H 17873  5-Melbourne Central Station/Elizabeth St (Melbourne City)  23:27:00  17875 
# 9415795: 9999.UQ.3-19-E-mjp-1.1.H 17875    3-Bourke Street Mall/Elizabeth St (Melbourne City)  23:30:00  17876 
# 9415796: 9999.UQ.3-19-E-mjp-1.1.H 17876      2-Collins St/Elizabeth St (Melbourne City)  23:31:00  17877 
# 9415797: 9999.UQ.3-19-E-mjp-1.1.H 17877 1-Flinders Street Railway Station/Elizabeth St (Melbourne City)  23:32:00   NA 
#   arrival_time_stop_to 
# 1:     25:43:00 
# 2:     25:44:00 
# 3:     25:45:00 
# 4:     25:46:00 
# 5:     25:47:00 
# ---      
# 9415793:    23:27:00 
# 9415794:    23:30:00 
# 9415795:    23:31:00 
# 9415796:    23:32:00 
# 9415797:     NA 

의 사용 방금 필요 graph_from_data_frame{igraph} 사용하기에 :

# get a df with nodes 
    nodes <- dt_stops[, .(stop_id, stop_lon, stop_lat)] 

# links beetween stops 
    links <- dt_stop_times[,.(stop_id, stop_id_to, trip_id)] 

# create graph 
    g <- graph_from_data_frame(links , directed=TRUE, vertices=nodes) 

마음 그러나 GTFS.zip 파일에는 두 개 이상의 전송 모드 (기차, 버스, 지하철 등)가있을 수 있으며 일부 스톱의 쌍은 서비스 빈도의 변화로 인해 다른 것보다 훨씬 높은 연결성을가집니다. GTFS.zip에서 그래프를 작성할 때이 두 점을 어떻게 고려해야하는지 아직 명확하지 않습니다. 아마도 앞으로의 길은 빈도에 따라 각 에지에 가중치를 부여하고 상호 의존적 인 레이어로 간주되는 각 전송 모드에서 공통적으로 일부 중지를 갖는 다층 네트워크를 구축하는 것입니다.