rasterize: Rasterize vector data

rasterizeR 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, sum=FALSE, cover=FALSE, filename="", overwrite=FALSE, wopt=list())

## S4 method for signature 'matrix,SpatRaster'
rasterize(x, y, values=1, fun, ..., background=NA, 
	update=FALSE, filename="", overwrite=FALSE, wopt=list())

Arguments

x

SpatVector or a two-column matrix (point coordinates)

y

SpatRaster

field

character or numeric. If field is a character, it should a variable name in x. If field is numeric it typically is a single number or a vector of length nrow(x). The values are recycled to nrow(x)

values

typically a numeric vector of length 1 or nrow(x). If the length is below nrow(x) the values will be recycled to nrow(x). Only used when x is a matrix. Can also be a matrix or data.frame

fun

function, summarizing function that returns a single number; for when there are multiple points in one cell. For example mean, length (to get a count), min or max. Only used if x consists of points

...

additional arguments passed to fun if x has point geometries

background

numeric. Value to put in the cells that are not covered by any of the features of x. Default is NA

touches

logical. If TRUE, all cells touched by lines or polygons are affected, not just those on the line render path, or whose center point is within the polygon. If touches=TRUE, add cannot be TRUE

update

logical. If TRUE, the values of the input SpatRaster are updated

sum

logical. If TRUE, the values of overlapping geometries are summed instead of replaced; and background is set to zero. Only used if x does not consists of points

cover

logical. If TRUE and the geometry of x is polygons, the fraction of a cell that is covered by the polygons is returned. This is estimated by determining presence/absence of the polygon in at least 100 sub-cells (more of there are very few cells)

filename

character. Output filename

overwrite

logical. If TRUE, filename is overwritten

wopt

list with additional arguments for writing files as in writeRaster

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)

terra documentation built on Jan. 17, 2023, 5:18 p.m.