Skip to content

rbind of SpPolDFs

4 messages · Agustin Lobo, Roger Bivand

#
Hi!

I have a set of shapefiles, each one with one single polygon, and I
want to end up with one single SpPolDF including all polygons.
I read the set of shapefiles with readOGR() and thus get one SpPolDF
for each polygon.
I (unsuccessfully)  try:
Error in validObject(.Object) :
  invalid class "SpatialPolygons" object: non-unique Polygons ID slot values
Calls: rbind.SpatialPolygons ... SpatialPolygons -> new -> initialize
-> initialize -> validObject
Error in validObject(.Object) :
  invalid class "SpatialPolygons" object: non-unique Polygons ID slot values
Calls: rbind.SpatialPolygonsDataFrame ... SpatialPolygons -> new ->
initialize -> initialize -> validObject
Error in as(x, "SpatialPolygons") :
  no method or default for coercing "list" to "SpatialPolygons"
Calls: rbind.SpatialPolygonsDataFrame -> do.call -> lapply -> FUN -> as

and some other variants.

Any help on how to do this? A solution would be using joinPolys from
maptools (the polygons do not overlap), but think that
rbind.SpatialPolygonsDataFrame() is better suited for this operation.

Agus
#
On Tue, 18 May 2010, Agustin Lobo wrote:

            
What about using spChFIDs() to assign unique FIDs? For example:

BesosBoundary <- spChFIDs(BesosBoundary, paste("Besos",
   row.names(BesosBoundary), sep="_"))

and so on. As it is, all will have "0", "1", ..., which are then not 
unique when rbind'ed. The user needs to control the FIDs, so needs to 
assign unique values before rbind.

Roger

  
    
#
Thanks. I have to do it this way as the tables are inconsistent:

BesosBoundary at data <- data.frame(Basin="Besos")
TerBoundary at data <- data.frame(Basin="Ter")
BesosBoundary <- spChFIDs(BesosBoundary, "Besos")
TerBoundary <- spChFIDs(TerBoundary, "Ter")
delme <- rbind.SpatialPolygonsDataFrame(BesosBoundary,TerBoundary)

Nevertheless, as I have several of these shape files, I'm trying a loop.
But while the following works:
[1] "Besos"
the following (which I need for using the names in an array) does not:
[1] "BesosBoundary"
Error in slot(get(bas[i]), "data") <- data.frame(Basin = basnom) :
  could not find function "get<-"

am I doing something wrong or have I hitted an inconsistency?

Agus
#
On Tue, 18 May 2010, Agustin Lobo wrote:

            
The error message seems explicit. Use get() first to a temporary local 
object, do slot()<- on that object, and assign() back to bas[i]. Untried.

Roger