I have a big list of SpatialPolygonsDataFrame objects I created using lapply
and gdal_polygonizeR
(code here: https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/) on a list of RasterLayer objects. I now want to union the boundaries of the polygon parts that touch within each SpatialPolygonsDataFrame using unionSpatialPolygons
(maptools
). I have tested this by calling an individual SpatialPolygonsDataFrame object, and it seems to work. But, when I try to do it for the list of all SpatialPolygonsDataFrame using lapply
, I get an error. See code below (very sorry my example is not reproducible) and please provide a solution using lapply
or alternative. Thanks
#convert RasterLayers to SpatialPolygonsDataFrame objects
polyl <- lapply(rastl, gdal_polygonizeR)
#test union of polygon parts within individual SpatialPolygonsDataFrame
tmp = unionSpatialPolygons(polyl[[10]], polyl[[10]]$DN)
polyl[[10]] #n = 360 features
tmp #n = 8 features
#run union on all SpatialPolygonsDataFrame in list
polyl_union <- lapply(polyl, unionSpatialPolygons, SpP =
polyl, IDs = polyl$DN)
#Error in FUN(X[[i]], ...) : not a SpatialPolygons object