Learn R Programming

sf (version 0.4-3)

valid: Validity operations on simple feature geometries

Description

Check validity on simple feature geometries, or make geometries valid

Usage

st_is_valid(x, NA_on_exception = TRUE, reason = FALSE)

st_make_valid(x)

Arguments

x

object of class sfg, sfg or sf

NA_on_exception

logical; if TRUE, for polygons that would otherwise raise an GEOS error (exception, e.g. for a POLYGON having more than zero but less than 4 points, or a LINESTRING having one point) return an NA rather than raising an error, and suppress warning messages (e.g. about self-intersection); if FALSE, regular GEOS errors and warnings will be emitted.

reason

logical; if TRUE, return a character with, for each geometry, the reason for invalidity, NA on exception, or "Valid Geometry" otherwise.

Value

matrix (sparse or dense); if dense: of type character for relate, numeric for distance, and logical for all others; matrix has dimension x by y; if sparse (only possible for those who return logical in case of dense): return list of length length(x) with indices of the TRUE values for matching y.

object of the same class as x

Details

st_make_valid uses the lwgeom_makevalid method also used by the PostGIS command ST_makevalid. It is only available if the package was linked against liblwgeom, which is currently not the case for the binary CRAN distributions; see the package source code repository for instructions how to install liblwgeom. The example below shows how to run-time check the availability of liblwgeom.

Examples

Run this code
p1 = st_as_sfc("POLYGON((0 0, 0 10, 10 0, 10 10, 0 0))")
st_is_valid(p1)
st_is_valid(st_sfc(st_point(0:1), p1[[1]]), reason = TRUE)
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)))))
if (!is.na(sf_extSoftVersion()["lwgeom"])) {
  suppressWarnings(st_is_valid(x))
  y = st_make_valid(x)
  st_is_valid(y)
  y %>% st_cast()
}

Run the code above in your browser using DataLab