In R, I have single SpatialPolygons object (i.e. multi-polygons) containing several hundred polygons. I would like to split this SpatialPolygons object
As I understand it, the OP wants to convert a SpatialPolygons object into a list of Polygons, preserving holes if present.
The SpP object created by the OP contains three polygons, the third of which has an associated hole.
You can use lapply to cycle through each polygon in SpP, returning a list of SpatialPolygons. The difference between a Polygons and SpatialPolygons object is the addition of plot order information. Since each resulting SpatialPolygons is of length = 1, however, this information is superfluous.
n_poly <- length(SpP)
out <- lapply(1:n_poly, function(i) SpP[i, ])
lapply(out, class)
> lapply(out, class)
[[1]]
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
[[2]]
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
[[3]]
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
plot(out[[3]]) # Hole preserved
If a list of Polygons is needed, simply pull the appropriate slot from the SpatialPolygons object:
out <- lapply(1:n_poly, function(i) SpP[i, ]@polygons[[1]])
lapply(out, class)
> lapply(out, class)
[[1]]
[1] "Polygons"
attr(,"package")
[1] "sp"
[[2]]
[1] "Polygons"
attr(,"package")
[1] "sp"
[[3]]
[1] "Polygons"
attr(,"package")
[1] "sp"