View source: R/set_trip_speed.R
| set_trip_speed | R Documentation |
Sets the average speed of each specified trip_id, or of a segment of it,
by changing the arrival_time and departure_time columns in stop_times.
set_trip_speed(
gtfs,
trip_id = NULL,
speed,
unit = "km/h",
first_stop = NULL,
last_stop = NULL,
from = NULL,
to = NULL,
route_type = NULL,
by_reference = FALSE
)
gtfs |
A GTFS object, as created by |
trip_id |
A string vector including the |
speed |
A numeric representing the speed to be set. Its length must
either equal 1, in which case the value is recycled for all
|
unit |
A string representing the unit in which the speed is given. One
of |
first_stop |
A string. The |
last_stop |
A string. The |
from |
A string, in the "HH:MM:SS" format. Only trips that depart from
the segment's first stop at or after |
to |
A string, in the "HH:MM:SS" format. Only trips that depart from
the segment's first stop at or before |
route_type |
An integer vector of |
by_reference |
Whether to update |
If by_reference is set to FALSE, returns a GTFS object with the
time columns of its stop_times adjusted. Else, returns a GTFS object
invisibly (note that in this case the original GTFS object is altered).
The duration of each trip's segment, from the departure at first_stop to
the arrival at last_stop, is set to its length divided by speed.
Lengths are calculated by get_trip_length() along the trip's shape, or as
straight lines between stops, with a warning, if there are no shapes; the
stops table is required. Trips whose length is NA (e.g. not linked to a
shape) are left unchanged.
The times at stops strictly inside the segment are set to "" (written as
NA by write_gtfs()). Times after last_stop are shifted by the change
in the segment's duration. Dwell times at first_stop and last_stop are
kept, except at the trip's first and last stops, whose dwell time becomes
0. first_stop is matched at its first visit in stop_sequence order, and
last_stop at its next visit after that (which may be the same stop).
Blank times at last_stop are filled if it's the trip's last stop.
With first_stop or last_stop, trips that don't visit first_stop and
then last_stop, or whose departure at first_stop is blank, or whose
times at last_stop are both blank (unless it's the trip's last stop), are
left unchanged, with a warning if they were given in trip_id. Without
first_stop, last_stop, from and to, a blank departure at the trip's
first stop leaves all the times of the trip blank, without a warning.
from and to are compared with the departure_time at first_stop as
written in stop_times (template times, for trips listed in
frequencies), so use times past "24:00:00" for departures after
midnight; trips whose departure there is blank are then left unchanged.
Existing _secs columns in stop_times are updated to match.
set_route_speed(), get_trip_speed(), get_route_speed()
library(dplyr)
data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
gtfs <- read_gtfs(data_path)
gtfs_new_speed <- set_trip_speed(gtfs, trip_id = "CPTM L07-0", 50)
gtfs_new_speed$stop_times |>
filter(trip_id == "CPTM L07-0")
# use the unit argument to change the speed unit
gtfs_new_speed <- set_trip_speed(
gtfs,
trip_id = "CPTM L07-0",
speed = 15,
unit = "m/s"
)
gtfs_new_speed$stop_times |>
filter(trip_id == "CPTM L07-0")
# set the speed only between two stops. later stops are shifted
gtfs_new_speed <- set_trip_speed(
gtfs,
trip_id = "CPTM L07-0",
speed = 30,
first_stop = "18917",
last_stop = "18922"
)
gtfs_new_speed$stop_times |>
filter(trip_id == "CPTM L07-0")
# set the speed only of the trips that depart within a time of day
poa_path <- system.file("extdata/poa_gtfs.zip", package = "gtfstools")
poa_gtfs <- read_gtfs(poa_path)
poa_new_speed <- set_trip_speed(
poa_gtfs,
trip_id = poa_gtfs$trips$trip_id,
speed = 25,
from = "07:00:00",
to = "09:00:00"
)
# the arrival at the last stop of a trip that departs at 07:02 changes
poa_gtfs$stop_times |>
filter(trip_id == "T2-1@1#702") |>
slice_tail(n = 1)
poa_new_speed$stop_times |>
filter(trip_id == "T2-1@1#702") |>
slice_tail(n = 1)
# original gtfs remains unchanged
gtfs$stop_times |>
filter(trip_id == "CPTM L07-0")
# when doing by reference, original gtfs is changed
set_trip_speed(gtfs, trip_id = "CPTM L07-0", 50, by_reference = TRUE)
gtfs$stop_times |>
filter(trip_id == "CPTM L07-0")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.