Skip to content

Commit

Permalink
feat: add mf_get_borders() to extract borders from contiguous polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
rCarto committed Oct 17, 2024
1 parent 5dfcb89 commit 9ca6272
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export(mf_choro)
export(mf_credits)
export(mf_distr)
export(mf_export)
export(mf_get_borders)
export(mf_get_breaks)
export(mf_get_leg_pos)
export(mf_get_links)
Expand Down
31 changes: 31 additions & 0 deletions R/mf_get_borders.R
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)
}
2 changes: 1 addition & 1 deletion R/mf_get_pencil.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' @title Pencil Layer
#' @title Get a pencil layer from polygons
#' @name mf_get_pencil
#' @description Create a pencil layer. This function transforms a POLYGON or
#' MULTIPOLYGON sf object into a MULTILINESTRING one.
Expand Down
2 changes: 2 additions & 0 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#' - [mf_get_breaks()] Get class intervals
#' - [mf_get_mtq()] Get the 'mtq' dataset
#' - [mf_get_ratio()] Get map width and height values
#' - [mf_get_pencil()] Get a pencil layer from polygons
#' - [mf_get_borders()] Get a border layer from polygons
#
#' @md
#'
Expand Down
7 changes: 7 additions & 0 deletions inst/tinytest/test_get_borders.R
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")
2 changes: 2 additions & 0 deletions man/mapsf.Rd

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

29 changes: 29 additions & 0 deletions man/mf_get_borders.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/mf_get_pencil.Rd

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

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ reference:
- mf_get_mtq
- mf_get_ratio
- mf_get_pencil
- mf_get_borders

figures:
dev: grDevices::png
Expand Down

0 comments on commit 9ca6272

Please sign in to comment.