Learn R Programming

gtfstools (version 2.0.1)

interpolate_shape_dist_traveled: Interpolate the distances travelled along shapes

Description

Calculates the distance, in meters, from the start of each shape to each stop and to each shape point, and writes it to the shape_dist_traveled columns of stop_times and shapes, replacing any existing values.

Usage

interpolate_shape_dist_traveled(gtfs)

Value

A GTFS object whose stop_times and shapes have a shape_dist_traveled column, in meters. The order of the rows and the other columns are kept. The given GTFS object is not modified.

Arguments

gtfs

A GTFS object, as created by read_gtfs().

Details

Each stop is projected onto the closest point of its trip's shape, with stops advancing along the shape in stop_sequence order, as in get_trip_length() (see there for the limitations of this method). Distances are great-circle distances on the sphere used by {s2}.

Existing shape_dist_traveled columns are entirely replaced, since their values may be in any unit, so existing values are lost even where new ones can't be calculated. Stops of trips not linked to a shape with at least two distinct points (with a warning), stops without coordinates and stops of trips with fewer than two stops with coordinates get NA, as do shape points with missing coordinates. Unlike in get_shape_length(), these points are skipped, and the other points of the shape are still measured.

See Also

get_trip_length(), get_shape_length()

Examples

Run this code
# \dontshow{
  old_dt_threads <- data.table::setDTthreads(1)
  on.exit(data.table::setDTthreads(old_dt_threads), add = TRUE)
# }
data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
gtfs <- read_gtfs(data_path)

gtfs <- interpolate_shape_dist_traveled(gtfs)
head(gtfs$stop_times[, c("trip_id", "stop_id", "shape_dist_traveled")])
head(gtfs$shapes)

Run the code above in your browser using DataLab