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,534
INT4U04,294,967,296
INT8U018,446,744,073,709,551,616
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 types, except the single byte types, the lowest (signed) or highest (unsigned) value is used to store NA.

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

INT4U is available but they are best avoided as R does not support 32-bit unsigned integers.

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] ""