Skip to content

Instantly share code, notes, and snippets.

@joeroe
Created September 4, 2018 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeroe/43bb6efa233cb5c8994b6a6d41a4911a to your computer and use it in GitHub Desktop.
Save joeroe/43bb6efa233cb5c8994b6a6d41a4911a to your computer and use it in GitHub Desktop.
Convert a deldir::deldir object to an sf::sf
#' @importFrom sf st_as_sf
st_as_sf.deldir <- function(dd, extract = c("tiles", "triangles")) {
extract <- match.arg(extract)
if (extract == "tiles") {
ddlist <- deldir::tile.list(dd)
}
else if (extract == "triangles") {
ddlist <- deldir::triang.list(dd)
}
ddlist %>%
purrr::map(~{cbind(x = .$x, y = .$y)} %>%
rbind(.[1,]) %>%
list() %>%
sf::st_polygon()) %>%
sf::st_sfc() %>%
sf::st_sf() %>%
dplyr::mutate(id = dplyr::row_number()) %>%
return()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment