This function checks if the provided tiff file is corrupted by attempting to
describe it using the terra
package and searching for the presence of a
"Driver" string in the description, which indicates a valid tiff file. If the
string is found, the function returns TRUE
and FALSE
otherwise. The
function works also for reading netCDF files with the terra
package.
Value
Logical; returns TRUE
if the TIFF file is not corrupted (i.e., the
file exists, can be described with a "Driver" in its metadata, has values,
and its data can be read without errors or warnings), and FALSE
otherwise.
Examples
(f <- system.file("ex/elev.tif", package="terra"))
#> [1] "/home/runner/work/_temp/Library/terra/ex/elev.tif"
check_tiff(x = f)
#> [1] TRUE
# a temp file ends with .tif (not a valid tiff file)
(temp_file <- tempfile(fileext = ".tif"))
#> [1] "/tmp/RtmphTiYIO/file211143b73af7.tif"
fs::file_create(temp_file)
check_tiff(x = temp_file)
#> [1] FALSE
# clean up
fs::file_delete(temp_file)