0

I have a data.table with a sfc_multipolygon geometry column. Some rows contain an polygon, others a multi polygon. The column was generated by merging polygons using st_union, which seems to generate some holes. However, I need only the external ring of each polygon.

If I run this:

tnp_c$geometry2 <- lapply(tnp_c$geometry, function(x) {
  if (any(class(x)=="MULTIPOLYGON")) 
    y <- st_polygon(list(x[[1]][[1]]))
  else 
    y <- st_polygon(list(x[[1]]))
  y
})

I can obtain the external polygons, but the result is still a list:

> str(tnp_c, max.level=1)
Classes ‘data.table’ and 'data.frame':  80 obs. of  6 variables:
 $ X        : num  -82.7 -82 -83.6 -83.9 -82.2 ...
 $ Y        : num  46.4 46.8 46.5 46.6 45.8 ...
 $ boq      : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ geometry :sfc_MULTIPOLYGON of length 80; first list element: List of 8
  ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
 $ geometry2:List of 80
 - attr(*, ".internal.selfref")=<externalptr> 

Is there a way to force a list to be an sfc_MULTIPOLYGON? Or more generally, is there a simpler way to do this? Thanks in advance for any help!

Carlos Alberto

Carlos Alberto
  • 598
  • 3
  • 9

0 Answers0