Skip to contents

Get the data types of the fields (attributes, variables) of a SpatVector or of the file(s) associated with a SpatRaster. A (layer of a) SpatRaster has no datatype if it has no values, or if the values are in memory.

Usage

# S4 method for class 'SpatRaster'
datatype(x, bylyr=TRUE)

# S4 method for class 'SpatVector'
datatype(x)

Arguments

x

SpatRaster or SpatVector

bylyr

logical. If TRUE a value is returned for each layer. Otherwise, a value is returned for each data source (such as a file)

Details

Setting the data type is useful if you want to write values to disk with writeRaster. In other cases you can use functions such as round and floor, or as.bool

raster datatypes are described by 5 characters. The first three indicate whether the values are integer or decimal values. The fourth character indicates the number of bytes used to save the values on disk, and the last character indicates whether the numbers are signed (that is, can be negative and positive values) or not (only zero and positive values allowed)

The following raster datatypes are available:

Datatype definitionminimum possible valuemaximum possible value
INT1U0255
INT2U065,535
INT4U04,294,967,296
INT8U018,446,744,073,709,551,616
INT1S-128128
INT2S-32,76732,767
INT4S-2,147,483,6472,147,483,647
INT8S-9,223,372,036,854,775,8089,223,372,036,854,775,808
FLT4S-3.4e+383.4e+38
FLT8S-1.7e+3081.7e+308

For all integer and byte types the lowest (signed) or highest (unsigned) value is used to store NA. For float types NaN is used (following the IEEE 754 standard).

Note that very large integer numbers may be imprecise as they are internally represented as decimal numbers.

Also note that NaN may not be equally supported by all implementations. For example OGR SQL and SQLite queries generally convert NaN values to NULL.

INT4U and INT8U are available but they are best avoided as R does not support 32-bit or 64-bit unsigned integers. INT8U is a special case where the NA store value is 18446744073709549568 (UINT64_MAX - 1101) because of precision in decimal representation.

INT8U and INT8S require GDAL version 3.5 or higher. INT1S requires GDAL version 3.7 or higher.

Value

character

See also

Raster data types to check / set the type of SpatRaster values.

Examples

f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
datatype(v)
#> [1] "double" "string" "double" "string" "double" "double"

f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
datatype(r)
#> [1] "INT2S"

# no data type
datatype(rast()) 
#> [1] ""