View source: R/set_max_speed.R
| set_max_speed | R Documentation |
Caps the speed of the specified trips: each part of a trip that is faster
than max_speed is slowed down to max_speed, by changing the
arrival_time and departure_time columns in stop_times. Parts of the
trips that are already slower are left unchanged.
set_max_speed(
gtfs,
max_speed,
unit = "km/h",
route_id = NULL,
trip_id = NULL,
first_stop = NULL,
last_stop = NULL,
from = NULL,
to = NULL,
route_type = NULL
)
gtfs |
A GTFS object, as created by |
max_speed |
A number. The maximum speed allowed, in the unit given by
|
unit |
A string representing the unit in which the speed is given. One
of |
route_id |
A character vector including the |
trip_id |
A character vector including the |
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 |
A GTFS object with the time columns of its stop_times adjusted.
A span is the part of a trip between two consecutive stops with times;
stops with blank times inside it are kept blank. Its speed is its length,
calculated by get_trip_length() along the trip's shape (or, with a
warning, as straight lines between stops if there are no shapes; the
stops table is required), divided by the time from the departure at its
first stop to the arrival at its second. Spans whose length is NA (e.g.
in trips not linked to a shape) are left unchanged.
Each span faster than max_speed, or with a zero duration, has its duration
raised to its length divided by max_speed, rounded up to the next second.
All the later times of the trip are shifted by the accumulated extra time,
so dwell times are kept. Spans whose arrival is earlier than the previous
departure (usually times after midnight written as "00:30:00" instead of
"24:30:00") are left unchanged, with a warning. An error is raised if a
shift would result in a time later than "9999:59:59". Only stop_times
is modified; its existing _secs columns are updated to match.
With first_stop and last_stop, matched as in set_trip_speed(), only
the spans between them are capped: times before first_stop don't change,
and the times at and after last_stop are shifted. The given stops must
have a time, so that no span crosses them (if only last_stop is given,
the trip's first stop needs none). Trips that don't visit first_stop and
then last_stop, or have blank times there, are left unchanged, with a
warning if they were given in trip_id or route_id.
from and to are compared with the departure at the segment's first stop
(or its arrival, if the departure is blank) as written in stop_times
(template times, for trips listed in frequencies), so use times past
"24:00:00" for departures after midnight. Trips without a time there are
left unchanged.
set_trip_speed(), get_trip_speed()
data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")
gtfs <- read_gtfs(data_path)
# the trip is faster than 30 km/h between some of its stops
get_trip_speed(gtfs, "CPTM L07-0", by = "segment")
gtfs_capped <- set_max_speed(gtfs, max_speed = 30, trip_id = "CPTM L07-0")
get_trip_speed(gtfs_capped, "CPTM L07-0", by = "segment")
# the later stops are shifted, so the trip ends later
get_start_and_end_times(gtfs, trip_id = "CPTM L07-0")
get_start_and_end_times(gtfs_capped, trip_id = "CPTM L07-0")
# cap the speed only between two stops. the spans before and after them
# are not capped
gtfs_capped <- set_max_speed(
gtfs,
max_speed = 30,
trip_id = "CPTM L07-0",
first_stop = "18921",
last_stop = "18974"
)
get_trip_speed(gtfs_capped, "CPTM L07-0", by = "segment")
# cap the speed of all the trips of all routes of the GTFS feed
gtfs_capped <- set_max_speed(gtfs, max_speed = 8)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.