When writing raster-type objects to disk, a datatype can be specified. These functions help identify what smallest datatype can be used.

assessDataType(ras, type = "writeRaster")

# S3 method for Raster
assessDataType(ras, type = "writeRaster")

# S3 method for RasterStack
assessDataType(ras, type = "writeRaster")

# S3 method for default
assessDataType(ras, type = "writeRaster")

assessDataTypeGDAL(ras)

Arguments

ras

The RasterLayer or RasterStack for which data type will be assessed.

type

Character. "writeRaster" (default) or "GDAL" to return the recommended data type for writing from the raster packages, respectively, or "projectRaster" to return recommended resampling type.

Value

A character string indicating the data type of the spatial layer (e.g., "INT2U"). See terra::datatype() or raster::dataType()

Author

Eliot McIntire, Ceres Barros, Ian Eddy, and Tati Micheletti

Examples

## LOG1S
library(raster)
#> Loading required package: sp
ras <- raster(ncol = 10, nrow = 10)
ras[] <- rep(c(0,1),50)
assessDataType(ras)
#> [1] "LOG1S"

ras[] <- rep(c(TRUE,FALSE),50)
assessDataType(ras)
#> [1] "LOG1S"

ras[] <- c(NA, NA, rep(c(0,1),49))
assessDataType(ras)
#> [1] "LOG1S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- c(0, NaN, rep(c(0,1),49))
assessDataType(ras)
#> [1] "LOG1S"


## INT1S
ras[] <- -1:98
assessDataType(ras)
#> [1] "INT1S"

ras[] <- c(NA, -1:97)
assessDataType(ras)
#> [1] "INT1S"

## INT1U
ras <- raster(ncol = 10, nrow = 10)
ras[] <- 1:100
assessDataType(ras)
#> [1] "INT1U"

ras[] <- c(NA, 2:100)
assessDataType(ras)
#> [1] "INT1U"

## INT2U
ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = 64000, max = 65000))
assessDataType(ras)
#> [1] "INT2U"

## INT2S
ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = -32767, max = 32767))
assessDataType(ras)
#> [1] "INT2S"

ras[54] <- NA
assessDataType(ras)
#> [1] "INT2S"

## INT4U
ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = 0, max = 500000000))
assessDataType(ras)
#> [1] "INT4U"

ras[14] <- NA
assessDataType(ras)
#> [1] "INT4U"

## INT4S
ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = -200000000, max = 200000000))
assessDataType(ras)
#> [1] "INT4S"

ras[14] <- NA
assessDataType(ras)
#> [1] "INT4S"

## FLT4S
ras <- raster(ncol = 10, nrow = 10)
ras[] <- runif(100, min = -10, max = 87)
assessDataType(ras)
#> [1] "FLT4S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = -3.4e+26, max = 3.4e+28))
assessDataType(ras)
#> [1] "FLT4S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = 3.4e+26, max = 3.4e+28))
assessDataType(ras)
#> [1] "FLT4S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = -3.4e+26, max = -1))
assessDataType(ras)
#> [1] "FLT4S"

## FLT8S
ras <- raster(ncol = 10, nrow = 10)
ras[] <- c(-Inf, 1, rep(c(0,1),49))
assessDataType(ras)
#> [1] "FLT8S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- c(Inf, 1, rep(c(0,1),49))
assessDataType(ras)
#> [1] "FLT8S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = -1.7e+30, max = 1.7e+308))
assessDataType(ras)
#> [1] "FLT8S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = 1.7e+30, max = 1.7e+308))
assessDataType(ras)
#> [1] "FLT8S"

ras <- raster(ncol = 10, nrow = 10)
ras[] <- round(runif(100, min = -1.7e+308, max = -1))
assessDataType(ras)
#> [1] "FLT8S"

# stack
ras <- raster(ncol = 10, nrow = 10)
ras[] <- rep(c(0,1),50)
ras1 <- raster(ncol = 10, nrow = 10)
ras1[] <- round(runif(100, min = -1.7e+308, max = -1))
sta <- stack(ras, ras1)
assessDataType(sta)
#> [1] "LOG1S" "FLT8S"