0

I am having trouble merging shapefiles in R. Here is my code thus far:

library(rgdal)
library(maptools)
library(gridExtra)

setwd("/Users/Cornelius/Dropbox/Cornelius_Sharedfolder")

#Load a geodatabase
fgdb = "/Users/Cornelius/Dropbox/Cornelius_Sharedfolder/RwandaGDB.gdb"

subset(ogrDrivers(), grepl("GDB", name))
fc_list = ogrListLayers(fgdb)
print(fc_list)

#Get the shapefiles from the geodatabase:
Residence=readOGR(dsn=fgdb, layer="Résidence")
Territoire = readOGR(dsn=fgdb,layer="Territoire")
Chefferie=readOGR(ds=fgdb, layer="Chefferie")
Sous_Chefferie=readOGR(ds=fgdb, layer="Sous_Chefferie")
RwandaPre2002=readOGR(ds=fgdb, layer="RwandaPre2002")
Country_Boundary2012=readOGR(ds=fgdb, layer="Country_Boundary2012")
Province_Boundary2012=readOGR(ds=fgdb, layer="Province_Boundary2012")
Sector_Boundary2012=readOGR(ds=fgdb, layer="Sector_Boundary2012")
District_Boundary2012=readOGR(ds=fgdb, layer="District_Boundary2012")
Cell_Boundary2012=readOGR(ds=fgdb, layer="Cell_Boundary2012")

x = list(Residence, Territoire, Sous_Chefferie, RwandaPre2002, Country_Boundary2012, Province_Boundary2012, Sector_Boundary2012, District_Boundary2012, Cell_Boundary2012)}

This all goes fine. However, when I try to merge two shapefiles - for example, Residence and Territoire, and use the following code, it gives me an error:

test_bind <-spRbind(Territoire, Residence)

The error says: "non-unique polygon IDs."

I'm not sure what this means. Could you please help?

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
Cornelius
  • 69
  • 1
  • 4
  • 1
    spRbind is equivalent to rbind(). Are you trying to merge, or append? If you want to merge variables in Residence to Territoire, you need to specify the variables that identify the unique matches between the two spdf's, similar to how you do in a normal merge(). If you want to append, you need to provide new polygon IDs. No polygon ID's can be identical. Check if you have duplicated IDs, and if so you need to modify them so they do not violate this requirement. See: http://www.inside-r.org/packages/cran/sp/docs/as.data.frame.SpatialPolygonsDataFrame – spesseh Jun 01 '16 at 21:19
  • When I use the merge command, it works, but when I save the merged file, it does not save as a Shapefile... – Cornelius Jun 01 '16 at 23:07

0 Answers0