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 a file, 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.
# S4 method for character
rast(x, subds=0, lyrs=NULL, opts=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)
# 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)
# S4 method for SpatRasterDataset
rast(x)
# S4 method for SpatVector
rast(x, ...)
# S4 method for SpatExtent
rast(x, ...)
filename (character), missing, SpatRaster, SpatRasterDataset, SpatExtent, SpatVector, matrix, array, list of SpatRaster objects. For other types it will be attempted to create a SpatRaster via (`as(x, "SpatRaster")`
positive integer or character to select a sub-dataset. If zero or "", all sub-datasets are returned (if possible)
positive integer or character to select a subset of layers (a.k.a. "bands")
character. GDAL dataset open options
positive integer. Number of rows
positive integer. Number of columns
positive integer. Number of layers
minimum x coordinate (left border)
maximum x coordinate (right border)
minimum y coordinate (bottom border)
maximum y coordinate (top border)
character. Description of the Coordinate Reference System (map projection) in PROJ.4
, WKT
or authority:code
notation. If this argument is missing, and the x coordinates are within -360 .. 360 and the y coordinates are within -90 .. 90, longitude/latitude is assigned
logical. If FALSE
the time stamps are discarded
logical. If FALSE
the layer units are discarded
logical. If TRUE
the properties (categories and color-table) are kept
object of class SpatExtent. If present, the arguments xmin, xmax, ymin and ymax are ignored
numeric vector of length 1 or 2 to set the resolution (see res
). If this argument is used, arguments ncol
and nrow
are ignored
numeric. An optional vector with cell values (if fewer values are provided, these are recycled to reach the number of cells)
character. An optional vector with layer names (must match the number of layers)
time or date stamps for each layer
character. units for each layer
character. If the value is not "xyz"
, the raster has the same number of rows and colums as the matrix. If the value is "xyz"
, the matrix must have at least two columns, the first with x
(or longitude) and the second with y
(or latitude) coordinates that represent the centers of raster cells. The additional columns are the values associated with the raster cells.
integer to set the precision for detecting whether points are on a regular grid (a low number of digits is a low precision). Only used when type="xyz"
additional arguments, in some cases passed on to the rast,missing-method
SpatRaster
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 prepending the driver name to the filename like this: rast('NETCDF:"filename.ncdf"')
sds
to create a SpatRasterDataset (4 dimensions) and vect
for vector (points, lines, polygons) data
# NOT RUN {
# 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)
s <- rast(system.file("ex/logo.tif", package="terra"))
# 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