13

How do I merge polygon features in a shapefile with many polygons?

rbind and union just combine the rows of the shapefile features, they don't actually merge the polygons themselves.

Desired result in example below:

How do I get the below shapefile with duplicated ID_2 to merge to a single polygon in sptemp?

Example below of GADM level 2 of Ethiopia has first two rows of the shapefile ID_2 column duplicated (value=1). I'd like sptemp with 79 features combining the first two rows that are the ones with duplicated ID_2. A plot of sptemp[1,] would bring up where current sptemp[1,] and sptemp2[2,] without the boundaries between the duplicated, i.e. the polygons are merged too.

Example Code:

Download, unzip, and load into R GADM file for Ethiopia level 2 (899kb to working directory):

library(curl)
library(rgdal)

curl_download("http://biogeo.ucdavis.edu/data/gadm2.8/shp/ETH_adm_shp.zip",
              destfile=paste0("gadmETH.zip"),
              quiet=FALSE)

unzip(paste0("gadmETH.zip"), exdir="gadmETH", overwrite=FALSE) 


###Load shapefile
sptemp <- readOGR(dsn="gadmETH", layer="ETH_adm2")

The ID_2 column for the first two polygons is duplicated

###You'll see in the first two rows ID_2 is duplicated
df.sptemp <- as.data.frame(sptemp)

View(sptemp)

###I can't just delete one because they are separate polygons 
plot(sptemp[1,], col="blue")
plot(sptemp[2,], add=TRUE, col="red" )
Neal Barsch
  • 2,810
  • 2
  • 13
  • 39

1 Answers1

25

Note This method uses st_union, which combines all the 'multipolygons' into single polygons. This may not be your actual desired result.


If you use library(sf) as opposed to sp (it's the successor to sp), you can use st_union to join geometries.

You can do this inside a dplyr pipe sequence too.

library(sf)
sptemp <- sf::st_read(dsn = "~/Desktop/gadmETH/", layer = "ETH_adm2")

library(dplyr)

sptemp %>% 
    group_by(ID_2) %>%
    summarise(geometry = sf::st_union(geometry)) %>%
    ungroup()

# Simple feature collection with 79 features and 1 field
# geometry type:  GEOMETRY
# dimension:      XY
# bbox:           xmin: 33.00154 ymin: 3.398823 xmax: 47.95823 ymax: 14.84548
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
# # A tibble: 79 x 2
#      ID_2                       geometry
#     <dbl>         <sf_geometry [degree]>
#   1    1. POLYGON ((38.85556 8.938293...
#   2    2. POLYGON ((42.15579 12.72123...
#   3    3. POLYGON ((40.17299 14.49028...
#   4    4. POLYGON ((41.11739 10.93207...
#   5    5. POLYGON ((40.61546 12.7958,...
#   6    6. POLYGON ((40.25209 11.24655...
#   7    7. POLYGON ((36.35452 12.04507...
#   8    8. POLYGON ((40.11263 10.87277...
#   9    9. POLYGON ((37.39736 11.60206...
#   10   10. POLYGON ((38.48427 12.32812...
#  # ... with 69 more rows
SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • This works great! And thanks for the tip on sf! Is there a downside to loading a shapefile in sf? I'm getting crazy loading performance differences (Ethiopia 2 in rgdal: user system elapsed 0.999 0.211 1.271, same layer in sf: user system elapsed 0.013 0.003 0.017). I tested a bunch of other processes like extract and velox extract and all seem to work fine with sf shapefiles instead of rgdal. Is there a downside to loading shapefiles in sf over rgdal or is the performance boost a pure factor of the update (maybe a loading process move to C or something?)? – Neal Barsch Mar 19 '18 at 02:18
  • @NealBarsch see the [Benchmarking section in the first sf vignette](https://r-spatial.github.io/sf/articles/sf1.html#reading-and-writing) for a brief discussion on performance. Any other information is usually linked in [the sf github page](https://github.com/r-spatial/sf) – SymbolixAU Mar 19 '18 at 02:25
  • 2
    @NealBarsch - do be aware of the impact of using `st_union`. It's merging all the multipolygons into single polygons. You're essentially changing the shape – SymbolixAU Mar 19 '18 at 02:26
  • Intention is to use the updated list for extent boundaries in raster extraction so shouldn't be an issue as long as I don't delete any area. – Neal Barsch Mar 19 '18 at 02:29
  • @NealBarsch - OK cool. I'll keep the 'note' at the top so others who see this are aware. – SymbolixAU Mar 19 '18 at 02:38