<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/merging-spatial-buffers-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://aghaynes.wordpress.com/2018/06/11/merging-spatial-buffers-in-r/"> R – Insights of a PhD</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<pre>library(osmdata)
library(rgdal) ; library(maptools) ; library(rgeos)

q0 &lt;- opq(bbox = &quot;Bern, Switzerland&quot;, timeout = 60)
q1 &lt;- add_osm_feature(q0, key = 'building', value = &quot;hospital&quot;)
x &lt;- osmdata_sp(q1)

library(leaflet)

spChFIDs(x$osm_polygons) &lt;- 1:nrow(x$osm_polygons@data)
cent &lt;- gCentroid(x$osm_polygons, byid = TRUE)
leaflet(cent) %>% addTiles() %>% addCircles()</pre>
<p>Here we plot the building centroids.</p>
<p><img data-orig-file="https://aghaynes.files.wordpress.com/2018/06/hospcent.png?w=456" data-orig-size="451,326" data-medium-file="https://aghaynes.files.wordpress.com/2018/06/hospcent.png?w=456?w=300" data-attachment-id="365" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2018/06/hospcent.png?w=456" data-permalink="https://aghaynes.wordpress.com/2018/06/11/merging-spatial-buffers-in-r/hospcent/" data-large-file="https://aghaynes.files.wordpress.com/2018/06/hospcent.png?w=456?w=451" alt="hospcent" data-image-title="hospcent" sizes="(max-width: 451px) 100vw, 451px" data-comments-opened="1" class="wp-image-365" data-recalc-dims="1"></p>
<p>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.</p>
<pre>buff &lt;- gBuffer(cent, byid = TRUE, width = 0.0015)
leaflet(cent) %>% addTiles() %>% addPolygons(data = buff, col = &quot;red&quot;) %>% addCircles()</pre>
<p><img data-orig-file="https://aghaynes.files.wordpress.com/2018/06/hospbuff.png?w=456" data-orig-size="407,335" data-medium-file="https://aghaynes.files.wordpress.com/2018/06/hospbuff.png?w=456?w=300" data-attachment-id="366" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2018/06/hospbuff.png?w=456" data-permalink="https://aghaynes.wordpress.com/2018/06/11/merging-spatial-buffers-in-r/hospbuff/" data-large-file="https://aghaynes.files.wordpress.com/2018/06/hospbuff.png?w=456?w=407" alt="hospbuff" data-image-title="hospbuff" sizes="(max-width: 407px) 100vw, 407px" data-comments-opened="1" class="wp-image-366" data-recalc-dims="1"></p>
<p>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.</p>
<pre>buff &lt;- SpatialPolygonsDataFrame(buff, data.frame(row.names = names(buff), n = 1:length(buff)))
gt &lt;- gIntersects(buff, byid = TRUE, returnDense = FALSE)
ut &lt;- unique(gt)
nth &lt;- 1:length(ut)
buff$n &lt;- 1:nrow(buff)
buff$nth &lt;- NA
for(i in 1:length(ut)){
  x &lt;- ut[[i]]
  buff$nth[x] &lt;- i
}
buffdis &lt;- gUnaryUnion(buff, buff$nth)
leaflet(cent) %>% addTiles() %>% addPolygons(data = buffdis, col = &quot;red&quot;) %>% addCircles()</pre>
<p><img data-orig-file="https://aghaynes.files.wordpress.com/2018/06/hospbuff2.png?w=456" data-orig-size="438,326" data-medium-file="https://aghaynes.files.wordpress.com/2018/06/hospbuff2.png?w=456?w=300" data-attachment-id="367" data-image-meta="{" data-image-description="" src="https://aghaynes.files.wordpress.com/2018/06/hospbuff2.png?w=456" data-permalink="https://aghaynes.wordpress.com/2018/06/11/merging-spatial-buffers-in-r/hospbuff2/" data-large-file="https://aghaynes.files.wordpress.com/2018/06/hospbuff2.png?w=456?w=438" alt="hospbuff2.png" data-image-title="hospbuff2" sizes="(max-width: 438px) 100vw, 438px" data-comments-opened="1" class="wp-image-367" data-recalc-dims="1"></p>
<p>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.</p>
<pre>gt &lt;- gIntersects(buffdis, byid = TRUE, returnDense = FALSE)
ut &lt;- unique(gt)
nth &lt;- 1:length(ut)
buffdis &lt;- SpatialPolygonsDataFrame(buffdis, data.frame(row.names = names(buffdis), n = 1:length(buffdis)))
buffdis$nth &lt;- NA
for(i in 1:length(ut)){
  x &lt;- ut[[i]]
  buffdis$nth[x] &lt;- i
}
buffdis &lt;- gUnaryUnion(buffdis, buffdis$nth)
leaflet(cent) %>% addTiles() %>% addPolygons(data = buffdis, col = &quot;red&quot;) %>% addCircles()

buffcent &lt;- gCentroid(buffdis, byid = TRUE</pre>
<p><a rel="nofollow" href="https://gist.githubusercontent.com/aghaynes/a057bd88ae5183eb1703c9455c19bfbc/raw/4a1e4df16be5f81902a59fec1acde1ac7b917d50/bufferunion.r" target="_blank">Code here</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://aghaynes.wordpress.com/2018/06/11/merging-spatial-buffers-in-r/"> R – Insights of a PhD</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/6k-K0P1v8Xs" height="1"></a></div>