Skip to content

Commit

Permalink
Re-add n_segments, close #558
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Apr 26, 2024
1 parent 78d3f6f commit e264ca8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
38 changes: 26 additions & 12 deletions R/linefuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ line_midpoint <- function(l, tolerance = NULL) {
#'
#' @inheritParams line2df
#' @param segment_length The approximate length of segments in the output (overrides n_segments if set)
#' @param n_segments The number of segments to divide the line into.
#' If there are multiple lines, this should be a vector of the same length.
#' @param use_rsgeo Should the `rsgeo` package be used?
#' If `rsgeo` is available, this faster implementation is used by default.
#' If `rsgeo` is not available, the `lwgeom` package is used.
Expand All @@ -183,40 +185,51 @@ line_midpoint <- function(l, tolerance = NULL) {
line_segment <- function(
l,
segment_length = NA,
n_segments = NA,
use_rsgeo = NULL,
debug_mode = FALSE) {
# Defensive programming:
if (is.na(segment_length) && is.na(n_segments)) {
rlang::abort(
"segment_length or n_segments must be set.",
call = rlang::caller_env()
)
}
UseMethod("line_segment")
}
#' @export
line_segment.sf <- function(
l,
segment_length = NA,
n_segments = NA,
use_rsgeo = NULL,
debug_mode = FALSE) {
if (is.na(segment_length)) {
rlang::abort(
"`segment_length` must be set.",
call = rlang::caller_env()
)
}
debug_mode = FALSE
) {
# Decide whether to use rsgeo or lwgeom, if not set:
if (is.null(use_rsgeo)) {
use_rsgeo <- use_rsgeo(l)
}
if (use_rsgeo) {
# If using rsgeo, we can do the whole thing in one go:
segment_lengths <- as.numeric(sf::st_length(l))
n_segments <- n_segments(segment_lengths, segment_length)
if (is.na(n_segments)) {
segment_lengths <- as.numeric(sf::st_length(l))
n_segments <- n_segments(segment_lengths, segment_length)
}
res <- line_segment_rsgeo(l, n_segments = n_segments)
return(res)
}
# lwgeom implementation:
n_row_l <- nrow(l)
if (n_row_l > 1) {
res_list <- pbapply::pblapply(seq(n_row_l), function(i) {
if (debug_mode) {
message(paste0("Processing row ", i, " of ", n_row_l))
}
l_segmented <- line_segment1(l[i, ], n_segments = NA, segment_length = segment_length)
if (is.na(n_segments)) {
l_segmented <- line_segment1(l[i, ], n_segments = NA, segment_length = segment_length)
} else {
l_segmented <- line_segment1(l[i, ], n_segments = n_segments[i], segment_length = NA)
}
res_names <- names(sf::st_drop_geometry(l_segmented))
# Work-around for https://github.com/ropensci/stplanr/issues/531
if (i == 1) {
Expand All @@ -233,11 +246,11 @@ line_segment.sf <- function(
res
}


#' @export
line_segment.sfc_LINESTRING <- function(
l,
segment_length = NA,
n_segments = NA,
use_rsgeo = NULL,
debug_mode = FALSE) {
l <- sf::st_as_sf(l)
Expand Down Expand Up @@ -267,7 +280,8 @@ line_segment.sfc_LINESTRING <- function(
line_segment1 <- function(
l,
n_segments = NA,
segment_length = NA) {
segment_length = NA
) {
UseMethod("line_segment1")
}
#' @export
Expand Down
19 changes: 15 additions & 4 deletions man/line_segment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/line_segment1.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e264ca8

Please sign in to comment.