Merging multiple SpatialPolygonDataFrames into 1 SPDF in R?
If you do not need to merge topology, but just add new polygons, you can simply use:
ab <- rbind(a,b)
If you get a "non-unique Polygons ID slot values" error it means that the rownames of the objects are the same. To fix this you can use spChFIDs to change the rownames and associated slot relationships. Since the slots in the object use the rownames to associate the object, you cannot just change row.names in the @data slot.
b <- spChFIDs(b, paste("b", row.names(b), sep="."))
The union (union_sp) function in the raster package is doing this, and calling gIntersects from rgeos, behind the scenes and is a very convenient helper function.
****Edit 08-06-2018 There is an undocumented argument that can be used to skip the duplicate ID issue.
ab <- rbind(a, b, makeUniqueIDs = TRUE)
Super easy solution provided by @mdsumner:
library(sp)
library(raster)
library(rgeos)
library(spatstat)
library(rgdal)
library(maptools)
setwd("C:/...")
a<-readOGR(dsn=getwd(), layer="pol.a")
b<- readOGR(dsn=getwd(), layer="pol.b")
# use union in {raster} package ?raster::union
ab<-union(a, b)
resulted in :
class(ab)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"