Make an invalid geometry valid

st_make_valid(x)

Arguments

x

object of class sfg, sfg or sf

Value

Object of the same class as x

Details

st_make_valid uses the lwgeom_makevalid method also used by the PostGIS command ST_makevalid if the GEOS version linked to is smaller than 3.8.0, and otherwise the version shipped in GEOS.

Examples

library(sf) x = st_sfc(st_polygon(list(rbind(c(0,0),c(0.5,0),c(0.5,0.5),c(0.5,0),c(1,0),c(1,1),c(0,1),c(0,0))))) suppressWarnings(st_is_valid(x))
#> [1] FALSE
y = st_make_valid(x) st_is_valid(y)
#> [1] TRUE
y %>% st_cast()
#> Geometry set for 2 features #> geometry type: GEOMETRY #> dimension: XY #> bbox: xmin: 0 ymin: 0 xmax: 1 ymax: 1 #> CRS: NA
#> POLYGON ((0.5 0, 0 0, 0 1, 1 1, 1 0, 0.5 0))
#> LINESTRING (0.5 0, 0.5 0.5)