| rasterize | R Documentation |
Folds a larger-than-RAM stream of points into a fixed raster grid one batch
at a time. The grid (template) is held resident in memory while the points
flow past the engine, so peak memory is the grid plus one batch regardless of
how many points there are – the streaming counterpart to running
terra::rasterize() on a point set that has to fit in RAM. Each point's
coordinate is mapped to its grid cell through the raster geotransform and the
per-cell value is accumulated in C.
rasterize(
x,
template = NULL,
field = NULL,
fun = c("count", "sum", "mean", "min", "max"),
extent = NULL,
res = NULL,
dims = NULL,
coords = c("x", "y"),
geom = NULL,
crs = NA,
background = NA_real_,
path = NULL,
dtype = "f32"
)
x |
A |
template |
Optional grid to borrow geometry and CRS from: a
|
field |
Name of a numeric column to aggregate. Required for every |
fun |
Reduction over the points in each cell: one of |
extent |
Numeric |
res |
Cell size: a single number for square cells, or |
dims |
Grid shape |
coords |
Length-2 character vector naming the x and y coordinate
columns. Default |
geom |
Name of a hex-WKB point-geometry column to rasterize instead of coordinate columns. Requires sf. |
crs |
Coordinate reference system recorded on the output, in any form
|
background |
Value for cells that receive no point. Default |
path |
Optional output path. When given, the grid is written to a |
dtype |
Storage dtype for the |
The reduction fun is a monoid over the points falling in each cell:
"count" tallies points (no field needed); "sum", "mean", "min",
"max" aggregate a numeric field. Cells that receive no point take the
background value (NA by default). This is the monoid fold tier of the
spatial toolbox: bounded memory, a single streaming pass, no spill.
Points arrive either as two numeric coordinate columns (coords, the default
and fully sf-free path – the headline larger-than-RAM case) or decoded
from a hex-WKB point-geometry column (geom, which needs sf). Geometry
input is expected to be points (one coordinate per row); line and polygon
coverage rasterization is out of scope here.
When path is NULL, a numeric matrix with nrow grid rows
(row 1 northmost) and ncol grid columns, carrying gt, extent, res,
crs, and fun attributes. When path is given, the written
vectra_raster handle (invisibly).
vec_write_raster() and vec_to_tiff() for raster output,
spatial_join() to instead tag points with polygon attributes.
set.seed(1)
n <- 1e4
pts <- data.frame(x = runif(n, 0, 10), y = runif(n, 0, 10), z = rnorm(n))
f <- tempfile(fileext = ".vtr")
write_vtr(pts, f)
# Point density on a 10x10 grid, streamed: the grid is resident, the
# points are not.
counts <- tbl(f) |> rasterize(extent = c(0, 0, 10, 10), dims = c(10, 10))
counts
# Mean of z per cell.
zmean <- tbl(f) |>
rasterize(extent = c(0, 0, 10, 10), dims = c(10, 10),
field = "z", fun = "mean")
unlink(f)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.