Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mush() #8

Open
mdsumner opened this issue Jun 5, 2022 · 0 comments
Open

mush() #8

mdsumner opened this issue Jun 5, 2022 · 0 comments

Comments

@mdsumner
Copy link
Member

mdsumner commented Jun 5, 2022

#' fuse multiple layers
#'
#' Combine polygon layers into one partition of the plane. 
#' 
#' Each layer (sf polygon object) is entered as an argument, may be 2 or more layers. 
#' 
#'  
#' @param ... entered arguments of sf polygons layers
#'
#' @return sf polygon layer with attributes, a, b, c, ... as per length of '..."
#' @export
#'
#' @examples
#' mush(A, B, C)
mush <- function(...) {
  listies <- list(...)
  if (length(listies) > length(letters)) stop("our alphabet is not long enough")
  ## assume, sf layers of  polygons - can't be lines for st_boundary need to escape hatch
  resultants <- sf::st_cast(sf::st_polygonize(sf::st_union(do.call(c, lapply(listies, function(.x) sf::st_boundary(sf::st_geometry(.x)))))))
  r.pip <- sf::st_point_on_surface(resultants)
  n.id <- lapply(listies, function(.x) sf::st_contains(.x, r.pip))
  out <- vector("list", length(n.id))
  names(out) <- letters[seq_len(length(listies))]
  for (i in seq_along(out))  {
    nas <- rep(NA_character_, length(resultants))
    nas[unlist(n.id[[i]])] <- letters[i]
    out[[i ]] <- nas
  }
  out <- tibble::as_tibble(out)
  bad <- rowSums(do.call(cbind, lapply(out, as.factor)), na.rm = TRUE) < 1
  sf::st_set_geometry(out[!bad, ] , resultants[!bad])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant