Skip to content

Commit 4eeea3e

Browse files
Merge pull request #559 from ropensci/558-re-add-n_segments-argument-to-line_segment
558 re add n segments argument to line segment
2 parents 2b68bb6 + ce874b4 commit 4eeea3e

File tree

11 files changed

+236
-31
lines changed

11 files changed

+236
-31
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `line_segment()` becomes an S3 generic which now has methods for `sf` and `sfc` class objects
66
- `line_segment()` now works around [{rsgeo} issue](https://github.com/JosiahParry/rsgeo/issues/42) with `line_segmentize()` returning fewer segments than requested (#552)
77
- Removal of offending URLs with `urlchecker::check_urls()`
8+
- Improvement of documentation of `line_midpoint()`: comparison of output with `sf::st_point_on_surface()`
89

910
# stplanr 1.1.2 (2023-09)
1011

R/linefuns.R

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ angle_diff <- function(l, angle, bidirectional = FALSE, absolute = TRUE) {
140140
#' plot(l$geometry, col = 2:5)
141141
#' midpoints <- line_midpoint(l)
142142
#' plot(midpoints, add = TRUE)
143+
#' # compare with sf::st_point_on_surface:
144+
#' midpoints2 <- sf::st_point_on_surface(l)
145+
#' plot(midpoints2, add = TRUE, col = "red")
143146
line_midpoint <- function(l, tolerance = NULL) {
144147
if (is.null(tolerance)) {
145148
sub <- lwgeom::st_linesubstring(x = l, from = 0, to = 0.5)
@@ -158,7 +161,9 @@ line_midpoint <- function(l, tolerance = NULL) {
158161
#' but does not always return the number of segments requested.
159162
#'
160163
#' @inheritParams line2df
161-
#' @param segment_length The approximate length of segments in the output (overides n_segments if set)
164+
#' @param segment_length The approximate length of segments in the output (overrides n_segments if set)
165+
#' @param n_segments The number of segments to divide the line into.
166+
#' If there are multiple lines, this should be a vector of the same length.
162167
#' @param use_rsgeo Should the `rsgeo` package be used?
163168
#' If `rsgeo` is available, this faster implementation is used by default.
164169
#' If `rsgeo` is not available, the `lwgeom` package is used.
@@ -174,49 +179,72 @@ line_midpoint <- function(l, tolerance = NULL) {
174179
#' plot(l_seg_multi["ID"])
175180
#' plot(l_seg_multi$geometry, col = seq_along(l_seg_multi), lwd = 5)
176181
#' round(st_length(l_seg_multi))
177-
#' # rsgeo implementation:
178-
#' rsmulti = line_segment(l, segment_length = 1000, use_rsgeo = TRUE)
179-
#' plot(rsmulti["ID"])
180-
#' plot(rsmulti$geometry, col = seq_along(l_seg_multi), lwd = 5)
181-
#' # round(st_length(rsmulti))
182-
#' # waldo::compare(l_seg_multi, rsmulti)
182+
#' # rsgeo implementation (default if available):
183+
#' if (rlang::is_installed("rsgeo")) {
184+
#' rsmulti = line_segment(l, segment_length = 1000, use_rsgeo = TRUE)
185+
#' plot(rsmulti["ID"])
186+
#' }
187+
#' # Check they have the same total length, to nearest mm:
188+
#' # round(sum(st_length(l_seg_multi)), 3) == round(sum(st_length(rsmulti)), 3)
189+
#' # With n_segments for 1 line:
190+
#' l_seg_multi_n <- line_segment(l[1, ], n_segments = 3, use_rsgeo = FALSE)
191+
#' l_seg_multi_n <- line_segment(l$geometry[1], n_segments = 3, use_rsgeo = FALSE)
192+
#' l_seg_multi_n <- line_segment(l$geometry[1], n_segments = 3, use_rsgeo = TRUE)
193+
#' # With n_segments for all 3 lines:
194+
#' l_seg_multi_n <- line_segment(l, n_segments = 2)
195+
#' nrow(l_seg_multi_n) == nrow(l) * 2
183196
line_segment <- function(
184197
l,
185198
segment_length = NA,
199+
n_segments = NA,
186200
use_rsgeo = NULL,
187201
debug_mode = FALSE) {
202+
# Defensive programming:
203+
if (is.na(segment_length) && is.na(n_segments)) {
204+
rlang::abort(
205+
"segment_length or n_segments must be set.",
206+
call = rlang::caller_env()
207+
)
208+
}
188209
UseMethod("line_segment")
189210
}
190211
#' @export
191212
line_segment.sf <- function(
192213
l,
193214
segment_length = NA,
215+
n_segments = NA,
194216
use_rsgeo = NULL,
195-
debug_mode = FALSE) {
196-
if (is.na(segment_length)) {
197-
rlang::abort(
198-
"`segment_length` must be set.",
199-
call = rlang::caller_env()
200-
)
217+
debug_mode = FALSE
218+
) {
219+
# Get n_segments if not provided:
220+
if (is.na(n_segments)) {
221+
segment_lengths <- as.numeric(sf::st_length(l))
222+
n_segments <- n_segments(segment_lengths, segment_length)
223+
} else {
224+
if (length(n_segments) != nrow(l)) {
225+
if (length(n_segments) == 1) {
226+
message("Setting n_segments to ", n_segments, " for all lines")
227+
n_segments <- rep.int(n_segments, nrow(l))
228+
}
229+
}
201230
}
202231
# Decide whether to use rsgeo or lwgeom, if not set:
203232
if (is.null(use_rsgeo)) {
204233
use_rsgeo <- use_rsgeo(l)
205234
}
206235
if (use_rsgeo) {
207236
# If using rsgeo, we can do the whole thing in one go:
208-
segment_lengths <- as.numeric(sf::st_length(l))
209-
n_segments <- n_segments(segment_lengths, segment_length)
210237
res <- line_segment_rsgeo(l, n_segments = n_segments)
211238
return(res)
212239
}
240+
# lwgeom implementation:
213241
n_row_l <- nrow(l)
214242
if (n_row_l > 1) {
215243
res_list <- pbapply::pblapply(seq(n_row_l), function(i) {
216244
if (debug_mode) {
217245
message(paste0("Processing row ", i, " of ", n_row_l))
218246
}
219-
l_segmented <- line_segment1(l[i, ], n_segments = NA, segment_length = segment_length)
247+
l_segmented <- line_segment1(l[i, ], n_segments = n_segments[i], segment_length = NA)
220248
res_names <- names(sf::st_drop_geometry(l_segmented))
221249
# Work-around for https://github.com/ropensci/stplanr/issues/531
222250
if (i == 1) {
@@ -228,20 +256,20 @@ line_segment.sf <- function(
228256
res <- bind_sf(res_list)
229257
} else {
230258
# If there's only one row:
231-
res <- line_segment1(l, n_segments = NA, segment_length = segment_length)
259+
res <- line_segment1(l, n_segments = n_segments)
232260
}
233261
res
234262
}
235263

236-
237264
#' @export
238265
line_segment.sfc_LINESTRING <- function(
239266
l,
240267
segment_length = NA,
268+
n_segments = NA,
241269
use_rsgeo = NULL,
242270
debug_mode = FALSE) {
243271
l <- sf::st_as_sf(l)
244-
res <- line_segment(l, segment_length = segment_length, use_rsgeo, debug_mode)
272+
res <- line_segment(l, segment_length = segment_length, n_segments = n_segments, use_rsgeo, debug_mode)
245273
sf::st_geometry(res)
246274
}
247275

@@ -267,7 +295,8 @@ line_segment.sfc_LINESTRING <- function(
267295
line_segment1 <- function(
268296
l,
269297
n_segments = NA,
270-
segment_length = NA) {
298+
segment_length = NA
299+
) {
271300
UseMethod("line_segment1")
272301
}
273302
#' @export
@@ -383,7 +412,7 @@ line_segment_rsgeo <- function(l, n_segments) {
383412
res_sf <- sf::st_as_sf(
384413
res_tbl,
385414
geometry = res,
386-
crs = crs
415+
crs = crs
387416
)
388417
res_sf
389418
}

cran-comments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Bug fix release, with offending URLs removed thanks to feedback from CRAN.
1+
Fix to reverse dependencies
22

33
## R CMD check results
44

man/line_midpoint.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/line_segment.Rd

Lines changed: 27 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/line_segment1.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

revdep/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Platform
2+
3+
|field |value |
4+
|:--------|:------------------------------------------------------------------------------------|
5+
|version |R version 4.3.3 (2024-02-29) |
6+
|os |Ubuntu 22.04.4 LTS |
7+
|system |x86_64, linux-gnu |
8+
|ui |RStudio |
9+
|language |en_GB:en |
10+
|collate |en_GB.UTF-8 |
11+
|ctype |en_GB.UTF-8 |
12+
|tz |Europe/London |
13+
|date |2024-04-26 |
14+
|rstudio |2024.04.0-daily+662 Chocolate Cosmos (desktop) |
15+
|pandoc |3.1.11 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/x86_64/ (via rmarkdown) |
16+
17+
# Dependencies
18+
19+
|package |old |new |Δ |
20+
|:----------|:-----|:---------|:--|
21+
|stplanr |1.1.2 |1.2.0 |* |
22+
|curl |NA |5.2.1 |* |
23+
|data.table |NA |1.15.4 |* |
24+
|DBI |NA |1.2.2 |* |
25+
|geosphere |NA |1.5-18 |* |
26+
|lwgeom |NA |0.2-14 |* |
27+
|nabor |0.5.0 |0.5.0 | |
28+
|od |0.4.4 |0.4.4 | |
29+
|openssl |NA |2.1.2 |* |
30+
|pbapply |1.7-2 |1.7-2 | |
31+
|RcppEigen |NA |0.3.4.0.0 |* |
32+
|sf |NA |1.0-16 |* |
33+
|sp |NA |2.1-3 |* |
34+
|tidyselect |NA |1.2.1 |* |
35+
|units |NA |0.8-5 |* |
36+
37+
# Revdeps
38+
39+
## Failed to check (3)
40+
41+
|package |version |error |warning |note |
42+
|:--------------|:-------|:-----|:-------|:----|
43+
|agricolaeplotr |? | | | |
44+
|cyclestreets |? | | | |
45+
|pct |? | | | |
46+

revdep/cran.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## revdepcheck results
2+
3+
We checked 3 reverse dependencies (0 from CRAN + 3 from Bioconductor), comparing R CMD check results across CRAN and dev versions of this package.
4+
5+
* We saw 0 new problems
6+
* We failed to check 0 packages
7+

revdep/data.sqlite

24 KB
Binary file not shown.

revdep/failures.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# agricolaeplotr
2+
3+
<details>
4+
5+
* Version:
6+
* GitHub: https://github.com/ropensci/stplanr
7+
* Source code: NA
8+
* Number of recursive dependencies: 0
9+
10+
</details>
11+
12+
## Error before installation
13+
14+
### Devel
15+
16+
```
17+
18+
19+
20+
21+
22+
23+
```
24+
### CRAN
25+
26+
```
27+
28+
29+
30+
31+
32+
33+
```
34+
# cyclestreets
35+
36+
<details>
37+
38+
* Version:
39+
* GitHub: https://github.com/ropensci/stplanr
40+
* Source code: NA
41+
* Number of recursive dependencies: 0
42+
43+
</details>
44+
45+
## Error before installation
46+
47+
### Devel
48+
49+
```
50+
51+
52+
53+
54+
55+
56+
```
57+
### CRAN
58+
59+
```
60+
61+
62+
63+
64+
65+
66+
```
67+
# pct
68+
69+
<details>
70+
71+
* Version:
72+
* GitHub: https://github.com/ropensci/stplanr
73+
* Source code: NA
74+
* Number of recursive dependencies: 0
75+
76+
</details>
77+
78+
## Error before installation
79+
80+
### Devel
81+
82+
```
83+
84+
85+
86+
87+
88+
89+
```
90+
### CRAN
91+
92+
```
93+
94+
95+
96+
97+
98+
99+
```

0 commit comments

Comments
 (0)