Skip to contents

Two SpatRasters are compatible (in terms of combining layers) if the crs, extent and resolution are similar. In those cases you can combine the SpatRasters simply as c(x, y).

This function compares those attributes informing of the results. See Solving issues section for minimal guidance.

Usage

compare_spatrasters(x, y, digits = 6)

Arguments

x, y

SpatRaster objects

digits

Integer to set the precision for comparing the extent and the resolution.

Value

A invisible logical TRUE/FALSE indicating if the SpatRasters are compatible, plus an informative message flagging the issues found (if any).

Solving issues

On non-equal crs, try terra::project(). On non-equal extent try terra::resample(). On non-equal resolution you can try terra::resample(), terra::aggregate() or terra::disagg().

See also

Examples

library(terra)

x <- rast(matrix(1:90, ncol = 3), crs = "EPSG:3857")

# Nothing
compare_spatrasters(x, x)

# Different crs
y_nocrs <- x
crs(y_nocrs) <- NA

compare_spatrasters(x, y_nocrs)
#> ! Results of `tidyterra::compare_spatrasters()`: 
#> The following attributes are not equal:
#>  crs

# Different extent
compare_spatrasters(x, x[1:10, , drop = FALSE])
#> ! Results of `tidyterra::compare_spatrasters()`: 
#> The following attributes are not equal:
#>  extent

# Different resolution
y_newres <- x

res(y_newres) <- res(x) / 2
compare_spatrasters(x, y_newres)
#> ! Results of `tidyterra::compare_spatrasters()`: 
#> The following attributes are not equal:
#>  resolution

# Everything

compare_spatrasters(x, project(x, "epsg:3035"))
#> ! Results of `tidyterra::compare_spatrasters()`: 
#> The following attributes are not equal:
#>  crs
#>  extent
#>  resolution