selection: Select Subsets of GTFS Data

View source: R/selection.R

selectionR Documentation

Select Subsets of GTFS Data

Description

The “selection” function makes a selection in the GTFS file without altering or filtering the GTFS file.

Usage

selection(gtfs, ..., add = FALSE)

unselection(gtfs)

Arguments

gtfs

An object representing GTFS data. It can be a list or a 'wizardgtfs' class gtfsect.

...

Expressions used to filter the data within 'gtfs'. The expressions can operate on four GTFS variables:

"stop_id"

Select the GTFS by stops using a vector of stop_id, must be character.

"route_id"

Select the GTFS by routes using a vector of route_id, must be character.

"trip_id"

Select the GTFS by trip using a vector of trip_id, must be character.

"geometry"

Select the GTFS by stops using an 'sf', 'sfc', or 'sfg' object. The geometry predicate function is evaluated with the geometry of the GTFS stops. Available predicates are:

%intersects%

%touches%

%within%

%equals%

%overlaps%

%contains%.

add

A logical argument. If 'TRUE', appends the new selection to existing ones in the gtfsect; otherwise, creates a new selection.

Details

The function evaluates the provided expressions in an environment restricted to recognized variables ('stop_id', 'route_id', 'trip_id', 'geometry'). An error is thrown if an unrecognized variable is used, indicating that only specific variables are allowed.

Value

A 'wizardgtfs_selected' wizardgtfs, which is a modified version of the original attributes with the selections applied. If the expression yields no matches, returns the original gtfs unchanged.

Examples

# Apply the selection function
result <- selection(for_rail_gtfs,
 stop_id == for_rail_gtfs$stops$stop_id[1] & trip_id %in% for_rail_gtfs$trips$trip_id[1:5])

# Check the selection
class(result)
attr(result, 'selection')

# Use geometry selection
bbox <- sf::st_bbox(c(
  xmin = -38.57219059002416,
  ymin = -3.7999496173114118,
  xmax = -38.50455165901261,
  ymax = -3.756631724636505
),
crs = sf::st_crs(4326))  # Set CRS to WGS 84

# Convert the bounding box to a polygon
polygon <- sf::st_as_sfc(bbox)

result <- selection(for_rail_gtfs, geometry %intersects% polygon)


GTFSwizard documentation built on April 4, 2025, 4:10 a.m.