View source: R/get_trip_length.R
| get_trip_length | R Documentation |
Returns the length of each specified trip_id, measured either from the
first to the last stop of the trip or between each pair of consecutive
stops. Lengths can be measured along the trip's shape or as straight lines
between stops.
get_trip_length(
gtfs,
trip_id = NULL,
method = "shapes",
by = "trip",
unit = "km",
sort_sequence,
file = NULL,
from = NULL,
to = NULL
)
gtfs |
A GTFS object, as created by |
trip_id |
A character vector including the |
method |
A string, either |
by |
A string, either |
unit |
A string representing the unit in which lengths are desired.
Either |
sort_sequence |
Deprecated; do not supply. Timetables and shapes are
now always sorted by |
file |
Deprecated. Use |
from |
A string, in the "HH:MM:SS" format. With |
to |
A string, in the "HH:MM:SS" format. Only trips (or segments) that
depart from their first stop at or before |
With by = "trip", a data.table with the trip_id and the
length of each trip. With by = "segment", a data.table with the
trip_id, the segment number, the from_stop_id and to_stop_id that
delimit the segment and its length.
Segments are numbered as in get_trip_segment_duration(), so both outputs
can be joined by trip_id and segment. Trips with a single stop have a
length of 0.
With method = "shapes", each stop is projected onto the closest point of
its trip's shape, with stops advancing along the shape in stop_sequence
order, so loops and shapes that pass by the same place more than once are
handled. Some limitations remain:
a stop slightly behind the previous one along the shape (e.g. due to imprecise coordinates) is placed at the previous stop's position, so their segment has a length of 0. On shapes that pass the same place twice in the same direction, such a stop may be matched to the later pass instead;
on shapes whose outbound and return legs overlap exactly, the stops just before and after the turnaround may be placed at the same position. The trip length is unaffected, but the segment between them gets a length of 0 and the next one is longer.
shape_dist_traveled is ignored. Trips not linked to a shape with at least
two distinct points have NA lengths, with a warning. Use
get_shape_length() for the length of entire shapes.
Lengths are great-circle (haversine) distances on the sphere used by
{s2}. Stops with missing coordinates, or not listed in stops, result in
NA lengths for their segments and trips.
from and to are compared with the departure_time of the first stop of
each trip (in stop_sequence order) or segment, as written in
stop_times, so use times past "24:00:00" for departures after midnight.
For trips listed in frequencies, these are template times, not the times
of each departure. Trips and segments whose departure is blank are never
included when from or to is given.
get_shape_length(), get_trip_speed(),
get_trip_segment_duration()
data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
gtfs <- read_gtfs(data_path)
# length along the shape from the first to the last stop of each trip
trip_length <- get_trip_length(gtfs)
head(trip_length)
# length along the shape between consecutive stops
segment_length <- get_trip_length(gtfs, "CPTM L07-0", by = "segment")
head(segment_length)
# straight-line distances between consecutive stops, in meters
straight_length <- get_trip_length(
gtfs,
"CPTM L07-0",
method = "euclidean",
by = "segment",
unit = "m"
)
head(straight_length)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.