st_distance_along: Compute Euclidian or great circle distance along a sequence...

View source: R/st_distance_along.R

st_distance_alongR Documentation

Compute Euclidian or great circle distance along a sequence of geometries

Description

Compute Euclidian or great circle distance along a sequence of geometries

Usage

st_distance_along(
  x,
  ...,
  which = ifelse(isTRUE(sf::st_is_longlat(x)), "Great Circle", "Euclidean"),
  par = 0,
  tolerance = 0
)

Arguments

x

object of class sf, sfc or sfg

...

passed on to s2_distance or s2_distance_matrix

which

character; for Cartesian coordinates only: one of Euclidean, Hausdorff or Frechet; for geodetic coordinates, great circle distances are computed; see details

par

for which equal to Hausdorff or Frechet, optionally use a value between 0 and 1 to densify the geometry

tolerance

ignored if sf::st_is_longlat(x) is FALSE; otherwise, if set to a positive value, the first distance smaller than tolerance will be returned, and true distance may be smaller; this may speed up computation. In meters, or a units object convertible to meters.

Details

For details see st_distance since st_distance_along() is built-on that function.

Value

Returns a numeric vector of length x starting with 0 for the first geometry, followed by the cumulative sums of distances between the geometries along the sequence of x. If a geometry is empty the corresponding value and any following value in vector are NA.

If the coordinate reference system of x was set, the distances are returned with corresponding unit of measure; see set_units.

Examples

library(sf)
library(tmap)

storms <- st_read(system.file("shape", package = "sf"), "storms_xyz", quiet = TRUE)
storms <- st_set_crs(storms, 4326)
st_storm_30 <- storms[30, ]
st_storm_30_pt <- st_cast(st_storm_30, "POINT", warn = FALSE)
dist_along <- st_distance_along(st_storm_30_pt)
st_storm_30_pt$dist_along_km <- units::set_units(dist_along, "km") %>% round() %>% paste("km")

tm_shape(st_storm_30) + tm_lines() +
  tm_shape(st_storm_30_pt) + tm_bubbles(size = 0.5, col = "red") +
  tm_text("dist_along_km", xmod = 2, ymod = 1) +
  tm_layout(inner.margins = 0.1)


a-benini/sfhelpers documentation built on Aug. 28, 2024, 3:30 a.m.