R/get_segments_vectorized.R

Defines functions get_segments_vectorized

## Creates list of LINESTRING sfg for each sub-segment in an edge using
## the xy coordinates of the line vertices as input

get_segments_vectorized <- function(x) {
  ## Get number of vertices
  nx <- nrow(x)

  ## Displace matrix of coordinates by one position and remove column L1
  x1 <- x[1:(nx - 1), -3]
  x2 <- x[2:nx, -3]

  ## In a vectorised fashion, create new matrices of sub-segments and
  ## store in a list
  segments <- lapply(1:(nx - 1), function(x) st_linestring(as.matrix(rbind(x1[x, ], x2[x, ]))))

  return(segments)
}

Try the SSNbler package in your browser

Any scripts or data that you put into this service are public.

SSNbler documentation built on Sept. 30, 2024, 9:44 a.m.