Open
Description
A common GIS task is to dissolve boundaries based on shared boundaries of polygons. Doing this with R always bends my head a little bit.
I think having a utility function in {sf} to do this would be very handy.
Here is a minimal example of how that function might work. Using spdep
is probably the best for this task because it already has functions for identifying contiguous polygons as well as the connected subgraphs.
This is inspired by @CGMossa's PhD work
dissolve_boundaries <- function(x) {
if (!requireNamespace("spdep")) {
stop("spdep is needed to use this function")
}
crs <- sf::st_crs(x)
nbs <- spdep::poly2nb(x)
nb_ids <- attr(nbs, "ncomp")[["comp.id"]]
res <- lapply(
split(x, nb_ids),
sf::st_union,
is_coverage = TRUE
)
sf::st_sfc(
unlist(res, recursive = FALSE),
crs = crs
) |> sf::st_combine()
}
Metadata
Assignees
Labels
No labels