Skip to contents

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

Usage

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.90281495 -0.82860353
#>  [2,]  1.31763370  0.28977446
#>  [3,]  1.10018974 -0.48005348
#>  [4,]  1.20376784 -0.60482935
#>  [5,] -1.43127078  1.46011018
#>  [6,]  1.38291086  0.14967935
#>  [7,]  0.00312594 -1.43332110
#>  [8,] -0.07788682 -0.01030332
#>  [9,]  0.44142823 -0.21223603
#> [10,] -0.90281495 -0.82860353
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
sf_remove_holes( sfc )
#> [[1]]
#> [[1]]
#>            [,1]       [,2]
#> [1,] -0.9028149 -0.8286035
#> [2,]  1.3176337  0.2897745
#> [3,]  1.1001897 -0.4800535
#> [4,] -0.9028149 -0.8286035
#> 
#> attr(,"class")
#> [1] "XY"      "POLYGON" "sfg"    
#> 
#> [[2]]
#> [[1]]
#>            [,1]       [,2]
#> [1,]  0.1289229 -0.9063402
#> [2,] -0.8302143 -2.1021525
#> [3,] -0.5035929  1.8933605
#> [4,]  0.1289229 -0.9063402
#> 
#> 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.431271 -2.102152  1.455841  1.893360 
#> attr(,"class")
#> [1] "bbox"
sf_remove_holes( sf )
#>   ml_id
#> 1     1
#> 2     2
#>                                                                                      geometry
#> 1 -0.9028149, 1.3176337, 1.1001897, -0.9028149, -0.8286035, 0.2897745, -0.4800535, -0.8286035
#> 2 0.1289229, -0.8302143, -0.5035929, 0.1289229, -0.9063402, -2.1021525, 1.8933605, -0.9063402