get_trip_geometry: Get trip geometry

View source: R/get_trip_geometry.R

get_trip_geometryR Documentation

Get trip geometry

Description

Returns the geometry of each specified trip_id, based either on the shapes or the stop_times file (or both).

Usage

get_trip_geometry(gtfs, trip_id = NULL, file = NULL, crs = 4326)

Arguments

gtfs

A GTFS object, as created by read_gtfs().

trip_id

A character vector including the trip_ids to have their geometries generated. If NULL (the default), the function generates geometries for every trip_id in the GTFS.

file

A character vector specifying the file from which geometries should be generated (either one of or both shapes and stop_times). If NULL (the default), the function attemps to generate the geometries from both files, but only raises an error if none of the files exist.

crs

The CRS of the resulting object, either as an EPSG code or as an crs object. Defaults to 4326 (WGS 84).

Value

A LINESTRING sf.

Details

The geometry generation works differently for the two files. In the case of shapes, the shape as described in the text file is converted to an sf object. For stop_times the geometry is the result of linking subsequent stops along a straight line (stops' coordinates are retrieved from the stops file). Thus, the resolution of the geometry when generated with shapes tends to be much higher than when created with stop_times.

Examples

data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")

gtfs <- read_gtfs(data_path)

trip_geometry <- get_trip_geometry(gtfs)
head(trip_geometry)

# the above is identical to
trip_geometry <- get_trip_geometry(gtfs, file = c("shapes", "stop_times"))
head(trip_geometry)

trip_ids <- c("CPTM L07-0", "2002-10-0")
trip_geometry <- get_trip_geometry(gtfs, trip_id = trip_ids)
trip_geometry
plot(trip_geometry["origin_file"])


gtfstools documentation built on Nov. 24, 2022, 5:09 p.m.