Last active
June 13, 2018 05:32
-
-
Save agricolamz/cc25f036d3d4df6e6983436a9c8f3d2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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