4

I´m looking for a way to create a SpatialPolygonsDataFrame from a list of SpatialPolygons?

In the following there´s an example of a list of Polygons from which a SpatialPolygonsDataFrame, containing all Polygons of the list, should be created.

EDIT: The SpatialPolygonsDataFrame must be created from the list of SpatialPolygons! As my original data doesn´t contains SpatialPolygons as separate values but the list of SpatialPolygons. The way I received this list is different form the one in the example. I posted the example to show the data structure of the list.

example of a list of SpatialPolygons taken from https://stat.ethz.ch/pipermail/r-sig-geo/2013-January/017225.html:

library(sp)
grd <- GridTopology(c(0.5, 0.5), c(1, 1), c(6, 6))
Spol <- as(grd, "SpatialPolygons")
list_of_SPolsu <- lapply(slot(Spol, "polygons"), function(x)
   SpatialPolygons(list(x)))
list_of_SPols <- lapply(slot(Spol, "polygons"), function(x) {
   Pol <- x
   slot(Pol, "ID") <- "1"
   SpatialPolygons(list(Pol))
})

Regards!

N'ya
  • 347
  • 3
  • 13
  • 1
    You link contains already info on how to combine the list into one SpatialPolygon. After that, do `as(mySpatialPolygon, "SpatialPolygonsDataFrame")`, and you got a SpatialPolygonsDataFrame. – lukeA Oct 23 '17 at 13:18
  • Unfortuneatly my data is slightly different from the one in the example. I don´t have seperate SpatialPolygons but just a list of Spatial Polygons. I´ll edit my question in order to make it more clear. – N'ya Oct 23 '17 at 14:17

2 Answers2

5

Try this:

#Creating a dataframe with Spol IDs
Spol_df<- as.data.frame(sapply(slot(Spol, "polygons"), function(x) slot(x, "ID")))

#Making the IDs row names 
row.names(Spol_df) <- sapply(slot(Spol, "polygons"), function(x) slot(x, "ID"))

# Making the spatial polygon data frame
Spol_SPDF <- SpatialPolygonsDataFrame(Spol, data =Spol_df)

Let me know if it worked

Edit:

Just to make the answer complete according to the Edit in the question... The solution on how to make a SpatialPolygon from a list of polygons comes from the provided link:

#Getting polygon IDs
IDs <- sapply(list_of_SPols, function(x)
  slot(slot(x, "polygons")[[1]], "ID"))

#Checking
length(unique(IDs)) == length(list_of_SPols)

#Making SpatialPolygons from list of polygons
Spol <- SpatialPolygons(lapply(list_of_SPols,
                                function(x) slot(x, "polygons")[[1]]))
mnky9800n
  • 1,113
  • 2
  • 15
  • 33
Thai
  • 493
  • 5
  • 15
  • your way works, but unfortuenatly the way I received the list of SpatialPolygons is different than in the example. I posted the example in order to show my data structure. My data doesn´t include the SpatialPolygons as seperate values. Therefore I have to create the SpatialPolygonsDataFrame from the list which contains the SpatialPolygons. – N'ya Oct 23 '17 at 14:16
  • I can't understand... the link you provided already has the information on how to convert a list of polygons to a formal class spatial polygons object... And I just told you how to make a Spatial Polygons Data Frame from a Spatial Polygons... What else are you looking for? You just have to change the names I used by the name you gave when you created your SpatialPolygons from the list... (Take out Spol from the 3 code lines I gave you... put the name of your object instead).. Got it? – Thai Oct 23 '17 at 14:43
  • First command returns Error in slot(shp, "polygons") : cannot get a slot ("polygons") from an object of type "list" – Peter.k Jun 18 '19 at 15:03
4

You can create a list of SpatialPolygonDataFrame objects, from a list of SpatialPolygon objects like this:

z <- lapply(list_of_SPols, function(i) SpatialPolygonsDataFrame(i, data.frame(id=1:length(i)), match.ID = FALSE))

Or, because in this case all SpatialPolygon objects have length 1

df <- data.frame(id=1)
z <- lapply(list_of_SPols, function(i) SpatialPolygonsDataFrame(i, df, match.ID = FALSE))

You can also first combine the SpatialPolygons into a single object

library(raster)
sp <- bind(list_of_SPols)
spdf <- SpatialPolygonsDataFrame(sp, data.frame(id=1:length(sp)))    

(the above may not address the problem the OP had, but it does address the question asked --- and thus this answer may be helpful for folks looking for an answer to that question).

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63