How can I divide the multiple overlapping polygons into multiple non-overlapping polygons? Here is an example: I would like to divide the long yellow polygon in the image into 3 polygons based on where it touches the other two.
library(spData) library(sf) seine_buff_5km = st_buffer(seine, dist = 5000) tmap_mode("plot") tm_shape(seine_buff_5km) + tm_fill( col = "name", alpha =.5) + tm_shape(seine) + tm_lines()
Of course, I would like to do that for a larger data set with many more polygons. I guess one way to do it is subtract all polygons in an sf object from each other.
st_intersection on a single object will chop it into non-overlapping parts:
> bi = st_intersection(seine_buff_5km) > bi Simple feature collection with 5 features and 3 fields geometry type: GEOMETRY dimension: XY bbox: xmin: 513345.8 ymin: 6655432 xmax: 884953.8 ymax: 6943863 epsg (SRID): 2154 proj4string: +proj=lcc +lat_1=49 +lat_2=44 +lat_0=46.5 +lon_0=3 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs name geometry n.overlaps origins 1 Marne POLYGON ((658951.7 6862882,... 1 1 2 Marne POLYGON ((657550.3 6852588,... 2 1, 2 3 Seine POLYGON ((517155.1 6930721,... 1 2 4 Seine POLYGON ((695195.4 6815345,... 2 2, 3 5 Yonne MULTIPOLYGON (((711168.3 68... 1 3
origins column is a "list-column" that shows which of the original parts form the new part. When plotted in unique colours the five parts look like this:
Note how the green part is treated as a single object because it isn't quite cut into parts by the other polygons.
The fifth row (blue polygon, Yonne), is shown as a MULTIPOLYGON because there is a tiny sliver cut off above the orange polygon.