Removes holes from polygons and multipolygons. Points and linestrings are unaffected.

sf_remove_holes(obj, close = TRUE)

Arguments

obj

sfg, sfc or sf object.

close

logical indicating whether polygons should be closed. If TRUE, all polygons will be checked and force closed if possible

Examples

df <- data.frame(
  ml_id = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2)
  , l_id = c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2)
  , x = rnorm(15)
  , y = rnorm(15)
  , z = rnorm(15)
  , m = rnorm(15)
)

sfg <- sfg_polygon( obj = df, x = "x", y = "y", linestring_id = "ml_id" )
sfc <- sfc_polygon( obj = df, x = "x", y = "y", polygon_id = "ml_id", linestring_id = "l_id" )
sf <- sf_polygon( obj = df, x = "x", y = "y", polygon_id = "ml_id", linestring_id = "l_id" )

sf_remove_holes( sfg )
#> [[1]]
#>             [,1]        [,2]
#>  [1,] -0.9984326 -1.68650474
#>  [2,]  1.2333901 -0.90281495
#>  [3,]  0.3404245  1.31763370
#>  [4,] -0.4727025  1.10018974
#>  [5,]  0.7087531  1.20376784
#>  [6,] -1.5289587 -1.43127078
#>  [7,]  0.2374253  1.38291086
#>  [8,] -1.3128142  0.00312594
#>  [9,]  0.7470286 -0.07788682
#> [10,] -0.9984326 -1.68650474
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
sf_remove_holes( sfc )
#> [[1]]
#> [[1]]
#>            [,1]       [,2]
#> [1,] -0.9984326 -1.6865047
#> [2,]  1.2333901 -0.9028149
#> [3,]  0.3404245  1.3176337
#> [4,] -0.9984326 -1.6865047
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
#> 
#> [[2]]
#> [[1]]
#>             [,1]       [,2]
#> [1,] -1.56251843  0.4414282
#> [2,]  0.07105336  0.1289229
#> [3,] -0.63953477 -0.8302143
#> [4,] -1.56251843  0.4414282
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
#> 
#> attr(,"n_empty")
#> [1] 0
#> attr(,"crs")
#> $input
#> [1] NA
#> 
#> $wkt
#> [1] NA
#> 
#> attr(,"class")
#> [1] "crs"
#> attr(,"class")
#> [1] "sfc_POLYGON" "sfc"        
#> attr(,"precision")
#> [1] 0
#> attr(,"bbox")
#>      xmin      ymin      xmax      ymax 
#> -1.562518 -1.686505  1.233390  1.382911 
#> attr(,"class")
#> [1] "bbox"
sf_remove_holes( sf )
#>   ml_id
#> 1     1
#> 2     2
#>                                                                                             geometry
#> 1        -0.9984326, 1.2333901, 0.3404245, -0.9984326, -1.6865047, -0.9028149, 1.3176337, -1.6865047
#> 2 -1.56251843, 0.07105336, -0.63953477, -1.56251843, 0.44142823, 0.12892290, -0.83021426, 0.44142823