Simon Besnard
Simon Besnard

Reputation: 397

Create a list of Spatial Polygon dataframe from a list of dataframe

I want Create a list of Spatial Polygon dataframe from a list of dataframe. The list of Spatial polygons is called list_sp_Tanzania and the list of dataframe is called list_df_Tanzania_Modis500. Each list contains 61 objects and each objects contains several polygons.

str(list_df_Tanzania_Modis500)
$ :'data.frame':    30 obs. of  11 variables:
  ..$ ID        : int [1:30] 296 298 321 323 324 330 331 361 419 453 ...
  ..$ LU_1990   : int [1:30] 11 11 11 11 11 11 11 11 11 11 ...
  ..$ LU_2000   : num [1:30] 12 12 12 12 12 12 12 12 12 12 ...
  ..$ CHLU_90_00: chr [1:30] "1112" "1112" "1112" "1112" ...
  ..$ LU_2005   : num [1:30] 12 12 12 12 12 12 12 12 12 15 ...
  ..$ CHLU_00_05: chr [1:30] "1212" "1212" "1212" "1212" ...
  ..$ Tile      : Factor w/ 1 level "S11_E039": 1 1 1 1 1 1 1 1 1 1 ...
  ..$ UNIQ_ID   : Factor w/ 30 levels "S11_E039_296",..: 1 2 3 4 5 6 7 8 9 10 ...
  ..$ AREA      : num [1:30] 219337 347133 393961 181875 105137 ...
  ..$ Sour_90_00: chr [1:30] "Modis500_2000" "Modis500_2000" "Modis500_2000" "Modis500_2000" ...
  ..$ Sour_00_05: chr [1:30] "Modis500_2005" "Modis500_2005" "Modis500_2005" "Modis500_2005" ...

str(list_sp_Tanzania)
[[61]]
class       : SpatialPolygons 
features    : 30 
extent      : 38.95413, 39.04577, -11.04522, -10.95469  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

Unfortunately, I cannot provide the data with dput because they are s4 objects and I could not find similar examples. Hope someone can help me somehow though.

Upvotes: 0

Views: 1247

Answers (1)

Simon Besnard
Simon Besnard

Reputation: 397

Here is the solution.

fun <- function(x, y) {
  SpatialPolygonsDataFrame(x, y, match.ID = F)
}

list_Spdf_Tanzania_Modis500 <- mapply(FUN = fun, 
                                      x = list_sp_Tanzania, 
                                      y = list_df_Tanzania_Modis500)

Upvotes: 2

Related Questions