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.5965503 -0.07088004
#>  [2,]  0.5418170  0.64826517
#>  [3,] -2.4237233 -1.10197330
#>  [4,] -1.1368640  1.40505937
#>  [5,] -1.7913891 -0.29780352
#>  [6,]  0.2979544 -0.77515730
#>  [7,]  0.8677669 -0.80490508
#>  [8,]  1.0274824  0.12218325
#>  [9,]  1.0973595 -2.00258639
#> [10,] -0.5965503 -0.07088004
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
sf_remove_holes( sfc )
#> [[1]]
#> [[1]]
#>            [,1]        [,2]
#> [1,] -0.5965503 -0.07088004
#> [2,]  0.5418170  0.64826517
#> [3,] -2.4237233 -1.10197330
#> [4,] -0.5965503 -0.07088004
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
#> 
#> [[2]]
#> [[1]]
#>            [,1]       [,2]
#> [1,]  1.4452917 -1.4920759
#> [2,]  0.4737215  0.6697399
#> [3,] -1.2316180 -1.4771427
#> [4,]  1.4452917 -1.4920759
#> 
#> 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 
#> -2.423723 -2.002586  1.445292  1.405059 
#> attr(,"class")
#> [1] "bbox"
sf_remove_holes( sf )
#>   ml_id
#> 1     1
#> 2     2
#>                                                                                               geometry
#> 1 -0.59655034, 0.54181699, -2.42372331, -0.59655034, -0.07088004, 0.64826517, -1.10197330, -0.07088004
#> 2           1.4452917, 0.4737215, -1.2316180, 1.4452917, -1.4920759, 0.6697399, -1.4771427, -1.4920759