Learn R Programming

terra (version 1.9-11)

rast: Create a SpatRaster

Description

Methods to create a SpatRaster. These objects can be created from scratch, from a filename, or from another object.

A SpatRaster represents a spatially referenced surface divided into three dimensional cells (rows, columns, and layers).

When a SpatRaster is created from one or more files, it does not load the cell (pixel) values into memory (RAM). It only reads the parameters that describe the geometry of the SpatRaster, such as the number of rows and columns and the coordinate reference system. The actual values will be read when needed.

Note that there are operating system level limitations to the number of files that can be opened simultaneously. Using a SpatRaster of very many files (e.g. 10,000) may cause R to crash when you use it in a computation. In situations like that you may need to split up the task or combine data into fewer (multi-layer) files. Also note that the GTiff format used for temporary files cannot store more than 65,535 layers in a single file.

Usage

# S4 method for character
rast(x, subds=0, lyrs=NULL, drivers=NULL, opts=NULL, win=NULL, 
		snap="near", vsi=FALSE, raw=FALSE, noflip=FALSE, 
		guessCRS=TRUE, domains="", md=FALSE, dims=NULL)

# S4 method for missing rast(x, nrows=180, ncols=360, nlyrs=1, xmin=-180, xmax=180, ymin=-90, ymax=90, crs, extent, resolution, vals, names, time, units)

# S4 method for SpatRaster rast(x, nlyrs=nlyr(x), names, vals, keeptime=TRUE, keepunits=FALSE, props=FALSE, tags=FALSE)

# S4 method for matrix rast(x, type="", crs="", digits=6, extent=NULL)

# S4 method for data.frame rast(x, type="xyz", crs="", digits=6, extent=NULL)

# S4 method for array rast(x, crs="", extent=NULL)

# S4 method for list rast(x, warn=TRUE)

# S4 method for SpatRasterDataset rast(x)

# S4 method for SpatVector rast(x, type="", ...) # S4 method for SpatExtent rast(x, ...)

Arguments

Value

SpatRaster

Details

Files are read with the GDAL library. GDAL guesses the file format from the name, and/or tries reading it with different "drivers" (see gdal) until it succeeds. In very few cases this may cause a file to be opened with the wrong driver, and some information may be lost. For example, when a netCDF file is opened with the HDF5 driver. You can avoid that by using argument rast("filename.ncdf", drivers="NETCDF")

These classes hold a C++ pointer to the data "reference class" and that creates some limitations. They cannot be recovered from a saved R session either or directly passed to nodes on a computer cluster. Generally, you should use writeRaster to save SpatRaster objects to disk (and pass a filename or cell values of cluster nodes). Also see wrap.

See Also

sds to create a SpatRasterDataset (SpatRasters with the same geometry representing different variables or higher dimension), sprc to create a SpatRasterCollection (to combine SpatRasters with different geometries), and vect for vector (points, lines, polygons) data

Examples

Run this code
# Create a SpatRaster from scratch
x <- rast(nrows=108, ncols=21, xmin=0, xmax=10)

# Create a SpatRaster from a file
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

# A file with multiple layers. This one is special as the layers are RGB color channels 
s <- rast(system.file("ex/logo.tif", package="terra"))

# remove the color channels
#plot(s)
#RGB(s) <- NULL
#plot(s)

# Create a skeleton with no associated cell values
rast(s)

# from a matrix 
m <- matrix(1:25, nrow=5, ncol=5)
rm <- rast(m)

# from a "xyz" data.frame
d <- as.data.frame(rm, xy=TRUE)
head(d)
rast(d, type="xyz")

Run the code above in your browser using DataLab