-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0205_waiting_times_fgc.R
337 lines (225 loc) · 10 KB
/
0205_waiting_times_fgc.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
library(tidyverse)
library(tidytransit)
library(sf)
library(furrr)
library(tmap)
library(lubridate)
future::multicore
# PENDENT: encodings, gràfics
# read gtfs
gtfss <- list.files("./data", pattern = "gtfs", full.names = TRUE)
gtfss <- as_tibble(gtfss)
colnames(gtfss) <- "file"
# functions
get_stops <- function(x){
files <- read_gtfs(x)
sfs <- gtfs_as_sf(files)
stops <- sfs$stops
}
get_stop_times <- function(x){
files <- read_gtfs(x)
stops <- files$stop_times
}
get_trips <- function(x){
files <- read_gtfs(x)
trips <- files$trips
}
get_calendar <- function(x){
files <- read_gtfs(x)
calendar <- files$calendar
}
get_calendar_dates <- function(x){
files <- read_gtfs(x)
calendar <- files$calendar_dates
}
get_routes <- function(x){
files <- read_gtfs(x)
routes <- files$routes
}
# read stops & stop_times
stops_map <- gtfss %>%
mutate("list" = future_map(file, get_stops)) %>%
unnest() %>%
mutate("agència" = str_remove(file, "./data/gtfs_"),
"agència" = str_remove(`agència`, ".zip")) %>%
select(-file) %>%
mutate("agència" = case_when(`agència` == "amb" ~ "Bus urbà TMB/AMB i metro",
`agència` == "tmb" ~ "Bus urbà TMB/AMB i metro",
`agència` == "trambaix" ~ "Tram",
`agència` == "trambesos" ~ "Tram",
`agència` == "fgc" ~ "Ferrocarrils de la Generalitat de Catalunya",
`agència` == "interurba" ~ "Bus Interurbà Generalitat",
TRUE ~ "Rodalies RENFE"))
stop_times <- gtfss %>%
mutate("list" = future_map(file, get_stop_times)) %>%
unnest() %>%
mutate("agència" = str_remove(file, "./data/gtfs_"),
"agència" = str_remove(`agència`, ".zip")) %>%
mutate("agència" = case_when(`agència` == "amb" ~ "Bus urbà TMB/AMB i metro",
`agència` == "tmb" ~ "Bus urbà TMB/AMB i metro",
`agència` == "trambaix" ~ "Tram",
`agència` == "trambesos" ~ "Tram",
`agència` == "fgc" ~ "Ferrocarrils de la Generalitat de Catalunya",
`agència` == "interurba" ~ "Bus Interurbà Generalitat",
TRUE ~ "Rodalies RENFE")) %>%
select(-file)
trips <- gtfss %>%
mutate("list" = future_map(file, get_trips)) %>%
unnest() %>%
mutate("agència" = str_remove(file, "./data/gtfs_"),
"agència" = str_remove(`agència`, ".zip")) %>%
mutate("agència" = case_when(`agència` == "amb" ~ "Bus urbà TMB/AMB i metro",
`agència` == "tmb" ~ "Bus urbà TMB/AMB i metro",
`agència` == "trambaix" ~ "Tram",
`agència` == "trambesos" ~ "Tram",
`agència` == "fgc" ~ "Ferrocarrils de la Generalitat de Catalunya",
`agència` == "interurba" ~ "Bus Interurbà Generalitat",
TRUE ~ "Rodalies RENFE")) %>%
select(-file)
# select interurban bus stops at less than 500 metres of a commuter rail stop
intbus <- stops_map %>%
filter(`agència` == "Bus Interurbà Generalitat") %>%
st_sf()
fgc <- stops_map %>%
filter(`agència` == "Ferrocarrils de la Generalitat de Catalunya") %>%
st_sf()
st_crs(fgc) <- "EPSG:4326"
st_crs(intbus) <- "EPSG:4326"
# read cells
cells <- st_read("./data/project_rodalies.gpkg", layer = "celles_mobilitat_agrupades")
# get stops only in studied cells
fgc <- fgc %>%
st_transform(st_crs(cells)) %>%
st_intersection(cells)
intbus <- intbus %>%
st_transform(st_crs(cells)) %>%
st_intersection(cells)
# define buffer
fgc_buffer <- fgc %>%
st_buffer(500)
# get stops at 500 metres of a train station with train info
intbus_filt <- intbus %>%
st_join({fgc_buffer %>%
rename("fgc_stop" = "stop_name") %>%
select(fgc_stop, geometry)}, left = FALSE) %>%
select(stop_id, stop_name, fgc_stop, geometry) %>%
distinct()
# get distance & travel time between each stop and its nearest neighbour
dist_matrix <- as.data.frame(st_distance(intbus_filt, fgc))
colnames(dist_matrix) <- fgc$stop_name
dist_matrix <- cbind(dist_matrix, as.data.frame(intbus_filt$stop_name))
dist_matrix_long <- dist_matrix %>%
pivot_longer(cols = (-`intbus_filt$stop_name`), names_to = "fgc_stop", values_to = "distance") %>%
rename("stop_name" = "intbus_filt$stop_name")
intbus_filt <- intbus_filt %>%
left_join(dist_matrix_long, by = c("stop_name", "fgc_stop")) %>%
mutate("time" = lubridate::period(second = ((3600/3500)*as.numeric(distance)))) %>% # assumming 3.5 kmh
distinct()
# train stops without nearby bus stops
fgc_nobus <- fgc %>%
filter(!(stop_name %in% intbus_filt$fgc_stop)) %>%
distinct()
# get stop_times
fgc_stoptimes <- stop_times %>%
filter(stop_id %in% fgc$stop_id) %>%
left_join(select(fgc, stop_id, stop_name), by = "stop_id")
intbus_stoptimes <- stop_times %>%
filter(stop_id %in% intbus_filt$stop_id) %>%
left_join(select(intbus_filt, stop_id, stop_name), by = "stop_id")
# filter stop_times for any Thursday in October - NOTE need to do this ad-hoc for each agency
intbus_calendar <- gtfss[3,1] %>%
mutate("list" = map(file, get_calendar)) %>%
select(-file) %>%
mutate("agència" = "Bus Interurbà Generalitat") %>%
unnest()
intbus_calendar_dates <- gtfss[3,1] %>%
mutate("list" = map(file, get_calendar_dates)) %>%
select(-file) %>%
mutate("agència" = "Bus Interurbà Generalitat") %>%
unnest()
intbus_calendar_dates_filt <- intbus_calendar_dates %>%
filter(`date` == "2021-10-21")
fgc_calendar <- gtfss[2,1] %>%
mutate("list" = map(file, get_calendar)) %>%
select(-file) %>%
mutate("agència" = "Ferrocarrils de la Generalitat de Catalunya") %>%
unnest()
fgc_calendar_dates <- gtfss[2,1] %>%
mutate("list" = map(file, get_calendar_dates)) %>%
select(-file) %>%
mutate("agència" = "Ferrocarrils de la Generalitat de Catalunya") %>%
unnest()
fgc_filtered_dates <- fgc_calendar %>%
filter(date == "2022-10-06") %>%
select(service_id)
fgc_trips_filtered <- trips %>%
filter((`agència` == "Ferrocarrils de la Generalitat de Catalunya") &
(service_id %in% fgc_filtered_dates$service_id)) #2021 only have Monestir de Montserrat
fgc_stoptimes_filtered <- fgc_stoptimes %>%
filter(trip_id %in% fgc_trips_filtered$trip_id)
intbus_trips_filtered <- trips %>%
filter((`agència` == "Bus Interurbà Generalitat") &
(service_id %in% intbus_calendar_dates_filt$service_id))
intbus_stoptimes_filtered <- intbus_stoptimes %>%
filter(trip_id %in% intbus_trips_filtered$trip_id) %>%
left_join(select(as_tibble(intbus_filt), stop_id, fgc_stop, time, -geometry), by = "stop_id")
# get earliest possible train departure time to ensure
intbus_stoptimes_filtered <- intbus_stoptimes_filtered %>%
mutate("minimal_deptime" = hms::hms(as.period(arrival_time)+time))
# join route names to intbus & train
intbus_stoptimes_filtered <- intbus_stoptimes_filtered %>%
left_join(select(intbus_trips_filtered, trip_id, route_id), by = "trip_id")
fgc_stoptimes_filtered <- fgc_stoptimes_filtered %>%
left_join(select(fgc_trips_filtered, trip_id, route_id), by = "trip_id")
fgc_routes <- gtfss[2,1] %>%
mutate("list" = map(file, get_routes)) %>%
select(-file) %>%
mutate("agència" = "Ferrocarrils de la Generalitat de Catalunya") %>%
unnest()
fgc_stoptimes_filtered <- fgc_stoptimes_filtered %>%
left_join(select(fgc_routes, route_id, route_long_name), by = "route_id")
# get nearest train departure time for each route & headsign after each assignation
get_next_stop <- function(time, train_stop){
mintrips <- fgc_stoptimes_filtered %>%
filter((stop_name == train_stop) &
(departure_time >= time)) %>%
rename("fgc_route_name" = "route_long_name") %>%
group_by(fgc_route_name) %>%
summarise("first_stop" = hms::hms(as.period(min(departure_time)))) %>%
ungroup()
}
connections <- intbus_stoptimes_filtered %>%
mutate("stop_times" = future_map2(minimal_deptime, fgc_stop, get_next_stop)) %>%
as_tibble() %>%
select(-geometry) %>%
unnest(cols = everything(), keep_empty = TRUE)
write.csv(connections, "./data/connexions_bus_fgc.csv")
cells <- st_read("./data/project_rodalies.gpkg", layer = "celles_mobilitat_agrupades")
connections <- read.csv("./data/connexions_bus_fgc.csv")
connections <- connections %>%
filter((arrival_time >= "07:00:00") &
(arrival_time <= "10:00:00")) %>%
arrange(arrival_time)
# get time lapse between bus arrival and first departure
connections$waiting_time <- lubridate::hms(connections$first_stop)-lubridate::hms(connections$minimal_deptime)
connections$waiting_time <- as.duration(connections$waiting_time)
# filter for stops outside of the central zone
fgc_nocentre <- fgc %>%
st_intersection(filter(cells, NOMBRE_CEL != "Zona central")) %>%
select(colnames(fgc))
connections <- connections %>%
filter(fgc_stop %in% fgc_nocentre$stop_name)
connections_bystop <- connections %>%
group_by(fgc_stop) %>%
summarise("min_waitingtime" = duration(seconds = min(as.numeric(waiting_time), na.rm = TRUE)),
"mean_waitingtime" = duration(seconds = mean(as.numeric(waiting_time), na.rm = TRUE)),
"median_waitingtime" = duration(seconds = median(as.numeric(waiting_time), na.rm = TRUE)),
"max_waitingtime" = duration(seconds = max(as.numeric(waiting_time), na.rm = TRUE)))
connections_byroute <- connections %>%
group_by(fgc_route_name) %>%
summarise("min_waitingtime" = duration(seconds = min(as.numeric(waiting_time), na.rm = TRUE)),
"mean_waitingtime" = duration(seconds = mean(as.numeric(waiting_time), na.rm = TRUE)),
"median_waitingtime" = duration(seconds = median(as.numeric(waiting_time), na.rm = TRUE)),
"max_waitingtime" = duration(seconds = max(as.numeric(waiting_time), na.rm = TRUE)))
write.csv(connections_byroute, "./data/connexions_ruta_horapunta_fgc.csv")
write.csv(connections_bystop, "./data/connexions_parada_horapunta_fgc.csv")