Skip to content

Commit

Permalink
Merge pull request #556 from ropensci/552-argument-of-segment_length-…
Browse files Browse the repository at this point in the history
…in-line_segment-fun-causes-issue-2

Use output of rsgeo for n_segments (#552)
  • Loading branch information
Robinlovelace authored Apr 23, 2024
2 parents 42773e7 + 5d91248 commit 4034c7a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: stplanr
Title: Sustainable Transport Planning
Version: 1.1.2.9000
Version: 1.2.0
Authors@R: c(
person("Robin", "Lovelace", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-5679-6536")),
Expand Down
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# stplanr (development version)
# stplanr 1.2.0

- `line_segment()` now will use `rsgeo::line_segmentize()` if available and only when the input geometry is not in geographic coordinates or does not have a CRS set as it uses euclidean distance
- `line_segment()` becomes an S3 generic which now has methods for `sf` and `sfc` class objects
- `line_segment()` now will use `rsgeo::line_segmentize()` if available
- `line_segment()` becomes an S3 generic which now has methods for `sf` and `sfc` class objects
- `line_segment()` now works around [{rsgeo} issue](https://github.com/JosiahParry/rsgeo/issues/42) with `line_segmentize()` returning fewer segments than requested (#552)

# stplanr 1.1.2 (2023-09)

Expand Down
15 changes: 8 additions & 7 deletions R/linefuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ line_midpoint <- function(l, tolerance = NULL) {
#' This function keeps the attributes.
#' Note: results differ when `use_rsgeo` is `TRUE`:
#' the `{rsgeo}` implementation is faster and more reliably
#' keeps returned linestrings below a the `segment_length` value.
#' keeps returned linestrings below a the `segment_length` value,
#' but does not always return the number of segments requested.
#'
#' @inheritParams line2df
#' @param segment_length The approximate length of segments in the output (overides n_segments if set)
Expand Down Expand Up @@ -354,6 +355,7 @@ line_segment_rsgeo <- function(l, n_segments) {

# give them them CRS
res <- sf::st_set_crs(res, crs)
n_segments <- length(res)

# calculate the number of original geometries
n_lines <- length(geo)
Expand All @@ -363,13 +365,12 @@ line_segment_rsgeo <- function(l, n_segments) {
# index the original sf object
res_tbl <- sf::st_drop_geometry(l)[ids, , drop = FALSE]

# assign the geometry column
nrow(res_tbl)

res_tbl[[attr(l, "sf_column")]] <- res

# convert to sf and return
res_sf <- sf::st_as_sf(res_tbl)
res_sf <- sf::st_as_sf(
res_tbl,
geometry = res,
crs = crs
)
res_sf
}

Expand Down
11 changes: 6 additions & 5 deletions R/rnet_join.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Join route networks
#'
#' This is a spatial join function that is enables adding columns to a
#' 'target' route network from a 'source' route
#' Join function that adds columns to a
#' 'target' route network `sf` object from a 'source' route
#' network that contains the base geometry, e.g. from OSM
#'
#' The output is an sf object containing polygons representing
Expand Down Expand Up @@ -180,6 +180,10 @@ line_cast <- function(x) {
}

#' Merge route networks, keeping attributes with aggregating functions
#'
#' This is a small wrapper around `rnet_join()`.
#' In most cases we recommend using [`rnet_join()`] directly,
#' as it gives more control over the results
#'
#' @inheritParams rnet_join
#' @param sum_flows Should flows be summed? `TRUE` by default.
Expand Down Expand Up @@ -224,9 +228,6 @@ line_cast <- function(x) {
#' # rnet_y = sf::read_sf("rnet_y_ed.geojson")
#' # rnet_merged = rnet_merge(rnet_x, rnet_y, dist = 9, segment_length = 20, funs = funs)
#' @return An sf object with the same geometry as `rnet_x`
#'


rnet_merge <- function(rnet_x, rnet_y, dist = 5, funs = NULL, sum_flows = TRUE, crs = geo_select_aeq(rnet_x), ...) {

# handle_strings = function(strings) {
Expand Down
3 changes: 2 additions & 1 deletion man/line_segment.Rd

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

4 changes: 2 additions & 2 deletions man/rnet_join.Rd

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

4 changes: 3 additions & 1 deletion man/rnet_merge.Rd

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

35 changes: 35 additions & 0 deletions reprexes/test-rnet_join.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,38 @@ funs = list(
}

rnet_merge(rnet_x, rnet_y, funs = funs, dist = 20)

sfc_integer = sf::st_linestring(
cbind(
c(418938.4, 418949.7, 418961),
c(434303.2, 434280.1, 434257)
)
) |>
sf::st_sfc() |>
sf::st_set_crs(27700)

line_segment(sfc_integer, 10)

sfc_no_integer = sf::st_linestring(
cbind(
c(418938.4, 418949.7, 418961.1),
c(434303.2, 434280.1, 434257)
)
) |>
sf::st_sfc()


rsgeo::line_segmentize(rsgeo::as_rsgeo(sfc_integer), n = 6) |>
sf::st_as_sfc() |>
sf::st_cast("LINESTRING") |>
length()

rsgeo::line_segmentize(rsgeo::as_rsgeo(sfc_no_integer), n = 6) |>
sf::st_as_sfc() |>
sf::st_cast("LINESTRING") |>
length()

library(stplanr)
rnet_x = sf::read_sf("https://github.com/ropensci/stplanr/releases/download/v1.0.2/rnet_x_ed.geojson")
rnet_y = sf::read_sf("https://github.com/ropensci/stplanr/releases/download/v1.0.2/rnet_y_ed.geojson")
line_segment(rnet_y, 10)

0 comments on commit 4034c7a

Please sign in to comment.