Skip to contents

This function takes spatial points and projects them onto a route (i.e., "snaps" them to the nearest point on the shape), returning the linear distance of each point along the route shape, starting from the route's beginning terminal.

Usage

project_onto_route(
  shape_geometry,
  points,
  original_crs = 4326,
  project_crs = 4326
)

Arguments

shape_geometry

The SF object to project onto. Must include the field shape_id. See get_shape_geometry().

points

Can be either: a dataframe representing point coordinates, with fields longitude and latitude; or, an SF or SFC point object.

original_crs

Optional. A numeric EPSG identifier. If a dataframe is provided for points, this will be used to define the coordinate system of the longitude / latitude values. Default is 4326 (WGS 84 ellipsoid).

project_crs

Optional. A numeric EPSG identifer indicating the coordinate system to use for spatial calculations. Consider setting to a Euclidian projection, such as the appropriate UTM zone. Default is 4326 (WGS 84 ellipsoid).

Value

The points input (either dataframe or SF) with an appended column for the linear distance along the route. If points is an SFC, a vector of numeric distances is returned. Units are those of the spatial projection set in project_crs (e.g., meters if using WGS UTM).

Examples

# Set my parameters
my_crs <- 32611

# Get shape data
lineE_shape <- new_transittraj_data("get_shape_geometry")

# Set points of interest
my_points <- data.frame(longitude = c(-118.270924, -118.230056),
                        latitude = c(34.033895, 34.047884),
                        poi_name = c("Flower & Washington",
                                     "1st St Viaduct"))

# Run project_onto_route
my_points_proj <- project_onto_route(shape_geometry = lineE_shape,
                                     points = my_points,
                                     project_crs = my_crs)
head(my_points_proj)
#>   longitude latitude            poi_name distance
#> 1 -118.2709 34.03390 Flower & Washington 22573.28
#> 2 -118.2301 34.04788      1st St Viaduct 27758.35