-
Notifications
You must be signed in to change notification settings - Fork 0
/
transit.R
740 lines (538 loc) · 25.7 KB
/
transit.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# aim: get daily frequency of transit modes in Portugal
# Automatically download GTFS data---------------------------------------------------
# create directory if does not exist already
if (!dir.exists("database/transit")) {
dir.create("database/transit", recursive = TRUE)
}
# Braga - Operador: 4 Planning
braga_gtfs_url = "https://gtfs.pro/files/uran/improved-gtfs-braga.zip" # 1.6MB
download.file(braga_gtfs_url, destfile = "database/transit/braga_gtfs.zip")
# Lisbon - Operador: Carris
lisbon_gtfs_url = "https://gtfs.pro/files/uran/improved-gtfs-gateway.zip" # 15.8MB
download.file(lisbon_gtfs_url, destfile = "database/transit/lisbon_gtfs.zip")
# Área Metropolitana de Lisboa - Operador: Carris Metropolitana
aml_gtfs_url = "https://github.com/carrismetropolitana/gtfs/raw/live/CarrisMetropolitana.zip" #48MB
download.file(aml_gtfs_url, destfile = "database/transit/AML_gtfs.zip")
# Cascais - Operador: Mobi-Cascais
cascais_gtfs_url = "https://gtfs.pro/files/uran/improved-gtfs-mobi-cascais.zip" # 1.4MB
download.file(cascais_gtfs_url, destfile = "database/transit/cascais_gtfs.zip")
# Barreiro - Operador: Transporlis
barreiro_gtfs_url = "https://gtfs.pro/files/uran/improved-gtfs-transportes-colectivos-do-barreiro-pt.zip" # 0.33MB
download.file(barreiro_gtfs_url, destfile = "database/transit/barreiro_gtfs.zip")
# Agueda, Aveiro - Operador: Câmara Municipal de Aveiro (operador?)
agueda_gtfs_url = "https://gtfs.pro/files/uran/improved-gtfs-agueda.zip" # 0.25MB
download.file(agueda_gtfs_url, destfile = "database/transit/agueda_gtfs.zip")
# Porto - Operador: STCP
porto_gtfs_url = "https://gtfs.pro/files/uran/improved-gtfs-stcp-porto-pt.zip" # 7.4MB
download.file(porto_gtfs_url, destfile = "database/transit/porto_gtfs.zip")
# filter gtfs by a tipical working day ------------------------------------
library(sf)
library(tidyverse)
library(lubridate)
library(tidytransit)
# read gtfs files
braga_gtfs = read_gtfs("database/transit/braga_gtfs.zip")
lisbon_gtfs = read_gtfs("database/transit/lisbon_gtfs.zip")
aml_gtfs = read_gtfs("database/transit/AML_gtfs.zip")
cascais_gtfs = read_gtfs("database/transit/cascais_gtfs.zip")
barreiro_gtfs = read_gtfs("database/transit/barreiro_gtfs.zip")
agueda_gtfs = read_gtfs("database/transit/agueda_gtfs.zip")
porto_gtfs = read_gtfs("database/transit/porto_gtfs.zip")
# Select and filter databases by a representative date (Wednesday)
filter_dates = data.frame(
mun = c("braga", "lisbon", "aml", "cascais", "barreiro", "agueda", "porto"),
dates = c("2024-04-03", "2024-05-15", "2024-04-10", "2024-04-10", "2019-04-10", "2019-04-10", "2022-11-09")
)
for (mun in filter_dates$mun) {
cena_date = filter_feed_by_date(
get(paste0(mun, "_gtfs")),
extract_date = filter_dates$dates[filter_dates$mun == mun]
)
assign(paste0(mun, "_date"), cena_date)
print(mun)
}
#Organize the table calculating the frequencies per bus stop
# Braga -------------------------------------------------------------------
## Service pattern
#### Create a table on the gtfs feed that lets us filter by weekday/weekend service
braga_pattern_gtfs = set_servicepattern(braga_gtfs) # WARNING: every time we run this, random numbers will be generated for the service patterns
#### Convert stops and shapes to simple features
braga_pattern_gtfs = gtfs_as_sf(braga_pattern_gtfs)
braga_pattern_gtfs$shapes$length = st_length(braga_pattern_gtfs$shapes)
braga_shape_lengths = braga_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
#### Statistics up to services
service_pattern_summary_braga = braga_pattern_gtfs$trips |>
left_join(braga_pattern_gtfs$.$servicepatterns, by="service_id") |>
left_join(braga_shape_lengths, by="shape_id") |>
left_join(braga_pattern_gtfs$stop_times, by="trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm=TRUE)/1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm=TRUE)/1e3)/(trips*routes),
stops=(n_distinct(stop_id)/2))
#### Number of days that each service operates
service_pattern_summary_braga = braga_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_braga, by = "servicepattern_id")
#### convert service pattern to an excel file
#library(writexl)
#write_xlsx(service_pattern_summary, "database/transit/braga_service_pattern_summary.xlsx")
#### Filter to the most common service pattern id
service_id_braga = braga_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id == 's_6a7097c') |> # my random generated service pattern
pull(service_id)
head(service_id_braga) |>
knitr::kable()
#### Filter by date
#Get stop frequency (missing data)
braga_f = data.frame()
for (i in 6:23) {
braga = get_stop_frequency(
braga_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = service_id_braga,
by_route = TRUE
)
braga = braga |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
braga_f = rbind(braga_f, braga)
}
braga_frequency = braga_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
braga_table = braga_frequency |>
left_join(braga_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(braga_table)
# Lisbon ------------------------------------------------------------------
#### Create a table on the gtfs feed that lets us filter by weekday/weekend service
lisbon_pattern_gtfs = set_servicepattern(lisbon_gtfs)
#### Convert stops and shapes to simple features
lisbon_pattern_gtfs = gtfs_as_sf(lisbon_pattern_gtfs)
lisbon_pattern_gtfs$shapes$length = st_length(lisbon_pattern_gtfs$shapes)
lisbon_shape_lengths = lisbon_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
#### Statistics up to services
service_pattern_summary_lisbon = lisbon_pattern_gtfs$trips |>
left_join(lisbon_pattern_gtfs$.$servicepatterns, by="service_id") |>
left_join(lisbon_shape_lengths, by="shape_id") |>
left_join(lisbon_pattern_gtfs$stop_times, by="trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm=TRUE)/1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm=TRUE)/1e3)/(trips*routes),
stops=(n_distinct(stop_id)/2))
#### Number of days that each service operates
service_pattern_summary_lisbon = lisbon_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_lisbon, by="servicepattern_id")
#### Filter to the most common service pattern id
service_id_lisbon = lisbon_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id == 's_840dfaf') |>
pull(service_id)
head(service_id_lisbon) |>
knitr::kable()
# Filter by date and get stop frequency
lisbon_f = data.frame()
for (i in 6:23) {
lisbon = get_stop_frequency(
lisbon_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = service_id_lisbon,
by_route = TRUE
)
lisbon = lisbon |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
lisbon_f = rbind(lisbon_f, lisbon)
}
lisbon_frequency = lisbon_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
lisbon_table = lisbon_frequency |>
left_join(lisbon_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(lisbon_table)
# AML ---------------------------------------------------------------------
# Setting the service patterns
aml_pattern_gtfs = set_servicepattern(aml_gtfs)
# Convert stops and shapes into simple features
aml_pattern_gtfs = gtfs_as_sf(aml_pattern_gtfs)
aml_pattern_gtfs$shapes$length = st_length(aml_pattern_gtfs$shapes)
aml_shape_lengths = aml_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
# Get statistics up to services
service_pattern_summary_aml = aml_pattern_gtfs$trips |>
left_join(aml_pattern_gtfs$.$servicepatterns, by = "service_id") |>
left_join(aml_shape_lengths, by = "shape_id") |>
left_join(aml_pattern_gtfs$stop_times, by = "trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm = TRUE) / 1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm = TRUE) / 1e3) / (trips * routes),
stops = (n_distinct(stop_id) / 2)
)
# Add the number of days that each service is in operation
service_pattern_summary_aml = aml_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_aml, by = "servicepattern_id")
# We tested the service patterns for the AML since the locations are very different.
## Adopted service patterns (ranked by "days in service"):
# 1. Service pattern #1: "s_d38ffee" (192 days)
# 2. Service pattern #2: "s_0973a74" (191 days)
# 3. Service pattern #3: "s_70dfe23" (188 days)
# 4. Service pattern #12: "s_fff1bcb" (39 days)
# 5. Service pattern #18: "s_bc376dc" (37 days)
# Get the service_ids for the most common service patterns
service_ids_aml_1 = aml_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_d38ffee") |>
pull(service_id)
service_ids_aml_2 = aml_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_0973a74") |>
pull(service_id)
service_ids_aml_3 = aml_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_70dfe23") |>
pull(service_id)
service_ids_aml_12 = aml_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_fff1bcb") |>
pull(service_id)
service_ids_aml_18 = aml_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_bc376dc") |>
pull(service_id)
# Get route geometries
aml_routes_pattern_1 = get_route_frequency(aml_pattern_gtfs, service_ids = service_ids_aml_1)
aml_routes_pattern_2 = get_route_frequency(aml_pattern_gtfs, service_ids = service_ids_aml_2)
aml_routes_pattern_3 = get_route_frequency(aml_pattern_gtfs, service_ids = service_ids_aml_3)
aml_routes_pattern_12 = get_route_frequency(aml_pattern_gtfs, service_ids = service_ids_aml_12)
aml_routes_pattern_18 = get_route_frequency(aml_pattern_gtfs, service_ids = service_ids_aml_18)
# get_route_geometry needs a gtfs object that includes shapes as simple feature data frames
routes_sf_1 = get_route_geometry(aml_pattern_gtfs, service_ids = service_ids_aml_1)
routes_sf_2 = get_route_geometry(aml_pattern_gtfs, service_ids = service_ids_aml_2)
routes_sf_3 = get_route_geometry(aml_pattern_gtfs, service_ids = service_ids_aml_3)
routes_sf_12 = get_route_geometry(aml_pattern_gtfs, service_ids = service_ids_aml_12)
routes_sf_18 = get_route_geometry(aml_pattern_gtfs, service_ids = service_ids_aml_18)
# join the geometries to the calculated frequencies
routes_sf_1 = routes_sf_1 |> inner_join(aml_routes_pattern_1, by = "route_id")
routes_sf_2 = routes_sf_2 |> inner_join(aml_routes_pattern_2, by = "route_id")
routes_sf_3 = routes_sf_3 |> inner_join(aml_routes_pattern_3, by = "route_id")
routes_sf_12 = routes_sf_12 |> inner_join(aml_routes_pattern_12, by = "route_id")
routes_sf_18 = routes_sf_18 |> inner_join(aml_routes_pattern_18, by = "route_id")
# visualize the routes
# mapview::mapview(routes_sf_1)
# mapview::mapview(routes_sf_2)
# mapview::mapview(routes_sf_3)
# mapview::mapview(routes_sf_12)
# mapview::mapview(routes_sf_18)
#get start and end days in operation for each service pattern
service_pattern_summary_aml = aml_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(start_date = min(date), end_date = max(date)) |>
left_join(service_pattern_summary_aml, by = "servicepattern_id")
#filter service_pattern_summary_aml to the service patterns
service_pattern_summary_aml = service_pattern_summary_aml |>
filter(servicepattern_id %in% c("s_d38ffee", "s_0973a74", "s_70dfe23", "s_fff1bcb", "s_bc376dc"))
#join selected service patterns ids with the frequencies per bus stop
aml_f = data.frame()
for (i in 6:23) {
aml = get_stop_frequency(
aml_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = c(
service_ids_aml_1,
service_ids_aml_2,
service_ids_aml_3,
service_ids_aml_12,
service_ids_aml_18
),
by_route = TRUE
)
aml = aml |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
aml_f = rbind(aml_f, aml)
}
aml_frequency = aml_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
aml_table = aml_frequency |>
left_join(aml_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(aml_table)
# Cascais -----------------------------------------------------------------
# Setting the service patterns
cascais_pattern_gtfs = set_servicepattern(cascais_gtfs)
# Convert stops and shapes into simple features
cascais_pattern_gtfs = gtfs_as_sf(cascais_pattern_gtfs)
cascais_pattern_gtfs$shapes$length = st_length(cascais_pattern_gtfs$shapes)
cascais_shape_lengths = cascais_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
# Get statistics up to services
service_pattern_summary_cascais = cascais_pattern_gtfs$trips |>
left_join(cascais_pattern_gtfs$.$servicepatterns, by = "service_id") |>
left_join(cascais_shape_lengths, by = "shape_id") |>
left_join(cascais_pattern_gtfs$stop_times, by = "trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm = TRUE) / 1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm = TRUE) / 1e3) / (trips * routes),
stops = (n_distinct(stop_id) / 2)
)
# Add the number of days that each service is in operation
service_pattern_summary_cascais = cascais_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_cascais, by = "servicepattern_id")
# Get the service_ids for the most common service patterns
service_ids_cascais = cascais_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_ebf7135") |>
pull(service_id)
# Filter by date and get stop frequency
cascais_f = data.frame()
for (i in 6:23) {
cascais = get_stop_frequency(
cascais_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = service_ids_cascais,
by_route = TRUE
)
cascais = cascais |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
cascais_f = rbind(cascais_f, cascais)
}
cascais_frequency = cascais_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
cascais_table = cascais_frequency |>
left_join(cascais_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(cascais_table)
# Barreiro ----------------------------------------------------------------
# Setting the service patterns
barreiro_pattern_gtfs = set_servicepattern(barreiro_gtfs)
# Convert stops and shapes into simple features
barreiro_pattern_gtfs = gtfs_as_sf(barreiro_pattern_gtfs)
barreiro_pattern_gtfs$shapes$length = st_length(barreiro_pattern_gtfs$shapes)
barreiro_shape_lengths = barreiro_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
# Get statistics up to services
service_pattern_summary_barreiro = barreiro_pattern_gtfs$trips |>
left_join(barreiro_pattern_gtfs$.$servicepatterns, by = "service_id") |>
left_join(barreiro_shape_lengths, by = "shape_id") |>
left_join(barreiro_pattern_gtfs$stop_times, by = "trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm = TRUE) / 1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm = TRUE) / 1e3) / (trips * routes),
stops = (n_distinct(stop_id) / 2)
)
# Add the number of days that each service is in operation
service_pattern_summary_barreiro = barreiro_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_barreiro, by = "servicepattern_id")
# Get the service_ids for the most common service patterns
service_ids_barreiro = barreiro_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_22f0a7c") |>
pull(service_id)
# Filter by date and get stop frequency
barreiro_f = data.frame()
for (i in 6:23) {
barreiro = get_stop_frequency(
barreiro_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = service_ids_barreiro,
by_route = TRUE
)
barreiro = barreiro |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
barreiro_f = rbind(barreiro_f, barreiro)
}
barreiro_frequency = barreiro_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
barreiro_table = barreiro_frequency |>
left_join(barreiro_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(barreiro_table)
# Agueda ------------------------------------------------------------------
# Setting the service patterns
agueda_pattern_gtfs = set_servicepattern(agueda_gtfs)
# Convert stops and shapes into simple features
agueda_pattern_gtfs = gtfs_as_sf(agueda_pattern_gtfs)
agueda_pattern_gtfs$shapes$length = st_length(agueda_pattern_gtfs$shapes)
agueda_shape_lengths = agueda_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
# Get statistics up to services
service_pattern_summary_agueda = agueda_pattern_gtfs$trips |>
left_join(agueda_pattern_gtfs$.$servicepatterns, by = "service_id") |>
left_join(agueda_shape_lengths, by = "shape_id") |>
left_join(agueda_pattern_gtfs$stop_times, by = "trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm = TRUE) / 1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm = TRUE) / 1e3) / (trips * routes),
stops = (n_distinct(stop_id) / 2)
)
# Add the number of days that each service is in operation
service_pattern_summary_agueda = agueda_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_agueda, by = "servicepattern_id")
# Get the service_ids for the most common service patterns
service_ids_agueda = agueda_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_3c9f481") |>
pull(service_id)
# Filter by date and get stop frequency
agueda_f = data.frame()
for (i in 6:23) {
agueda = get_stop_frequency(
agueda_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = service_ids_agueda,
by_route = TRUE
)
agueda = agueda |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
agueda_f = rbind(agueda_f, agueda)
}
agueda_frequency = agueda_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
agueda_table = agueda_frequency |>
left_join(agueda_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(agueda_table)
# Porto -------------------------------------------------------------------
# Setting the service patterns
porto_pattern_gtfs = set_servicepattern(porto_gtfs)
# Convert stops and shapes into simple features
porto_pattern_gtfs = gtfs_as_sf(porto_pattern_gtfs)
porto_pattern_gtfs$shapes$length = st_length(porto_pattern_gtfs$shapes)
porto_shape_lengths = porto_pattern_gtfs$shapes |>
as.data.frame() |>
select(shape_id, length, -geometry)
# Get statistics up to services
service_pattern_summary_porto = porto_pattern_gtfs$trips |>
left_join(porto_pattern_gtfs$.$servicepatterns, by = "service_id") |>
left_join(porto_shape_lengths, by = "shape_id") |>
left_join(porto_pattern_gtfs$stop_times, by = "trip_id") |>
group_by(servicepattern_id) |>
summarise(
trips = n(),
routes = n_distinct(route_id),
total_distance_per_day_km = sum(as.numeric(length), na.rm = TRUE) / 1e3,
route_avg_distance_km = (sum(as.numeric(length), na.rm = TRUE) / 1e3) / (trips * routes),
stops = (n_distinct(stop_id) / 2)
)
# Add the number of days that each service is in operation
service_pattern_summary_porto = porto_pattern_gtfs$.$dates_servicepatterns |>
group_by(servicepattern_id) |>
summarise(days_in_service = n()) |>
left_join(service_pattern_summary_porto, by = "servicepattern_id")
# Get the service_ids for the most common service patterns
service_ids_porto = porto_pattern_gtfs$.$servicepattern |>
filter(servicepattern_id %in% "s_f70554e") |>
pull(service_id)
# Filter by date and get stop frequency
porto_f = data.frame()
for (i in 6:23) {
porto = get_stop_frequency(
porto_date,
start_time = ifelse(i < 10, paste0(i, ":00:00"), paste0(i, ":00:00")),
end_time = ifelse(i < 10, paste0("0", i, ":59:59"), paste0(i, ":59:59")),
service_ids = service_ids_porto,
by_route = TRUE
)
porto = porto |>
group_by(stop_id) |>
summarise(frequency = sum(n_departures)) |>
mutate(hour = i)
agueda_f = rbind(porto_f, porto)
}
porto_frequency = porto_f |>
ungroup() |>
group_by(stop_id, hour) |>
summarise(frequency = sum(frequency)) |>
ungroup()
porto_table = porto_frequency |>
left_join(porto_date$stops |>
select(stop_id, stop_lon, stop_lat), by = "stop_id") |>
st_as_sf(crs = 4326, coords = c("stop_lon", "stop_lat"))
#mapview::mapview(porto_table)
# COMBINE ALL BUS STOPS ---------------------------------------------------
transit_table_all = rbind(braga_table, lisbon_table, aml_table, cascais_table, barreiro_table, agueda_table, porto_table)
#mapview::mapview(transit_table_all)
# Save the final table in gpkg format
st_write(transit_table_all, "database/transit/bus_stop_frequency.gpkg")
piggyback::pb_upload("database/transit/bus_stop_freq.gpkg")
# download.file("https://github.com/U-Shift/SiteSelection/releases/download/0.1/bus_stop_freq.gpkg", "database/transit/bus_stop_freq.gpkg")
# list municipalities with GTFS -------------------------------------------
municipios = list(
c(
"Lisboa",
"Oeiras",
"Amadora",
"Sintra",
"Cascais",
"Barreiro",
"Braga",
"Agueda", # Águeda?
"Porto"
)
)