Merging spatial buffers in R

By aghaynes


(This article was first published on R – Insights of a PhD, and kindly contributed to R-bloggers)

I’m sure there’s a better way out there, but I struggled to find a way to dissolve polygons that touched/overlapped each other (the special case being buffers). For example, using the osmdata package, we can download the polygons representing hospital buildings in Bern, Switzerland.

library(rgdal) ; library(maptools) ; library(rgeos)

q0 % addTiles() %>% addCircles()

Here we plot the building centroids.

Each point represents a hospital building. We don’t particularly care about the buildings themselves though. We want to create hospitals. To do so, we try a 150m buffer around each centroid.

buff % addTiles() %>% addPolygons(data = buff, col = "red") %>% addCircles()


We then want to merge the buffers into, in this case, four groups. This is the point that doesn’t seem to be implemented anywhere that I could see (I also tried QGIS but that just created one big feature, rather than many small ones). My approach is to get the unique sets of intersections, add them as a variable to the buffer and unify the polygons.

buff % addTiles() %>% addPolygons(data = buffdis, col = "red") %>% addCircles()


As you see, it almost worked. The lower left group is composed of three polygons. Doing the same process again clears it (only code shown). Large jobs might need more iterations (or larger buffers). The final job is to get the hospital centroids.

gt % addTiles() %>% addPolygons(data = buffdis, col = "red") %>% addCircles()


Code here.

To leave a comment for the author, please follow the link and comment on their blog: R – Insights of a PhD. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Source:: R News

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.