
Filter a GTFS object using a spatial extent
Source:R/filter_by_spatial_extent.R
filter_by_spatial_extent.RdFilters a GTFS object using a spatial extent (passed as an sf object),
keeping (or dropping) entries related to shapes and trips whose geometries
are selected through a specified spatial operation.
Usage
filter_by_spatial_extent(
gtfs,
geom,
spatial_operation = sf::st_intersects,
keep = TRUE
)Arguments
- gtfs
A GTFS object, as created by
read_gtfs().- geom
An
sf,sfcorbboxobject, in WGS 84 (EPSG 4326). Describes the spatial extent used to filter the data. Abboxis treated as a rectangle in longitude and latitude (unlikesf::st_as_sfc(bbox), whose edges are geodesics when{s2}is used).- spatial_operation
A spatial operation function from the set of options listed in geos_binary_pred. Defaults to
sf::st_intersects, which tests if the shapes and trips have ANY intersection with the object specified ingeom. Please note thatgeomis passed as thexargument of these functions.sf::st_equals_exact()andsf::st_is_within_distance()are not supported, since they require additional arguments.- keep
A logical. Whether the entries related to the shapes and trips selected by the given spatial operation should be kept or dropped (defaults to
TRUE, which keeps the entries).
Examples
data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
gtfs <- read_gtfs(data_path)
shape_id <- "68962"
shape_sf <- get_shapes_geometry(gtfs, shape_id)
bbox <- sf::st_bbox(shape_sf)
object.size(gtfs)
#> 811304 bytes
# keeps entries that intersect with the specified polygon
smaller_gtfs <- filter_by_spatial_extent(gtfs, bbox)
object.size(smaller_gtfs)
#> 325096 bytes
# drops entries that intersect with the specified polygon
smaller_gtfs <- filter_by_spatial_extent(gtfs, bbox, keep = FALSE)
object.size(smaller_gtfs)
#> 512352 bytes
# uses a different function to filter the gtfs
smaller_gtfs <- filter_by_spatial_extent(
gtfs,
bbox,
spatial_operation = sf::st_contains
)
object.size(smaller_gtfs)
#> 69040 bytes