-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mf_get_borders() to extract borders from contiguous polygons
- Loading branch information
Showing
9 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#' @title Get a border layer from polygons | ||
#' @description This function extracts borders between contiguous polygons. | ||
#' @name mf_get_borders | ||
#' @param x an sf object of POLYGONS, using a projected CRS | ||
#' @note | ||
#' If the polygon layer contains topology errors (such as contiguous | ||
#' polygons not sharing exactly the same boundary) the function may not return | ||
#' all boundaries correctly. It is possible to use `st_snap()` or other | ||
#' functions to try and correct these errors. | ||
#' @return An sf object (MULTILINESTRING) of borders is returned. | ||
#' @md | ||
#' @examples | ||
#' mtq <- mf_get_mtq() | ||
#' mtq_b <- mf_get_borders(mtq) | ||
#' mf_map(mtq) | ||
#' mf_map(mtq_b, col = 1:5, lwd = 4, add = TRUE) | ||
#' @export | ||
mf_get_borders <- function(x) { | ||
if (get_geom_type(x) != "POLYGON") { | ||
stop(paste0('"x" should be a POLYGON sf object'), call. = FALSE) | ||
} | ||
if (sf::st_is_longlat(x)) { | ||
stop("This feature does not work on unprojected (long/lat) layers.", | ||
call. = FALSE) | ||
} | ||
oag <- sf::st_agr(x) | ||
x <- sf::st_set_agr(x, "constant") | ||
border <- sf::st_collection_extract(sf::st_intersection(x, x), "LINESTRING") | ||
border <- sf::st_set_agr(border, c(oag, oag)) | ||
return(border) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mtq <- mf_get_mtq() | ||
expect_silent(mf_get_borders(mtq)) | ||
expect_error(mf_get_borders(st_transform(mtq, 4326))) | ||
expect_error(mf_get_borders(st_cast(mtq, "MULTIPOINT"))) | ||
x <- mf_get_borders(mtq) | ||
expect_equal(nrow(x), 142) | ||
expect_inherits(sf::st_geometry(x), "sfc_MULTILINESTRING") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters