raster_metrics {lidaRtRee} | R Documentation |
Computes metrics by aggregating a raster at lower resolution or summarizing attributes based on XY locations
Description
Computes statistics by aggregating a raster at lower resolution. Aggregation groups are larger cells, new values are computed by applying a user-specified function to original cells contained in the larger cells. Results are provided as a data.frame with the XY coordinates of the larger cells, or as SpatRaster.
Usage
raster_metrics(
r,
res = 20,
fun = function(x) {
data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3]))
},
output = "raster"
)
Arguments
r |
SpatRaster object, data.frame with xy coordinates in two first columns, or POINT |
res |
numeric. Resolution of the aggregation raster, should be a multiple of r resolution if a raster is provided |
fun |
function. Function to compute metrics in each aggregated cell from the values contained in the initial raster (use x$layer to access raster values) / data.frame (use x$colum_name to access values) |
output |
string. indicates the class of output object "raster" for a SpatRaster or "data.frame" |
Value
a data.frame with the XY center coordinates of the aggregated cells, and the values computed with the user-specified function, or a SpatRaster object
Examples
data(chm_chablais3)
chm_chablais3 <- terra::rast(chm_chablais3)
# raster metrics from raster
metrics1 <- raster_metrics(chm_chablais3, res = 10)
metrics1
# raster metrics from data.frame
n <- 1000
df <- data.frame(
x = runif(n, 0, 100), y = runif(n, 0, 100), z1 = runif(n, 0, 1),
z2 = runif(n, 10, 20)
)
# compute raster metrics
metrics2 <- raster_metrics(df,
res = 10,
fun = function(x) {
data.frame(max.z = max(x$z1), max.sum = max(x$z1 + x$z2))
},
output = "data.frame"
)
summary(metrics2)
# display raster metrics
terra::plot(metrics1)
# display data.frame metrics
terra::plot(terra::rast(metrics2, type = "xyz"))