gvan
gvan

Reputation: 523

Subsetting Polygons from Spatial Polygons object by slot

I have 'SpatialPolygons' object and I want to subset the polygons where @hole == FALSE and make a new object.

str(my_object) returns this: structure output of my SpatialPolygons object

Using the code below predictably get 1 polygon at a time (or none if @hole == TRUE), but I am not able to subset multiple polygons from my object.

newSP <- aDis[aDis@polygons[[1]]@Polygons[[1]]@hole == FALSE]

I think my issue lies in that I don't know how to reference the second list "in general", i.e. take the items from list(Polygon) where @hole == FALSE. Leaving the second [[]] blank does not work.

P.S. sorry there is no reproducible example; I'm not sure exactly how to recreate this issue in a simple way.

Upvotes: 0

Views: 542

Answers (1)

JDL
JDL

Reputation: 1654

The easiest way to do this is with sapply:

hasHole <- sapply(
  aDis@polygons[[1]]@Polygons,
  slot,
  "hole"
)
aDis@polygons[[1]]@Polygons[!hasHole]

It's unusual to have to resort to this sort of thing as an end user, though. I'm not familiar with this particular package --- are there no functions defined elsewhere in it that help you to access these slots?

Upvotes: 2

Related Questions