From f1a885770465bbfb227d0a4a20bc4cf3ed04c326 Mon Sep 17 00:00:00 2001 From: "jean-matthieu.monnet" <jean-matthieu.monnet@inrae.fr> Date: Fri, 2 Jun 2023 08:11:29 +0200 Subject: [PATCH] Added "start" parameter in raster_metrics to align on predefined grid --- DESCRIPTION | 2 +- R/raster_metrics.R | 8 ++++++-- man/raster_metrics.Rd | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cc15060..4112828 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: lidaRtRee Type: Package -Version: 4.0.5 +Version: 4.0.6 Title: Forest Analysis with Airborne Laser Scanning (LiDAR) Data Date: 2023-04-05 Authors@R: c( diff --git a/R/raster_metrics.R b/R/raster_metrics.R index 63c0be3..9ff411c 100755 --- a/R/raster_metrics.R +++ b/R/raster_metrics.R @@ -17,12 +17,15 @@ #' @param 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) +#' @param start vector of x and y coordinates for the reference raster. Default is (0,0) meaning that the grid aligns on (0,0). #' @param output string. indicates the class of output object "raster" for a SpatRaster or "data.frame" #' @return 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) +#' # replace NA with zeros +#' chm_chablais3[is.na(chm_chablais3)] <- 0 #' #' # raster metrics from raster #' metrics1 <- raster_metrics(chm_chablais3, res = 10) @@ -55,6 +58,7 @@ raster_metrics <- fun = function(x) { data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3])) }, + start = c(0, 0), output = "raster") { if (inherits(r, "SpatRaster")) { # convert to data.frame @@ -76,8 +80,8 @@ raster_metrics <- # return NULL if empty if (nrow(st) == 0) return(NULL) # compute coordinates of new cell center at metrics resolution - dummy <- data.frame(X = round((st[, 1] - res / 2) / res) * res + res / 2, - Y = round((st[, 2] - res / 2) / res) * res + res / 2) + dummy <- data.frame(X = round((st[, 1] - start[1] - res / 2) / res) * res + start[1] + res / 2, + Y = round((st[, 2] - start[2]- res / 2) / res) * res + start[2] + res / 2) # compute metrics by grouping factor dummy <- lapply(split(st, list(dummy$X, dummy$Y), sep = "_"), FUN = fun) # convert to data.frame diff --git a/man/raster_metrics.Rd b/man/raster_metrics.Rd index 5d9afdd..ca87775 100755 --- a/man/raster_metrics.Rd +++ b/man/raster_metrics.Rd @@ -11,6 +11,7 @@ raster_metrics( fun = function(x) { data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3])) }, + start = c(0, 0), output = "raster" ) } @@ -24,6 +25,8 @@ of r resolution if a raster is provided} the values contained in the initial raster (use x$layer to access raster values) / data.frame (use x$colum_name to access values)} +\item{start}{vector of x and y coordinates for the reference raster. Default is (0,0) meaning that the grid aligns on (0,0).} + \item{output}{string. indicates the class of output object "raster" for a SpatRaster or "data.frame"} } \value{ @@ -39,6 +42,8 @@ as a data.frame with the XY coordinates of the larger cells, or as SpatRaster. \examples{ data(chm_chablais3) chm_chablais3 <- terra::rast(chm_chablais3) +# replace NA with zeros +chm_chablais3[is.na(chm_chablais3)] <- 0 # raster metrics from raster metrics1 <- raster_metrics(chm_chablais3, res = 10) -- GitLab