Skip to content

Commit

Permalink
add directed argument to st_line_merge()
Browse files Browse the repository at this point in the history
closes #2264
  • Loading branch information
edzer committed Nov 15, 2023
1 parent 6e39d6d commit ec14995
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# version 1.0-15

* add `directed` argument to `st_line_merge()`; #2264

* `st_union.sfc()` with `x` and `y` given works consistent across geodetic and projected objects; #2262

* `st_union.sf()` with `x` and `y` given unions pairwise if `by_feature=TRUE`; #2259
Expand Down
18 changes: 11 additions & 7 deletions R/geom-transformers.R
Original file line number Diff line number Diff line change
Expand Up @@ -514,26 +514,30 @@ st_polygonize.sf = function(x) {

#' @name geos_unary
#' @export
#' @param directed logical; if \code{TRUE}, lines with opposite directions will not be merged
#' @details \code{st_line_merge} merges lines. In case of \code{st_line_merge}, \code{x} must be an object of class \code{MULTILINESTRING}, or an \code{sfc} geometry list-column object containing these
#' @examples
#' mls = st_multilinestring(list(rbind(c(0,0), c(1,1)), rbind(c(2,0), c(1,1))))
#' st_line_merge(st_sfc(mls))
st_line_merge = function(x)
st_line_merge = function(x, ..., directed = FALSE)
UseMethod("st_line_merge")

#' @export
st_line_merge.sfg = function(x)
get_first_sfg(st_line_merge(st_sfc(x)))
st_line_merge.sfg = function(x, ..., directed = FALSE)
get_first_sfg(st_line_merge(st_sfc(x), directed = directed, ...))

#' @export
st_line_merge.sfc = function(x) {
st_line_merge.sfc = function(x, ..., directed = FALSE) {
stopifnot(inherits(x, "sfc_MULTILINESTRING"))
st_sfc(CPL_geos_op("linemerge", x, numeric(0), integer(0), numeric(0), logical(0)))
if (directed)
st_sfc(CPL_geos_op("linemergedirected", x, numeric(0), integer(0), numeric(0), logical(0)))
else
st_sfc(CPL_geos_op("linemerge", x, numeric(0), integer(0), numeric(0), logical(0)))
}

#' @export
st_line_merge.sf = function(x) {
st_set_geometry(x, st_line_merge(st_geometry(x)))
st_line_merge.sf = function(x, ..., directed = FALSE) {
st_set_geometry(x, st_line_merge(st_geometry(x), directed = directed, ...))
}

#' @name geos_unary
Expand Down
4 changes: 3 additions & 1 deletion man/geos_unary.Rd

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

7 changes: 7 additions & 0 deletions src/geos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,13 @@ Rcpp::List CPL_geos_op(std::string op, Rcpp::List sfc,
} else if (op == "linemerge") {
for (size_t i = 0; i < g.size(); i++)
out[i] = geos_ptr(chkNULL(GEOSLineMerge_r(hGEOSCtxt, g[i].get())), hGEOSCtxt);
} else if (op == "linemergedirected") {
#ifdef HAVE311
for (size_t i = 0; i < g.size(); i++)
out[i] = geos_ptr(chkNULL(GEOSLineMergeDirected_r(hGEOSCtxt, g[i].get())), hGEOSCtxt);
#else
Rcpp::stop("directed line merge requires GEOS >= 3.11");
#endif
} else if (op == "polygonize") {
for (size_t i = 0; i < g.size(); i++) {
const GEOSGeometry* gi = g[i].get();
Expand Down

0 comments on commit ec14995

Please sign in to comment.