View source: R/matrix2raster.r
matrix2raster | R Documentation |
matrix2raster
Converts a matrix to a RasterLayer or arrays to a RasterStack-object.
matrix2raster(z,x,y,layer,proj="+proj=longlat")
z |
matrix or array to be converted. |
x |
optional x-coordinates giving the horizontal range of the raster layer, its size does not need to coincide with ncol(z)! |
y |
optional y-coordinates giving the verical range of the raster layer, its size does not need to coincide with nrow(z)! |
layer |
layer to be selected (only valid if z is an array). |
proj |
optional argument, setting the coordinate reference system (CRS) of a Raster* object (default is |
Robert K. Bauer
## Example 1: convert a matrix
m <- matrix(3,2,2)
matrix2raster(m)
## Example 2: convert an array
a <- array(3,dim=c(2,2,2))
matrix2raster(a)
matrix2raster(a,layer=1)
## Example 3: convert '.nc'-file to raster-object manually
owd <- getwd()
path <- system.file("test_files", package="oceanmap")
ncfile <- Sys.glob(paste0(path,'/herring*.nc')) # load sample-'.nc'-files
library('ncdf4')
library('raster')
nc <- nc_open(ncfile) # open netcdf file
z <- ncvar_get(nc,'Conc')[,,1]
lon <- as.vector(ncvar_get(nc,'lon')) # fillvalues are automatically replaced by NA
lat <- as.vector(ncvar_get(nc,'lat')) # fillvalues are automatically replaced by NA
matrix2raster(z,x=lon,y=lat)
## Example 4: convert '.nc'-file to raster-object using nc2raster
nc2raster(ncfile,varname='Conc',layer=1:4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.