Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

raster

Create a RasterLayer object


Description

Methods to create a RasterLayer object. RasterLayer objects can be created from scratch, a file, an Extent object, a matrix, an 'image' object, or from a Raster*, Spatial*, im (spatstat) 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 in (RAM) memory, it only has the parameters that describe the RasterLayer. You can access cell-values with getValues, extract and related functions. You can assign new values with setValues and with replacement.

For an overview of the functions in the raster package have a look here: raster-package.

Usage

## S4 method for signature 'character'
raster(x, band=1, ...)

## S4 method for signature 'RasterLayer'
raster(x) 

## S4 method for signature 'RasterStack'
raster(x, layer=0) 

## S4 method for signature 'RasterBrick'
raster(x, layer=0) 

## S4 method for signature 'missing'
raster(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, 
		crs, ext, resolution, vals=NULL)

## S4 method for signature 'Extent'
raster(x, nrows=10, ncols=10, crs="", ...)

## S4 method for signature 'matrix'
raster(x, xmn=0, xmx=1, ymn=0, ymx=1, crs="", template=NULL)

## S4 method for signature 'Spatial'
raster(x, origin, ...) 

## S4 method for signature 'SpatialGrid'
raster(x, layer=1, values=TRUE)

## S4 method for signature 'SpatialPixels'
raster(x, layer=1, values=TRUE)

## S4 method for signature 'sf'
raster(x, origin, ...)

Arguments

x

filename (character), Extent, Raster*, sf, SpatialPixels*, SpatialGrid*, object, 'image', matrix, im, or missing. Supported file types are the 'native' raster package format and those that can be read via rgdal (see readGDAL

band

integer. The layer to use in a multi-layer file

...

Additional arguments, see Details

layer

integer. The layer (variable) to use in a multi-layer file, or the layer to extract from a RasterStack/Brick or SpatialPixelsDataFrame or SpatialGridDataFrame. An empty RasterLayer (no associated values) is returned if layer=0

values

logical. If TRUE, the cell values of 'x' are copied to the RasterLayer object that is returned

nrows

integer > 0. Number of rows

ncols

integer > 0. 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)

ext

object of class Extent. If present, the arguments xmn, xmx, ymn and ynx are ignored

crs

character or object of class CRS. PROJ.4 type description of a Coordinate Reference System (map projection). If this argument is missing, and the x coordinates are within -360 .. 360 and the y coordinates are within -90 .. 90, "+proj=longlat +datum=WGS84" is used. Also see under Details if x is a character (filename)

resolution

numeric vector of length 1 or 2 to set the resolution (see res). If this argument is used, arguments ncols and nrows are ignored

vals

optional. Values for the new RasterLayer. Accepted formats are as for setValues

origin

minimum y coordinate (bottom border)

template

Raster* or Extent object used to set the extent (and CRS in case of a Raster* object). If not NULL, arguments xmn, xmx, ymn, ymx and crs (unless template is an Extent object) are ignored

Details

If x is a filename, the following additional variables are recognized:

sub: positive integer. Subdataset number for a file with subdatasets

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.

RAT: logical. The default is TRUE, in which case a raster attribute table is created for files that have one

offset: integer. To indicate the number of header rows on non-standard ascii files (rarely useful; use with caution)

crs: character. PROJ.4 string to set the CRS. Ignored when the file provides a CRS description that can be interpreted.

If x represents a NetCDF file, the following additional variable is recognized:

varname: character. The variable name, such as 'tasmax' or 'pr'. If not supplied and the file has multiple variables are a guess will be made (and reported)

lvar: integer > 0 (default=3). To select the 'level variable' (3rd dimension variable) to use, if the file has 4 dimensions (e.g. depth instead of time)

level: integer > 0 (default=1). To select the 'level' (4th dimension variable) to use, if the file has 4 dimensions, e.g. to create a RasterBrick of weather over time at a certain height.

To use NetCDF files the ncdf4 package needs to be available. It is assumed that these files follow, or are compatible with, the CF-1 convention (The GMT format may also work). If the ncdf file does not have a standard extension (which is used to recognize the file format), you can use argument ncdf=TRUE to indicate the format.

If x is a Spatial or an Extent object, additional arguments are for the method with signature 'missing'

Value

RasterLayer

See Also

Examples

# 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)

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.