Split polygon parts of a single SpatialPolygons Object

前端 未结 4 2175
长发绾君心
长发绾君心 2021-02-20 02:50

In R, I have single SpatialPolygons object (i.e. multi-polygons) containing several hundred polygons. I would like to split this SpatialPolygons object

4条回答
  •  温柔的废话
    2021-02-20 03:23

    To separate multipolygon objects into single polygons (with holes if present) you can do

    d <- disaggregate(p)
    

    Where p is a SpatialPolygons object. After that, you can use d@polygons.

    For example

    library(sp)
    library(raster)
    ### example data
    p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
    hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20))
    p1 <- list(p1, hole)
    p2 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
    p3 <- rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))
    pols <- spPolygons(p1, p2, p3)
    ###
    
    a <- aggregate(pols, dissolve=FALSE)
    d <- disaggregate(a)
    

提交回复
热议问题