get_trip_geometry: Get trip geometry

View source: R/get_trip_geometry.R

get_trip_geometryR Documentation

Get trip geometry

Description

Returns the geometry of each specified trip_id, from its first to its last stop, either along the trip's shape or as straight lines between stops.

Usage

get_trip_geometry(
  gtfs,
  trip_id = NULL,
  method = "shapes",
  crs = 4326,
  sort_sequence,
  file = NULL
)

Arguments

gtfs

A GTFS object, as created by read_gtfs().

trip_id

A character vector including the trip_ids to have their geometries generated. If NULL (the default), the function generates geometries for every trip_id in the GTFS.

method

A string, either "shapes" (the default) or "euclidean". "shapes" returns the part of the trip's shape, described in the shapes table, between its first and last stops, while "euclidean" links the consecutive stops of the trip along straight lines. If the GTFS object doesn't have a shapes table, or if its trips table doesn't have a shape_id column, "euclidean" is used instead, with a warning.

crs

The CRS of the resulting object, either as an EPSG code or as an crs object. Defaults to 4326 (WGS 84).

sort_sequence

Deprecated; do not supply. Timetables and shapes are now always sorted by stop_sequence and shape_pt_sequence. Passing TRUE raises a warning, and passing FALSE raises an error.

file

Deprecated. Use method instead (file = "stop_times" corresponds to method = "euclidean").

Value

A ⁠LINESTRING sf⁠ with the trip_id and the geometry of each trip listed in stop_times.

Details

With method = "shapes", the first and last stops of each trip are projected onto its shape, as in get_trip_length(), and the geometry is the part of the shape between them. Thus, its length matches the length returned by get_trip_length() (when this length is not NA, see below), and the limitations described there apply. Trips whose stops are all located at the same point of the shape get a zero-length geometry. Use get_shapes_geometry() for the geometry of entire shapes.

With method = "euclidean", the geometry links the consecutive stops of each trip along straight lines (stops' coordinates are retrieved from the stops table), so its resolution tends to be much lower than that of the geometry generated from the shapes. Trips with a single stop get a single-point geometry.

Both methods ignore stops with missing coordinates, or not listed in stops (for which get_trip_length() returns NA lengths). Trips without any stop with coordinates get an empty geometry, as do, with method = "shapes", trips with fewer than two stops with coordinates or not linked to a shape with at least two distinct points (the latter with a warning). Geometries along shapes that cross the antimeridian are not handled correctly.

See Also

get_trip_length(), get_shapes_geometry()

Examples


data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")

gtfs <- read_gtfs(data_path)

# geometry along the shape from the first to the last stop of each trip
trip_geometry <- get_trip_geometry(gtfs)
head(trip_geometry)

# straight lines between consecutive stops
trip_ids <- c("CPTM L07-0", "2002-10-0")
straight_geometry <- get_trip_geometry(
  gtfs,
  trip_id = trip_ids,
  method = "euclidean"
)
straight_geometry
plot(straight_geometry["trip_id"])


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