raster (version 1.0.4)

raster: Create a RasterLayer object

Description

Methods to create a RasterLayer object. RasterLayer objects can be created from scratch, a filename, a Extent object, a matrix, or a Raster* or Spatial* object. In many cases, e.g. when a RasterLayer is created from a file, it does (initially) not contain any cell (pixel) values, it only has the parameters that describe the RasterLayer. If you are new to the raster package, first have a look here: raster-package.

Usage

raster(x, ...)

Arguments

x
a filename, Extent, Raster*, SpatialPixels*, SpatialGrid* object, matrix, or missing
...
additional argumens, see below

Value

  • RasterLayer object

Methods

1) Create a RasterLayer object from a file raster(x, values=FALSE, band=1, ...) rll{ x character. Name of raster file values logical. If TRUE, RasterLayer values are read into memory with readAll band integer. Band number in case of a file of multiple bands, default = 1 ... additional arguments. } Additional arguments: rll{ native Logical. Default is FALSE except when package rgdal is missing. If TRUE, reading and writing of IDRISI, BIL, BSQ, BIP, and ArcAscii files is done with native (raster package) drivers, rather then via rgdal. 'raster', RSAGA, and netcdf format files are always read with native drivers. } More additional arguments, that can be used for netCDF files only: rll{ xvar character. The x variable (e.g. 'longitude' or 'x') yvar character. The y variable (e.g. 'latitude' or 'y') zvar character. The z variable (e.g. 'altitide' or 'precipitation') time integer > 0. The 'time' variable (if there are any) (default=NA) } If x is a character value, it should be a filename of a file that the raster package can read. Supported file types are the 'native' raster package format and those that can be read via rgdal. See readGDAL help for supported file types. For netCDF files, the function will try to guess values for the xvar, yvar, and zvar if they are not supplied. For this function to work, the RNetCDF package needs to have been installed. Note that currently, a raster object made from a netCDF file will load all values from file at creation (unlike other raster objects), and that read* functions are therefore not applicable. netCDF data can only be read if the data are stored in 2 (x and y) or 3 (x, y, and time) dimensions. If the data has three dimensions, a value must be supplied for time 2) Create a RasterLayer object from scratch raster(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, projs="+proj=longlat +datum=WGS84") rll{ nrows number of rows ncols number of columns xmn minimum x coordinate (left border) xmx maximum x coordinate (right border) ymn minimum y coordinate (bottom border) ymx maximum y coordinate (top border) projs Character. PROJ4 type description of a Coordinate Reference System (map projection) } (item x is 'missing' in this case) 3) Create a RasterLayer object from an Extent object raster(x, nrows=10, ncols=10, projs=NA) rll{ x Extent object nrows number of rows ncols number of columns projs PROJ4 type description of a map projection } 4) Create a RasterLayer object from a RasterLayer object. This copies the parameters of a RasterLayer object to a new RasterLayer, but does not copy the filename nor the data associated with the original Raster object. raster(x, filename="", values=NULL) rll{ x a Raster* object filename new filename for new RasterLayer values If not NULL it is used to set values to new object. Either pass a vector of length == ncells(x), or a single number } 5) Create a RasterLayer object from a RasterStack or RasterBrick object. raster(x, index=0) rll{ x a RasterStack, SpatialPixels* or SpatialGrid* object layer Integer. The layer from which to copy values to the new RasterLayer, if layer > 0. } 6) Create a RasterLayer object from a SpatialPixels* or SpatialGrid* object. raster(x, index=0) rll{ x a RasterStack, SpatialPixels* or SpatialGrid* object layer Integer. the layer from which to copy values to the new RasterLayer, if layer > 0. } 7) Create a RasterLayer object from a matrix. The default extent is set to be between 0 and 1 in the x and y direction but can be changed at creation of the RasterLayer object or later. You can also provide a projection. function(x, xmn=0, xmx=1, ymn=0, ymx=1, projs=NA) rll{ x a matrix xmn minimum x coordinate (left border) xmx maximum x coordinate (right border) ymn minimum y coordinate (bottom border) ymx maximum y coordinate (top border) projs PROJ4 type description of a map projection }

Details

New RasterLayer objects normally have no values in memory. If they are created from a file on disk, you can access values with getValues), xyValues and related functions. You can assign new values with setValues and with replacement.

See Also

stack, getValues

Examples

Run this code
#from file
r <- raster(system.file("external/test.grd", package="raster"))
logo <- raster(system.file("external/rlogo.grd", package="raster"), values=TRUE) 

#from scratch
r1 <- raster(nrows=108, ncols=21, xmn=0, xmx=10)

#from an Extent object
e <- extent(r)
r2 <- raster(e)

#from another Raster* object
r3 <- raster(r)
s <- stack(r, r)
r3 <- raster(s)

Run the code above in your browser using DataCamp Workspace