Skip to content

Instantly share code, notes, and snippets.

@agricolamz
Last active June 13, 2018 05:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agricolamz/cc25f036d3d4df6e6983436a9c8f3d2f to your computer and use it in GitHub Desktop.
Save agricolamz/cc25f036d3d4df6e6983436a9c8f3d2f to your computer and use it in GitHub Desktop.
merge_by_intercept <- function(buff){
gt <- rgeos::gIntersects(buff, byid = TRUE, returnDense = FALSE)
ut <- unique(gt)
while(sum(duplicated(unlist(unique(ut)))) > 0){
ut <- unique(sapply(ut, function(x)
unique(unlist(ut[sapply(ut, function(y)
any(x %in% y))]))))
if(is.matrix(ut)){
ut <- list(`1` = ut[,1])
}
}
nth <- 1:length(ut)
buff$n <- 1:nrow(buff)
buff$nth <- NA
sapply(seq_along(nth), function(i){
buff$nth[ut[[i]]] <<- i
})
buff <- rgeos::gUnaryUnion(buff, buff$nth)
return(buff)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment