I am trying to shrink an sf polygon by a fixed distance.
Take this simple polygon:
coords <- list(rbind(c(0,0), c(2,0), c(2,2), c(1,2), c(1,1), c(0,1), c(0,0)))
p <- sf::st_polygon(x = coords)
ggplot() + geom_sf(data = p, fill = "white", color = "black")
I want to make a polygon of the same shape, but with edges all exactly 0.1 units away from the original polygon. This is the target:
target.coords <- list(rbind(c(0.1,0.1), c(1.9,0.1), c(1.9,1.9), c(1.1,1.9), c(1.1,0.9), c(0.1,0.9), c(0.1,0.1)))
target <- sf::st_polygon(x = target.coords)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = target, fill = "green", color = "dark green")
I thought that sf::st_buffer()
would get me there, but the outside corner doesn't quite match.
p1 <- sf::st_buffer(x = p, dist = -0.1)
ggplot() +
geom_sf(data = p, fill = "white", color = "black") +
geom_sf(data = p1, fill = "red", color = "dark red")
Is there an alternative setting to sf::st_buffer()
that would allow me to achieve the target polygon? Or is there an alternative function that would achieve this? The offset needs to be by a fixed distance, not by shrinking the polygon a relative amount (e.g. 90%).