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

rasterFromXYZ

Create a Raster* object from x, y, z values


Description

Create a Raster* object from x, y and z values. x and y represent spatial coordinates and must be on a regular grid. If the resolution is not supplied, it is assumed to be the minimum distance between x and y coordinates, but a resolution of up to 10 times smaller is evaluated if a regular grid can otherwise not be created. z values can be single or multiple columns (variables) If the exact properties of the RasterLayer are known beforehand, it may be preferable to simply create a new RasterLayer with the raster function instead, compute cell numbers and assign the values with these (see example below).

Usage

rasterFromXYZ(xyz, res=c(NA,NA), crs="", digits=5)

Arguments

xyz

matrix or data.frame with at least three columns: x and y coordinates, and values (z). There may be several 'z' variables (columns)

res

numeric. The x and y cell resolution (optional)

crs

CRS object or a character string describing a projection and datum in PROJ.4 format

digits

numeric, indicating the requested precision for detecting whether points are on a regular grid (a low number of digits is a low precision)

Value

RasterLayer or RasterBrick

See Also

See rasterize for points that are not on a regular grid

Examples

r <- raster(nrow=5, ncol=5, xmn=0, xmx=10, ymn=0, ymx=10, crs="")
set.seed(1)
values(r) <- sample(1:25)
r[r < 15] <- NA
xyz <- rasterToPoints(r)

rst <- rasterFromXYZ(xyz)

# equivalent to:
rr <- raster(nrow=5, ncol=5, xmn=0, xmx=10, ymn=0, ymx=10)
cells <- cellFromXY(rr, xyz[,1:2])
rr[cells] <- xyz[,3]

# multiple layers
xyzz <- cbind(xyz, a=1:nrow(xyz), b=nrow(xyz):1)
b <- rasterFromXYZ(xyzz)

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.