Type: | Package |
Title: | Read and Write General Transit Feed Specification (GTFS) Files |
Version: | 1.2.0 |
Description: | Tools for the development of packages related to General Transit Feed Specification (GTFS) files. Establishes a standard for representing GTFS feeds using R data types. Provides fast and flexible functions to read and write GTFS feeds while sticking to this standard. Defines a basic 'gtfs' class which is meant to be extended by packages that depend on it. And offers utility functions that support checking the structure of GTFS objects. |
License: | MIT + file LICENSE |
URL: | https://r-transit.github.io/gtfsio/, https://github.com/r-transit/gtfsio |
BugReports: | https://github.com/r-transit/gtfsio/issues |
Imports: | data.table, fs, utils, zip, jsonlite |
Suggests: | knitr, rmarkdown, tinytest |
VignetteBuilder: | knitr |
Encoding: | UTF-8 |
RoxygenNote: | 7.3.2 |
Collate: | 'gtfsio_error.R' 'assert_gtfs.R' 'assert_inputs.R' 'checks.R' 'data.R' 'export_gtfs.R' 'get_gtfs_standards.R' 'gtfs_methods.R' 'gtfs_subset.R' 'gtfsio.R' 'import_gtfs.R' 'new_gtfs.R' 'utils.R' |
LazyData: | true |
Depends: | R (≥ 3.1.0) |
NeedsCompilation: | no |
Packaged: | 2024-10-11 19:01:29 UTC; b14912846767 |
Author: | Daniel Herszenhut |
Maintainer: | Daniel Herszenhut <dhersz@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2024-10-11 23:30:02 UTC |
gtfsio: Read and Write General Transit Feed Specification (GTFS) Data
Description
Tools for the development of GTFS-related packages. Establishes a standard
for representing GTFS feeds using R data types. Provides fast and flexible
functions to read and write GTFS feeds while sticking to this standard.
Defines a basic gtfs
class which is meant to be extended by packages
that depend on it. And offers utility functions that support checking the
structure of GTFS objects.
Author(s)
Maintainer: Daniel Herszenhut dhersz@gmail.com (ORCID)
Authors:
Flavio Poletti flavio.poletti@hotmail.ch
Mark Padgham mark.padgham@email.com
Other contributors:
Rafael H. M. Pereira rafa.pereira.br@gmail.com (ORCID) [reviewer]
Tom Buckley tom@tbuckl.com [reviewer]
Ipea - Institute for Applied Economic Research [copyright holder, funder]
See Also
Useful links:
Report bugs at https://github.com/r-transit/gtfsio/issues
Subset a GTFS object
Description
Subsetting a GTFS object using [
preserves the object class.
Usage
## S3 method for class 'gtfs'
x[value]
Arguments
x |
A GTFS object. |
value |
Either a numeric or a character vector. Designates the elements to be returned. |
Value
A GTFS object.
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
names(gtfs)
class(gtfs)
small_gtfs <- gtfs[1:5]
names(small_gtfs)
class(small_gtfs)
small_gtfs <- gtfs[c("shapes", "trips")]
names(small_gtfs)
class(small_gtfs)
GTFS object validator
Description
Asserts that a GTFS object is valid. Valid objects are those in which:
Every element is named.
Every element inherits from
data.frame
s.
The exception to the second rule are objects that contain an element named
"."
. In such case, this element is actually composed by a named list of
elements who inherit from data.frame
s.
Usage
assert_gtfs(x)
Arguments
x |
A GTFS object. |
Value
The same GTFS object passed to x
.
See Also
Other constructors:
new_gtfs()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
gtfs <- assert_gtfs(gtfs)
Assert that an input is a vector/list with desired properties
Description
Assert that an input is a vector/list with desired properties
Usage
assert_vector(
x,
class,
len = NULL,
null_ok = FALSE,
var_name = NULL,
subset_of = NULL,
named = FALSE,
n_call = -1L
)
assert_list(x, len = NULL, null_ok = FALSE, named = FALSE)
assert_class(x, class, n_call = -1)
Arguments
x |
A vector/list. The input to be analysed. |
class |
A string. The class that this input should inherit from. |
len |
An integer. The length of the vector. If |
null_ok |
A logical. Whether the input could also be |
var_name |
A string. The name of the variable in the informative
message. If |
subset_of |
A character vector. If |
named |
A logical. Whether the input should be a named vector/list. If
|
n_call |
A negative integer. The number of frames to go back to find the call to associate the error generated by the function with. Defaults to the parent frame (-1). This argument is only relevant to other assertion functions that build on top of this one. |
Value
TRUE
if the check is successful. Throws an error describing the
issue with the input otherwise.
Check the classes of fields in a GTFS object element
Description
Checks the classes of fields, represented by columns, inside a GTFS object element.
Usage
check_field_class(x, file, fields, classes)
assert_field_class(x, file, fields, classes)
Arguments
x |
A GTFS object. |
file |
A string. The element, that represents a GTFS text file, whose fields' classes should be checked. |
fields |
A character vector. The fields to have their classes checked. |
classes |
A character vector, with the same length of |
Value
check_field_class
returns TRUE
if the check is successful, and
FALSE
otherwise.
assert_field_class
returns x
invisibly if the check is
successful, and throws an error otherwise.
See Also
Other checking functions:
check_field_exists()
,
check_file_exists()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
check_field_class(
gtfs,
"calendar",
fields = c("monday", "tuesday"),
classes = rep("integer", 2)
)
check_field_class(
gtfs,
"calendar",
fields = c("monday", "tuesday"),
classes = c("integer", "character")
)
Check the existence of fields in a GTFS object element
Description
Checks the existence of fields, represented by columns, inside a GTFS object element.
Usage
check_field_exists(x, file, fields)
assert_field_exists(x, file, fields)
Arguments
x |
A GTFS object. |
file |
A string. The element, that represents a GTFS text file, where fields should be searched. |
fields |
A character vector. The fields to check the existence of. |
Value
check_field_exists
returns TRUE
if the check is successful, and
FALSE
otherwise.
assert_field_exists
returns x
invisibly if the check is
successful, and throws an error otherwise.
See Also
Other checking functions:
check_field_class()
,
check_file_exists()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
check_field_exists(gtfs, "calendar", c("monday", "tuesday"))
check_field_exists(gtfs, "calendar", c("monday", "oi"))
Check the existence of text files in a GTFS object
Description
Checks the existence of elements inside a GTFS object that represent specific GTFS text files.
Usage
check_file_exists(x, files)
assert_file_exists(x, files)
Arguments
x |
A GTFS object. |
files |
A character vector. The files to check the existence of. |
Value
check_file_exists
returns TRUE
if the check is successful, and
FALSE
otherwise.
assert_file_exists
returns x
invisibly if the check is
successful, and throws an error otherwise.
See Also
Other checking functions:
check_field_class()
,
check_field_exists()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
check_file_exists(gtfs, c("calendar", "agency"))
check_file_exists(gtfs, c("calendar", "oi"))
Export GTFS objects
Description
Writes GTFS objects to disk as GTFS transit feeds. The object must be
formatted according to the standards for reading and writing GTFS transit
feeds, as specified in gtfs_reference
(i.e. data types are
not checked). If present, does not write auxiliary tables held in a sub-list
named "."
.
Usage
export_gtfs(
gtfs,
path,
files = NULL,
standard_only = FALSE,
compression_level = 9,
as_dir = FALSE,
overwrite = TRUE,
quiet = TRUE
)
Arguments
gtfs |
A GTFS object. |
path |
A string. Where the resulting |
files |
A character vector. The name of the elements to be written to the feed. |
standard_only |
A logical. Whether only standard files and fields should
be written (defaults to |
compression_level |
A numeric, between 1 and 9. The higher the value, the best the compression, which demands more processing time. Defaults to 9 (best compression). |
as_dir |
A logical. Whether the feed should be exported as a directory,
instead of a |
overwrite |
A logical. Whether to overwrite an existing |
quiet |
A logical. Whether to hide log messages and progress bars
(defaults to |
Value
Invisibly returns the same GTFS object passed to gtfs
.
See Also
Other io functions:
import_gtfs()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
tmpf <- tempfile(pattern = "gtfs", fileext = ".zip")
export_gtfs(gtfs, tmpf)
zip::zip_list(tmpf)$filename
export_gtfs(gtfs, tmpf, files = c("shapes", "trips"))
zip::zip_list(tmpf)$filename
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()
.
Usage
get_gtfs_standards()
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.
Value
A named list, in which each element represents the R equivalent of each GTFS table standard.
Details
GTFS standards were derived from GTFS 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
See Also
Examples
## Not run:
gtfs_standards <- get_gtfs_standards()
## End(Not run)
GTFS reference
Description
The data from the official GTFS specification document parsed to a list. Revision date:
2024-08-16
.
Usage
gtfs_reference
Format
A list with data for every GTFS file. Each named list element (also a list) has specifications for one GTFS file in the following structure:
File_Name
: file name including file extension (txt or geojson)File_Presence
: Presence condition applied to the filefile
: file name without file extensionfile_ext
: file extensionfields
: data.frame with parsed field specification (columns:Field_Name
,Type
,Presence
,Description
,gtfsio_type
)primary_key
: primary key as vectorfield_types
: named vector on how GTFS types (values) should be read in gtfsio (names). Values are the same as infields
.
Details
GTFS Types are converted to R types in gtfsio according to the following list:
Array =
geojson_array
Color =
character
Currency amount =
numeric
Currency code =
character
Date =
integer
Email =
character
Enum =
character, integer
Float =
numeric
ID =
character
Integer =
integer
Language code =
character
Latitude =
numeric
Longitude =
numeric
Non-negative float =
numeric
Non-negative integer =
integer
Non-null integer =
integer
Non-zero integer =
integer
Object =
geojson_object
Phone number =
character
Positive float =
numeric
Positive integer =
integer
String =
character
Text =
character
Text or URL or Email or Phone number =
character
Time =
character
Timezone =
character
URL =
character
Unique ID =
character
Source
https://github.com/google/transit/blob/master/gtfs/spec/en/reference.md
gtfsio's custom error condition constructor
Description
gtfsio's custom error condition constructor
Usage
gtfsio_error(message, subclass = character(0), call = sys.call(-1))
Arguments
message |
The message to inform about the error. |
subclass |
The subclass of the error. |
call |
A call to associate the error with. |
See Also
Other error constructors:
parent_function_error()
Import GTFS transit feeds
Description
Imports GTFS transit feeds from either a local .zip
file or an URL.
Columns are parsed according to the standards for reading and writing GTFS
feeds specified in gtfs_reference
.
Usage
import_gtfs(
path,
files = NULL,
fields = NULL,
extra_spec = NULL,
skip = NULL,
quiet = TRUE,
encoding = "unknown"
)
Arguments
path |
A string. The path to a GTFS |
files |
A character vector. The text files to be read from the GTFS,
without the |
fields |
A named list. The fields to be read from each text file, in the
format |
extra_spec |
A named list. Custom specification used when reading
undocumented fields, in the format
|
skip |
A character vector. Text files that should not be read from the
GTFS, without the |
quiet |
A logical. Whether to hide log messages and progress bars
(defaults to |
encoding |
A string. Passed to |
Value
A GTFS object: a named list of data frames, each one corresponding to a distinct text file from the given GTFS feed.
See Also
Other io functions:
export_gtfs()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
# read all files and columns
gtfs <- import_gtfs(gtfs_path)
names(gtfs)
names(gtfs$trips)
# read all columns from selected files
gtfs <- import_gtfs(gtfs_path, files = c("trips", "stops"))
names(gtfs)
names(gtfs$trips)
# read specific columns from selected files
gtfs <- import_gtfs(
gtfs_path,
files = c("trips", "stops"),
fields = list(
trips = c("route_id", "trip_id"),
stops = c("stop_id", "stop_lat", "stop_lon")
)
)
GTFS object constructor
Description
Creates a GTFS object. Mostly useful for package authors who may want to
either create gtfs
objects in their packages or create subclasses of
the main gtfs
class. The usage of this function assumes some knowledge
on gtfs
objects, thus inputs are not extensively checked. See
assert_gtfs
for more thorough checks.
Usage
new_gtfs(x, subclass = character(), ...)
Arguments
x |
A GTFS-like object (either a GTFS object or a named list). Each element must represent a distinct GTFS text file. |
subclass |
A character vector. Subclasses to be assigned to the
|
... |
Name-value pairs. Additional attributes. |
Value
A GTFS object: a named list of data frames, each one corresponding to
a distinct GTFS text file, with gtfs
and list
classes.
See Also
Other constructors:
assert_gtfs()
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
tmpdir <- tempfile(pattern = "new_gtfs_example")
zip::unzip(gtfs_path, exdir = tmpdir)
agency <- data.table::fread(file.path(tmpdir, "agency.txt"))
stops <- data.table::fread(file.path(tmpdir, "stops.txt"))
routes <- data.table::fread(file.path(tmpdir, "routes.txt"))
trips <- data.table::fread(file.path(tmpdir, "trips.txt"))
stop_times <- data.table::fread(file.path(tmpdir, "stop_times.txt"))
calendar <- data.table::fread(file.path(tmpdir, "calendar.txt"))
txt_files <- list(
agency = agency,
stops = stops,
routes = routes,
trips = trips,
stop_times = stop_times,
calendar = calendar
)
gtfs <- new_gtfs(txt_files)
class(gtfs)
names(gtfs)
Parent error function constructor
Description
Creates a function that raises an error that is assigned to the function in
which the error was originally seen. Useful to prevent big repetitive
gtfsio_error()
calls in the "main" functions.
Usage
parent_function_error(message, subclass = character(0))
Arguments
message |
The message to inform about the error. |
subclass |
The subclass of the error. |
See Also
Other error constructors:
gtfsio_error()
Print a GTFS object
Description
Prints a GTFS object suppressing the class
attribute.
Usage
## S3 method for class 'gtfs'
print(x, ...)
Arguments
x |
A GTFS object. |
... |
Optional arguments ultimately passed to |
Value
The GTFS object that was printed, invisibly.
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
# subset 'gtfs' for a smaller output
gtfs <- gtfs[c("routes", "trips")]
print(gtfs)
Read a GTFS text file
Description
Reads a GTFS text file from the main .zip
file.
Usage
read_files(file, fields, extra_spec, tmpdir, quiet, encoding)
Arguments
file |
A string. The name of the file (with |
fields |
A named list. Passed by the user to |
extra_spec |
A named list. Passed by the user to
|
tmpdir |
A string. The path to the temporary folder where GTFS text files were unzipped to. |
quiet |
Whether to hide log messages and progress bars (defaults to TRUE). |
encoding |
A string. Passed to |
Value
A data.table
representing the desired text file according to
the standards for reading and writing GTFS feeds with R.
See Also
Read geojson file
Description
Read geojson file
Usage
read_geojson(file.geojson)
Arguments
file.geojson |
geojson file |
Print summary of a GTFS object
Description
Print summary of a GTFS object
Usage
## S3 method for class 'gtfs'
summary(object, ...)
Arguments
object |
A GTFS object. |
... |
Ignored. |
Examples
gtfs_path <- system.file("extdata/ggl_gtfs.zip", package = "gtfsio")
gtfs <- import_gtfs(gtfs_path)
summary(gtfs)
Translate GTFS specification types to R equivalent types
Description
Translate GTFS specification types to R equivalent types
Usage
translate_types(text_file, r_equivalents)
Arguments
text_file |
A named |
r_equivalents |
A named |
Value
A named list
holding a GTFS text file specification, but with
R data types instead of GTFS spec data types.