[R-sig-Geo] merging several shapefiles into one

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed May 25 11:13:09 CEST 2011


On Wed, May 25, 2011 at 8:06 AM, Nicolas Degallier
<nicolas.degallier at ird.fr> wrote:
> Hi All,
>
> I have read different shapefiles that are contiguous and that have different column names with the function "readShapePoly".
>
> example :
>
> noumea <- readShapePoly(noumea.file, IDvar = "NOUMEA2_ID", proj4string = crs_rgnc)
>
> and
>
> dumbea <- readShapePoly(dumbea.file, IDvar = "DUMBEA2_", proj4string = crs_rgnc)
>
> Now I would like to create a new shapefile with all the polygones in the "noumea" and "dumbea" spatial polygone data frames.
>
> I tried :
> spRbind(noumea, dumbea)
>
> I get this message error :
>
> Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
>  non-unique polygon IDs
>
> I have tried with the function rbind()
> I have tried to set noumea$NOUMEA2_ID and dumbea$DUMBEA2_ with unique values but I still get the same message error as if the function spRbind was not using those two columns as polygons ID...
>
> Does anyone know how I can fix this problem ?

 once you've read in your data using NOUMEA2_ID and DUMBEA2_ as the ID
variables, changing the values in the data frame won''t change the ID
attached to each feature.

 You need to use the "spChFIDs-methods" to create new spatial polygons
data frames with unique IDs between them.

 Here, scot1 is the first 10 features from scot_BNG as read in help(rgdal)

 > scot2=scot1
 > scot12=spRbind(scot1,scot2)
 Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) :
  non-unique polygon IDs

 - that fails because the IDs are the same. So we change scot2 to be
"A" to "J" (which isnt what the IDs are currently...)

 > scot2=spChFIDs(scot2,letters[1:10])
 > scot12=spRbind(scot1,scot2)

and that works.

Barry



More information about the R-sig-Geo mailing list