[R-sig-Geo] Dissolve only overlapping polygons in R using sf
Dijk, Michiel van
m|ch|e|@v@nd|jk @end|ng |rom wur@n|
Sun May 19 22:18:41 CEST 2019
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:
sq = function(pt, sz = 1) st_polygon(list(rbind(c(pt - sz), c(pt + sz, pt - sz), c(pt + sz), c(pt - sz, pt + sz), c(pt - sz)))) x = st_sf(box = 1:6, st_sfc(sq(c(4.2,4.2)), sq(c(0,0)), sq(c(1, -0.8)), sq(c(0.5, 1.7)), sq(c(3,3)), sq(c(-3, -3))))
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
ySchlossplatz 1 - A-2361 Laxenburg, Austria, room s-138 S +43 2236 807 537
More information about the R-sig-Geo