diff --git a/R/line_segment.R b/R/line_segment.R new file mode 100644 index 00000000..1e5280c3 --- /dev/null +++ b/R/line_segment.R @@ -0,0 +1,26 @@ +#' Divide sf LINESTRING objects into regular segments +#' @inheritParams line2df +#' @param n_segments The number of segments to divide the line into +#' @param segment_length The approximate length of segments in the output (overides n_segments if set) +#' @family lines +#' @export +#' @examples +#' l <- routes_fast_sf[2, ] +#' l_seg2 <- line_segment(l = l, n_segments = 2) +#' plot(sf::st_geometry(l_seg2), col = 1:2, lwd = 5) +line_segment_sf <- function(l, n_segments, segment_length = NA) { + if (!is.na(segment_length)) { + l_length <- line_length(l) + n_segments <- round(l_length / segment_length) + } + # browser() # tests + # first_linestring = lwgeom::st_linesubstring(x = l, from = 0, to = 0.2) + from_to_sequence = seq(from = 0, to = 1, length.out = n_segments + 1) + line_segment_list = lapply(seq(n_segments), function(i) + lwgeom::st_linesubstring( + x = l, + from = from_to_sequence[i], + to = from_to_sequence[i + 1]) + ) + do.call(rbind, line_segment_list) +} \ No newline at end of file diff --git a/man/line_segment_sf.Rd b/man/line_segment_sf.Rd new file mode 100644 index 00000000..8028d654 --- /dev/null +++ b/man/line_segment_sf.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/line_segment.R +\name{line_segment_sf} +\alias{line_segment_sf} +\title{Divide sf LINESTRING objects into regular segments} +\usage{ +line_segment_sf(l, n_segments, segment_length = NA) +} +\arguments{ +\item{l}{A spatial lines object} + +\item{n_segments}{The number of segments to divide the line into} + +\item{segment_length}{The approximate length of segments in the output (overides n_segments if set)} +} +\description{ +Divide sf LINESTRING objects into regular segments +} +\examples{ +l <- routes_fast_sf[2, ] +l_seg2 <- line_segment(l = l, n_segments = 2) +plot(sf::st_geometry(l_seg2), col = 1:2, lwd = 5) +} +\seealso{ +Other lines: +\code{\link{angle_diff}()}, +\code{\link{geo_toptail}()}, +\code{\link{is_linepoint}()}, +\code{\link{line2df}()}, +\code{\link{line2points}()}, +\code{\link{line_bearing}()}, +\code{\link{line_breakup}()}, +\code{\link{line_midpoint}()}, +\code{\link{line_sample}()}, +\code{\link{line_segment}()}, +\code{\link{line_via}()}, +\code{\link{mats2line}()}, +\code{\link{n_sample_length}()}, +\code{\link{n_vertices}()}, +\code{\link{onewaygeo}()}, +\code{\link{points2line}()}, +\code{\link{toptail_buff}()}, +\code{\link{toptailgs}()}, +\code{\link{update_line_geometry}()} +} +\concept{lines}