I extracted information from open street map that contains many polygons, some of them overlapping. I want to union/dissolve all polygons that have overlap with other polygons. I am looking for an approach that uses the sf package. I posted this question on StackExchange here: https://gis.stackexchange.com/questions/323038/dissolve-only-overlapping-polygons-in-r-using-sf/323067#323067 with a reproducible simple example. With help of a reader (Spacedman), I managed to solve it for the simple case. Unfortunately, it seems the osm data is somewhat messy and the solution does not seem to work (or I do not understand it completely). For this reason I am posting it here in the hope somebody has an idea.
The reproducible examples is as follows - three clusters of rectangles of which two have overlap:
parts <- st_cast(st_union(x),"POLYGON")
clust <- unlist(st_intersects(x, parts))
diss <- cbind(x, clust) %>%
summarize(box = paste(box, collapse = ", ")) # I would like to preserve the names of the polygons before the dissolve hence the paste command.
However, when I run this on my dataset from Open Street Map, which can be found here: https://www.dropbox.com/s/4n259yhh5swqgf2/osm_data.rds?dl=0, it does not work. When I use st_intersects(x, parts) to determine the links between the features and the clusters it appears that features can be related to two (or more) clusters. I do not understand how this is possible as then these two clusters should be regarded as one cluster. Right? How can I adjust the code above so all the overlapping polygons in my dataset will be dissolved?
M. (Michiel) van Dijk, PhD
Research scholar | Ecosystems Services and Management (ESM) | International Institute for Applied Systems Analysis (IIASA) Senior researcher (out of office) | International Policy Division (IB) | Wageningen Economic Research