These must be very common for this function to be useful. Currently, the only meaningful method is on SpatialPolygons, and it runs sf::st_is_valid. If FALSE, then it runs st_make_valid or terra::buffer, depending on whether x is sf or SpatialPolygons*, respectively.

This uses terra::buffer(..., width = 0) internally, which fixes some failures to sf::st_is_valid

fixErrors(
  x,
  objectName,
  attemptErrorFixes = TRUE,
  useCache = getOption("reproducible.useCache", FALSE),
  verbose = getOption("reproducible.verbose", 1),
  testValidity = getOption("reproducible.testValidity", TRUE),
  ...
)

# S3 method for default
fixErrors(
  x,
  objectName,
  attemptErrorFixes = TRUE,
  useCache = getOption("reproducible.useCache", FALSE),
  verbose = getOption("reproducible.verbose", 1),
  testValidity = getOption("reproducible.testValidity", TRUE),
  ...
)

# S3 method for Raster
fixErrors(
  x,
  objectName,
  attemptErrorFixes = TRUE,
  useCache = getOption("reproducible.useCache", FALSE),
  verbose = getOption("reproducible.verbose", 1),
  testValidity = getOption("reproducible.testValidity", TRUE),
  ...
)

# S3 method for SpatialPolygons
fixErrors(
  x,
  objectName = NULL,
  attemptErrorFixes = TRUE,
  useCache = getOption("reproducible.useCache", FALSE),
  verbose = getOption("reproducible.verbose", 1),
  testValidity = getOption("reproducible.testValidity", TRUE),
  ...
)

# S3 method for sf
fixErrors(
  x,
  objectName = NULL,
  attemptErrorFixes = TRUE,
  useCache = getOption("reproducible.useCache", FALSE),
  verbose = getOption("reproducible.verbose", 1),
  testValidity = getOption("reproducible.testValidity", TRUE),
  ...
)

Arguments

x

A SpatialPolygons* or sf object.

objectName

Optional. This is only for messaging; if provided, then messages relayed to user will mention this.

attemptErrorFixes

Will attempt to fix known errors. Currently only some failures for SpatialPolygons* are attempted. Notably with terra::buffer(..., width = 0). Default TRUE, though this may not be the right action for all cases.

useCache

Logical, default getOption("reproducible.useCache", FALSE), whether Cache is used on the internal terra::buffer command.

testValidity

Logical. If TRUE, the a test for validity will happen before actually running buffering (which is the solution in most cases). However, sometimes it takes longer to test for validity than just buffer without testing (there are no consequences of buffering if everything is valid). If FALSE, then the test will be skipped and the buffering will happen regardless. If NA, then all testing and buffering will be skipped.

...

Passed to methods. None currently implemented.

Value

A GIS file (e.g., RasterLayer, SpatRaster etc.) that has been attempted to be fixed, if it finds errors.

Examples

library(sp)
library(raster)

# make a SpatialPolygon
coords1 <- structure(c(-123.98, -117.1, -80.2, -100, -123.98, 60.9, 67.73, 65.58, 51.79, 60.9),
                       .Dim = c(5L, 2L))
Sr1 <- Polygon(coords1)
Srs1 <- Polygons(list(Sr1), "s1")
shpEcozone <- SpatialPolygons(list(Srs1), 1L)
crs(shpEcozone) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
fixErrors(shpEcozone)
#> Checking for errors in SpatialPolygon
#>   Found no errors.
#> class       : SpatialPolygons 
#> features    : 1 
#> extent      : -123.98, -80.2, 51.79, 67.73  (xmin, xmax, ymin, ymax)
#> crs         : +proj=longlat +datum=WGS84 +no_defs