The function removes all polygon holes and return the modified layer

st_remove_holes(x, max_area = 0)

Arguments

x

Object of class sf, sfc or sfg, of type "POLYGON" or "MULTIPOLYGON"

max_area

Maximum area of holes to be removed (numeric), in the units of x or in [m^2] for layers in geographic projection (lon/lat). Default value (0) causes removing all holes.

Value

Object of same class as x, with holes removed

Note

See function sfheaders::sf_remove_holes for a highly-optimized faster alternative if you don't need the argument max_area: https://github.com/dcooley/sfheaders

References

Following the StackOverflow answer by user lbusett:

https://stackoverflow.com/questions/52654701/removing-holes-from-polygons-in-r-sf

Examples

opar = par(mfrow = c(1, 2))

# Example with 'sfg' - POLYGON
p1 = rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
p2 = rbind(c(1,1), c(1,2), c(2,2), c(1,1))
pol = st_polygon(list(p1, p2))
pol
#> POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0), (1 1, 1 2, 2 2, 1 1))
result = st_remove_holes(pol)
result
#> POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0))
plot(pol, col = "#FF000033", main = "Before")
plot(result, col = "#FF000033", main = "After")


# Example with 'sfg' - MULTIPOLYGON
p3 = rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
p4 = rbind(c(3.3,0.3), c(3.8,0.3), c(3.8,0.8), c(3.3,0.8), c(3.3,0.3))[5:1,]
p5 = rbind(c(3,3), c(4,2), c(4,3), c(3,3))
mpol = st_multipolygon(list(list(p1,p2), list(p3,p4), list(p5)))
mpol
#> MULTIPOLYGON (((0 0, 1 0, 3 2, 2 4, 1 4, 0 0), (1 1, 1 2, 2 2, 1 1)), ((3 0, 4 0, 4 1, 3 1, 3 0), (3.3 0.3, 3.3 0.8, 3.8 0.8, 3.8 0.3, 3.3 0.3)), ((3 3, 4 2, 4 3, 3 3)))
result = st_remove_holes(mpol)
result
#> MULTIPOLYGON (((0 0, 1 0, 3 2, 2 4, 1 4, 0 0)), ((3 0, 4 0, 4 1, 3 1, 3 0)), ((3 3, 4 2, 4 3, 3 3)))
plot(mpol, col = "#FF000033", main = "Before")
plot(result, col = "#FF000033", main = "After")


# Example with 'sfc' - POLYGON
x = st_sfc(pol, pol * 0.75 + c(3.5, 2))
x
#> Geometry set for 2 features 
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 5.75 ymax: 5
#> CRS:           NA
#> POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0), (1 1, ...
#> POLYGON ((3.5 2, 4.25 2, 5.75 3.5, 5 5, 4.25 5,...
result = st_remove_holes(x)
result
#> Geometry set for 2 features 
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 5.75 ymax: 5
#> CRS:           NA
#> POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0))
#> POLYGON ((3.5 2, 4.25 2, 5.75 3.5, 5 5, 4.25 5,...
plot(x, col = "#FF000033", main = "Before")
plot(result, col = "#FF000033", main = "After")


# Example with 'sfc' - MULTIPOLYGON
x = st_sfc(pol, mpol * 0.75 + c(3.5, 2))
x
#> Geometry set for 2 features 
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 6.5 ymax: 5
#> CRS:           NA
#> POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0), (1 1, ...
#> MULTIPOLYGON (((3.5 2, 4.25 2, 5.75 3.5, 5 5, 4...
result = st_remove_holes(x)
result
#> Geometry set for 2 features 
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 6.5 ymax: 5
#> CRS:           NA
#> POLYGON ((0 0, 1 0, 3 2, 2 4, 1 4, 0 0))
#> MULTIPOLYGON (((3.5 2, 4.25 2, 5.75 3.5, 5 5, 4...
plot(x, col = "#FF000033", main = "Before")
plot(result, col = "#FF000033", main = "After")


par(opar)

# Example with 'sf'
x = st_sfc(pol, mpol * 0.75 + c(3.5, 2))
x = st_sf(geom = x, data.frame(id = 1:length(x)))
result = st_remove_holes(x)
result
#> Simple feature collection with 2 features and 1 field
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 6.5 ymax: 5
#> CRS:           NA
#>   id                       geometry
#> 1  1 POLYGON ((0 0, 1 0, 3 2, 2 ...
#> 2  2 MULTIPOLYGON (((3.5 2, 4.25...
plot(x, main = "Before")

plot(result, main = "After")


# Example with 'sf' using argument 'max_area'
x = st_sfc(pol, mpol * 0.75 + c(3.5, 2))
x = st_sf(geom = x, data.frame(id = 1:length(x)))
result = st_remove_holes(x, max_area = 0.4)
result
#> Simple feature collection with 2 features and 1 field
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 0 ymin: 0 xmax: 6.5 ymax: 5
#> CRS:           NA
#>   id                       geometry
#> 1  1 POLYGON ((0 0, 1 0, 3 2, 2 ...
#> 2  2 MULTIPOLYGON (((3.5 2, 4.25...
plot(x, main = "Before")

plot(result, main = "After")