set_max_speed: Set trip maximum speed

View source: R/set_max_speed.R

set_max_speedR Documentation

Set trip maximum speed

Description

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.

Usage

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
)

Arguments

gtfs

A GTFS object, as created by read_gtfs().

max_speed

A number. The maximum speed allowed, in the unit given by unit.

unit

A string representing the unit in which the speed is given. One of "km/h" (the default), "m/s" or "mph" (miles per hour).

route_id

A character vector including the route_ids whose trips have their speeds capped. If route_id, trip_id and route_type are all NULL (the default), the speeds of every trip in the GTFS are capped. route_ids not listed in trips are ignored, with a warning.

trip_id

A character vector including the trip_ids to have their speeds capped. When given together with route_id, the trips of both are capped.

first_stop

A string. The stop_id where the segment whose speeds are capped starts. If NULL (the default), the segment starts at each trip's first stop.

last_stop

A string. The stop_id where the segment ends. If NULL (the default), the segment ends at each trip's last stop.

from

A string, in the "HH:MM:SS" format. Only trips that depart from the segment's first stop at or after from are changed. If NULL (the default), there is no lower limit.

to

A string, in the "HH:MM:SS" format. Only trips that depart from the segment's first stop at or before to are changed. If NULL (the default), there is no upper limit.

route_type

An integer vector of route_types (see filter_by_route_type()). If given, only trips of routes of these types are changed; together with the other selection arguments, it restricts their selection. route_types not listed in routes are ignored. If NULL (the default), trips of any type are changed.

Value

A GTFS object with the time columns of its stop_times adjusted.

Details

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.

See Also

set_trip_speed(), get_trip_speed()

Examples


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)


gtfstools documentation built on Oct. 28, 2026, 5:13 p.m.