-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_gtfs_standards.R
417 lines (377 loc) · 14.7 KB
/
get_gtfs_standards.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
# nocov start
#' Generate GTFS standards
#'
#' @description
#' The dataset [gtfs_reference] now contains the standard specifications.
#' This function is deprecated and no longer used in [import_gtfs()] or [export_gtfs()].
#'
#' @details
#' Generates a list specifying the standards to be used when reading and writing
#' GTFS feeds with R. Each list element (also a list) represents a distinct GTFS
#' table, and describes:
#'
#' - whether the table is required, optional or conditionally required;
#' - the fields that compose the table, including which R data type is best
#' suited to represent it, whether the field is required, optional or
#' conditionally required, and which values it can assume (most relevant to GTFS
#' `ENUM`s.
#'
#' Note: the standards list is based on the specification as revised in May 9th,
#' 2022.
#'
#' @return A named list, in which each element represents the R equivalent of
#' each GTFS table standard.
#'
#' @section Details:
#' GTFS standards were derived from [GTFS Schedule
#' Reference](https://gtfs.org/documentation/schedule/reference/). The R data types chosen to
#' represent each GTFS data type are described below:
#'
#' - Color = `character`
#' - Currency amount = `numeric`
#' - Currency code = `character`
#' - Date = `integer`
#' - Email = `character`
#' - ENUM = `integer`
#' - ID = `character`
#' - Integer = `integer`
#' - Language code = `character`
#' - Latitude = `numeric`
#' - Longitude = `numeric`
#' - Float = `numeric`
#' - Phone number = `character`
#' - Text = `character`
#' - Time = `character`
#' - Timezone = `character`
#' - URL = `character`
#'
#' @seealso [gtfs_reference]
#'
#' @examples \dontrun{
#' gtfs_standards <- get_gtfs_standards()
#' }
#' @export
get_gtfs_standards <- function() {
.Deprecated("gtfs_reference")
agency <- list(
file_spec = "req",
agency_id = list("id", "cond"),
agency_name = list("text", "req"),
agency_url = list("url", "req"),
agency_timezone = list("timezone", "req"),
agency_lang = list("language_code", "opt"),
agency_phone = list("phone_number", "opt"),
agency_fare_url = list("url", "opt"),
agency_email = list("email", "opt")
)
stops <- list(
file_spec = "req",
stop_id = list("id", "req"),
stop_code = list("text", "opt"),
stop_name = list("text", "cond"),
tts_stop_name = list("text", "opt"),
stop_desc = list("text", "opt"),
stop_lat = list("latitude", "cond"),
stop_lon = list("longitude", "cond"),
zone_id = list("id", "cond"),
stop_url = list("url", "opt"),
location_type = list("enum", "opt", c(0, 1, 2, 3, 4)),
parent_station = list("id", "cond"),
stop_timezone = list("timezone", "opt"),
wheelchair_boarding = list("enum", "opt", c(0, 1, 2)),
level_id = list("id", "opt"),
platform_code = list("text", "opt")
)
routes <- list(
file_spec = "req",
route_id = list("id", "req"),
agency_id = list("id", "cond"),
route_short_name = list("text", "cond"),
route_long_name = list("text", "cond"),
route_desc = list("text", "opt"),
route_type = list("enum", "req", c(0, 1, 2, 3, 4, 5, 6, 7, 11,
12)),
route_url = list("url", "opt"),
route_color = list("color", "opt"),
route_text_color = list("color", "opt"),
route_sort_order = list("integer", "opt"),
continuous_pickup = list("enum", "opt", c(0, 1, 2, 3)),
continuous_dropoff = list("enum", "opt", c(0, 1, 2, 3)),
network_id = list("id", "opt")
)
trips <- list(
file_spec = "req",
route_id = list("id", "req"),
service_id = list("id", "req"),
trip_id = list("id", "req"),
trip_headsign = list("text", "opt"),
trip_short_name = list("text", "opt"),
direction_id = list("enum", "opt", c(0, 1)),
block_id = list("id", "opt"),
shape_id = list("id", "cond"),
wheelchair_accessible = list("enum", "opt", c(0, 1, 2)),
bikes_allowed = list("enum", "opt", c(0, 1, 2))
)
stop_times <- list(
file_spec = "req",
trip_id = list("id", "req"),
arrival_time = list("time", "cond"),
departure_time = list("time", "cond"),
stop_id = list("id", "req"),
stop_sequence = list("integer", "req"),
stop_headsign = list("text", "opt"),
pickup_type = list("enum", "opt", c(0, 1, 2, 3)),
drop_off_type = list("enum", "opt", c(0, 1, 2, 3)),
continuous_pickup = list("enum", "opt", c(0, 1, 2, 3)),
continuous_drop_off = list("enum", "opt", c(0, 1, 2, 3)),
shape_dist_traveled = list("float", "opt"),
timepoint = list("enum", "opt", c(0, 1))
)
calendar <- list(
file_spec = "cond",
service_id = list("id", "req"),
monday = list("enum", "req", c(0, 1)),
tuesday = list("enum", "req", c(0, 1)),
wednesday = list("enum", "req", c(0, 1)),
thursday = list("enum", "req", c(0, 1)),
friday = list("enum", "req", c(0, 1)),
saturday = list("enum", "req", c(0, 1)),
sunday = list("enum", "req", c(0, 1)),
start_date = list("date", "req", c(0, 1)),
end_date = list("date", "req", c(0, 1))
)
calendar_dates <- list(
file_spec = "cond",
service_id = list("id", "req"),
date = list("date", "req"),
exception_type = list("enum", "req", c(1, 2))
)
fare_attributes <- list(
file_spec = "opt",
fare_id = list("id", "req"),
price = list("float", "req"),
currency_type = list("currency_code", "req"),
payment_method = list("enum", "req", c(0, 1)),
transfers = list("enum", "req", c(0, 1, 2)),
agency_id = list("id", "cond"),
transfer_duration = list("integer", "opt")
)
fare_rules <- list(
file_spec = "opt",
fare_id = list("id", "req"),
route_id = list("id", "opt"),
origin_id = list("id", "opt"),
destination_id = list("id", "opt"),
contains_id = list("id", "opt")
)
fare_products <- list(
file_spec = "opt",
fare_product_id = list("id", "req"),
fare_product_name = list("text", "opt"),
amount = list("currency_amount", "req"),
currency = list("currency_code", "req")
)
fare_leg_rules <- list(
file_spec = "opt",
leg_group_id = list("id", "opt"),
network_id = list("id", "opt"),
from_area_id = list("id", "opt"),
to_area_id = list("id", "opt"),
fare_product_id = list("id", "req")
)
fare_transfer_rules <- list(
file_spec = "opt",
from_leg_group_id = list("id", "opt"),
to_leg_group_id = list("id", "opt"),
transfer_count = list("integer", "cond"),
duration_limit = list("integer", "opt"),
duration_limit_type = list("enum", "cond", c(0, 1, 2, 3)),
fare_transfer_type = list("enum", "req", c(0, 1, 2)),
fare_product_id = list("id", "opt")
)
areas <- list(
file_spec = "opt",
area_id = list("id", "req"),
area_name = list("text", "opt")
)
stop_areas <- list(
file_spec = "opt",
area_id = list("id", "req"),
stop_id = list("id", "opt")
)
shapes <- list(
file_spec = "opt",
shape_id = list("id", "req"),
shape_pt_lat = list("latitude", "req"),
shape_pt_lon = list("longitude", "req"),
shape_pt_sequence = list("integer", "req"),
shape_dist_traveled = list("float", "opt")
)
frequencies <- list(
file_spec = "opt",
trip_id = list("id", "req"),
start_time = list("time", "req"),
end_time = list("time", "req"),
headway_secs = list("integer", "req"),
exact_times = list("enum", "opt", c(0, 1))
)
transfers <- list(
file_spec = "opt",
from_stop_id = list("id", "cond"),
to_stop_id = list("id", "cond"),
from_route_id = list("id", "opt"),
to_route_id = list("id", "opt"),
from_trip_id = list("id", "cond"),
to_trip_id = list("id", "cond"),
transfer_type = list("enum", "req", c(0, 1, 2, 3, 4, 5)),
min_transfer_time = list("integer", "opt")
)
pathways <- list(
file_spec = "opt",
pathway_id = list("id", "req"),
from_stop_id = list("id", "req"),
to_stop_id = list("id", "req"),
pathway_mode = list("enum", "req", c(1, 2, 3, 4, 5, 6, 7)),
is_bidirectional = list("enum", "req", c(0, 1)),
length = list("float", "opt"),
traversal_time = list("integer", "opt"),
stair_count = list("integer", "opt"),
max_slope = list("float", "opt"),
min_width = list("float", "opt"),
signposted_as = list("text", "opt"),
reversed_signposted_as = list("text", "opt")
)
levels <- list(
file_spec = "cond",
level_id = list("id", "req"),
level_index = list("float", "req"),
level_name = list("text", "opt")
)
translations <- list(
file_spec = "opt",
table_name = list("enum", "req", c("agency", "stops", "routes",
"trips", "stop_times",
"feed_info", "pathways",
"levels", "attribution")),
field_name = list("text", "req"),
language = list("language_code", "req"),
translation = list(c("text", "url", "email", "phone_number"), "req"),
record_id = list("id", "cond"),
record_sub_id = list("id", "cond"),
field_value = list(c("text", "url", "email", "phone_number"), "cond")
)
feed_info <- list(
file_spec = "cond",
feed_publisher_name = list("text", "req"),
feed_publisher_url = list("url", "req"),
feed_lang = list("language_code", "req"),
default_lang = list("language_code", "opt"),
feed_start_date = list("date", "opt"),
feed_end_date = list("date", "opt"),
feed_version = list("text", "opt"),
feed_contact_email = list("email", "opt"),
feed_contact_url = list("url", "opt")
)
attributions <- list(
file_spec = "opt",
attribution_id = list("id", "opt"),
agency_id = list("id", "opt"),
route_id = list("id", "opt"),
trip_id = list("id", "opt"),
organization_name = list("text", "req"),
is_producer = list("enum", "opt", c(0, 1)),
is_operator = list("enum", "opt", c(0, 1)),
is_authority = list("enum", "opt", c(0, 1)),
attribution_url = list("url", "opt"),
attribution_email = list("email", "opt"),
attribution_phone = list("phone_number", "opt")
)
# create gtfs_standards object
gtfs_standards <- list(
agency = agency,
stops = stops,
routes = routes,
trips = trips,
stop_times = stop_times,
calendar = calendar,
calendar_dates = calendar_dates,
fare_attributes = fare_attributes,
fare_rules = fare_rules,
fare_products = fare_products,
fare_leg_rules = fare_leg_rules,
fare_transfer_rules = fare_transfer_rules,
areas = areas,
stop_areas = stop_areas,
shapes = shapes,
frequencies = frequencies,
transfers = transfers,
pathways = pathways,
levels = levels,
translations = translations,
feed_info = feed_info,
attributions = attributions
)
# define R types most similar to GTFS reference types
r_equivalents <- c(
color = "character",
currency_amount = "numeric",
currency_code = "character",
date = "integer",
email = "character",
enum = "integer",
id = "character",
integer = "integer",
language_code = "character",
latitude = "numeric",
longitude = "numeric",
float = "numeric",
phone_number = "character",
text = "character",
time = "character",
timezone = "character",
url = "character"
)
# translate GTFS reference types to R types
gtfs_standards <- lapply(gtfs_standards, translate_types, r_equivalents)
# correct a small special case:
# 'translations' 'table_name' field is an ENUM, but its allowed values are
# strings, not integers. this results in a warning when importing the GTFS
# using data.table::fread(). so change R equivalent to character, instead of
# integer
gtfs_standards$translations$table_name[[1]] <- "character"
return(gtfs_standards)
}
#' Translate GTFS specification types to R equivalent types
#'
#' @param text_file A named `list` containing a GTFS text file specification, as
#' described in the body of [get_gtfs_standards].
#' @param r_equivalents A named `character vector`, in which each name is the
#' GTFS specification type and the content its R equivalent.
#'
#' @return A named `list` holding a GTFS text file specification, but with
#' R data types instead of GTFS spec data types.
#'
#' @keywords internal
translate_types <- function(text_file, r_equivalents) {
# for each text_file element:
# - check if it's a list.
# - if it's not, then it's the 'file_spec' element. return 'file_spec' value
# - if it is, replace first entry (GTFS spec type) for an equivalent R type
text_file <- lapply(
text_file,
function(i) {
if (!is.list(i)) return(i)
new_spec <- i
gtfs_type <- new_spec[[1]]
r_type <- r_equivalents[gtfs_type]
# some 'translations' fields ('translation' and 'field_value') might have
# more than one GTFS data type.
# their R equivalent is always 'character', no matter the GTFS type, so we
# can safely get only the first value ('character')
if (length(r_type) > 1) r_type <- r_type[1]
new_spec[[1]] <- r_type
return(new_spec)
}
)
}
# nocov end