Learn R Programming

raster (version 1.9-13)

raster: Create a RasterLayer object

Description

Methods to create a RasterLayer object. RasterLayer objects can be created from scratch, a filename, an Extent object, a matrix, an 'image' object, or from a Raster*, Spatial*, asc, kasc (adehabitat*), grf (geoR) or kde 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. For an overview of the functions in the raster package have a look here: raster-package.

Usage

raster(x, ...)

Arguments

x
Filename (character), Extent, Raster*, SpatialPixels*, SpatialGrid* object, matrix, or missing
...
Additional arguments, see below

Value

  • RasterLayer object

Methods

1) Create a RasterLayer object from a file raster(x, band=1, ...) rll{ x character. Name of raster file 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, SAGA, and Arc ASCII files is done with native (raster package) drivers, rather then via rgdal. 'raster' and netcdf format files are always read with native drivers. } For netCDF files (CF convention): rll{ varname character. The variable name (e.g. 'altitude' or 'precipitation'. If not supplied and the file has multiple variables are a guess will be made (and reported)) band integer > 0. The 'band' (layer) number of the file. E.g., the 'time' variable (if there are any) (default=NA) } To read netCDF files, the ncdf package needs to be available. 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. 2) Create a RasterLayer object from scratch raster(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, crs, ext) 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) crs Character or object of class CRS. PROJ4 type description of a Coordinate Reference System (map projection). If this argument is missing, and the x coordinates are withing -360 .. 360 and the y coordinates are within -90 .. 90, "+proj=longlat +datum=WGS84" is used. ext object of Extent-class. If present, the arguments xmn, xmx, ymn and ynx are ignored } (item x is 'missing' in this case) 3) Create a RasterLayer object from an Extent-class object raster(x, nrows=10, ncols=10, crs=NA) rll{ x Extent object nrows number of rows ncols number of columns crs PROJ4 type description of a map projection } 4) Create a RasterLayer object from a Raster* object. This copies the parameters of a Raster* object to a new RasterLayer, but does not copy the filename nor the cell values associated with the original Raster* object. raster(x) rll{ x a Raster* object } 5) Create a RasterLayer object from a RasterStack or RasterBrick object. raster(x, layer=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 RasterStack, RasterBrick, SpatialPixels* or SpatialGrid* object. raster(x, layer=0) rll{ x RasterStack, RasterBrick, 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, crs=NA) rll{ x 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) crs PROJ4 type description of a map projection (optional) } 8) Create a RasterLayer object from an 'image' object (a list with vectors x and y and matrix z), or from an asc, kasc, or kde object raster(img, crs) rll{ x RasterStack, SpatialPixels* or SpatialGrid* object crs PROJ4 type description of a map projection (optional) }

Details

A new RasterLayer object normally has no cell-values in memory. If it is created from a file on disk, you can access cell-values with getValues, extract and related functions. You can assign new values with setValues and with replacement.

See Also

stack, brick

Examples

Run this code
# Create a RasterLayer object from a file
#   N.B.: For your own files, omit the 'system.file' and 'package="raster"' bits
#   these are just to get the path to files installed with the package

f <- system.file("external/test.grd", package="raster")
f
r <- raster(f)

logo <- raster(system.file("external/rlogo.grd", package="raster")) 


#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, r)
r4 <- raster(s)
r5 <- raster(s, 3)

Run the code above in your browser using DataLab