Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@statnmap statnmap commented May 20, 2020

I see the problem
image

Find out which one overlap:
image

@statnmap

This comment has been minimized.

Copy link
Owner Author

@statnmap statnmap commented May 20, 2020

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