rasterize {terra} | R Documentation |
Rasterize vector data
Description
Transfer values associated with the geometries of vector data to a raster
Usage
## S4 method for signature 'SpatVector,SpatRaster'
rasterize(x, y, field="", fun, ..., background=NA, touches=FALSE, update=FALSE,
cover=FALSE, by=NULL, filename="", overwrite=FALSE, wopt=list())
## S4 method for signature 'matrix,SpatRaster'
rasterize(x, y, values=1, fun, ..., background=NA, update=FALSE,
by=NULL, filename="", overwrite=FALSE, wopt=list())
Arguments
x |
SpatVector or a two-column matrix (point coordinates) |
y |
SpatRaster |
field |
character or numeric. If |
values |
typically a numeric vector of length |
fun |
summarizing function for when there are multiple geometries in one cell. For lines and polygons you can only use |
... |
additional arguments passed to |
background |
numeric. Value to put in the cells that are not covered by any of the features of |
touches |
logical. If |
update |
logical. If |
cover |
logical. If |
by |
character or numeric value(s) to split |
filename |
character. Output filename |
overwrite |
logical. If |
wopt |
list with additional arguments for writing files as in |
Value
SpatRaster
See Also
rasterizeGeom
, rasterizeWin
, mask
Examples
r <- rast(xmin=0, ncols=18, nrows=18)
# generate points
set.seed(1)
p <- spatSample(r, 1000, xy=TRUE, replace=TRUE)
# rasterize points as a matrix
x <- rasterize(p, r, fun=sum)
y <- rasterize(p, r, value=1:nrow(p), fun=max)
# rasterize points as a SpatVector
pv <- vect(p)
xv <- rasterize(pv, r, fun=sum)
# Polygons
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
r <- rast(v, ncols=75, nrows=100)
z <- rasterize(v, r, "NAME_2")
plot(z)
lines(v)