Skip to content

Instantly share code, notes, and snippets.

@statnmap
Created May 20, 2020 15:40
Show Gist options
  • Save statnmap/9d7b4e1082c2f86692aacbd8c02ab85c to your computer and use it in GitHub Desktop.
Save statnmap/9d7b4e1082c2f86692aacbd8c02ab85c to your computer and use it in GitHub Desktop.
Find overlapping polygons after buffer
library(sf)
library(ggplot2)
library(dplyr)
# Get data
extraWD <- "."
if (!file.exists(file.path(extraWD, "departement.zip"))) {
githubURL <- "https://github.com/statnmap/blog_tips/raw/master/2018-07-14-introduction-to-mapping-with-sf-and-co/data/departement.zip"
download.file(githubURL, file.path(extraWD, "departement.zip"))
unzip(file.path(extraWD, "departement.zip"), exdir = extraWD)
}
departements_L93 <- st_read(dsn = extraWD, layer = "DEPARTEMENT",
quiet = TRUE) %>%
st_transform(2154)
# Reduce dataset
dep_bretagne <- departements_L93 %>%
filter(NOM_REG == "BRETAGNE")
# Create buffer
dept_buf <- dep_bretagne %>%
st_buffer(
dist =
units::set_units(30, km)
)
ggplot(dept_buf) +
geom_sf(fill = NA)
dept_buf_overlap <- dept_buf %>%
st_intersection() %>%
st_collection_extract("POLYGON")
ggplot(dept_buf_overlap) +
geom_sf(aes(fill = n.overlaps))
@statnmap
Copy link
Author

statnmap commented May 20, 2020

I see the problem
image

Find out which one overlap:
image

@statnmap
Copy link
Author

I tried Voronoi over the buffer, but that may be a little more complicated....

dept_voronoi <- dept_buf_overlap %>% 
  st_voronoi()

ggplot() + 
  geom_sf(data = dep_bretagne[1,]) +
  geom_sf(data = dept_voronoi[1,], fill = NA)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment