Commit f1a88577 authored by Monnet Jean-Matthieu's avatar Monnet Jean-Matthieu
Browse files

Added "start" parameter in raster_metrics to align on predefined grid

parent b8b29627
No related merge requests found
Showing with 12 additions and 3 deletions
+12 -3
Package: lidaRtRee Package: lidaRtRee
Type: Package Type: Package
Version: 4.0.5 Version: 4.0.6
Title: Forest Analysis with Airborne Laser Scanning (LiDAR) Data Title: Forest Analysis with Airborne Laser Scanning (LiDAR) Data
Date: 2023-04-05 Date: 2023-04-05
Authors@R: c( Authors@R: c(
......
...@@ -17,12 +17,15 @@ ...@@ -17,12 +17,15 @@
#' @param fun function. Function to compute metrics in each aggregated cell from #' @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 #' the values contained in the initial raster (use x$layer to access raster
#' values) / data.frame (use x$colum_name to access values) #' 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" #' @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, #' @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 #' and the values computed with the user-specified function, or a SpatRaster object
#' @examples #' @examples
#' data(chm_chablais3) #' data(chm_chablais3)
#' chm_chablais3 <- terra::rast(chm_chablais3) #' chm_chablais3 <- terra::rast(chm_chablais3)
#' # replace NA with zeros
#' chm_chablais3[is.na(chm_chablais3)] <- 0
#' #'
#' # raster metrics from raster #' # raster metrics from raster
#' metrics1 <- raster_metrics(chm_chablais3, res = 10) #' metrics1 <- raster_metrics(chm_chablais3, res = 10)
...@@ -55,6 +58,7 @@ raster_metrics <- ...@@ -55,6 +58,7 @@ raster_metrics <-
fun = function(x) { fun = function(x) {
data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3])) data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3]))
}, },
start = c(0, 0),
output = "raster") { output = "raster") {
if (inherits(r, "SpatRaster")) { if (inherits(r, "SpatRaster")) {
# convert to data.frame # convert to data.frame
...@@ -76,8 +80,8 @@ raster_metrics <- ...@@ -76,8 +80,8 @@ raster_metrics <-
# return NULL if empty # return NULL if empty
if (nrow(st) == 0) return(NULL) if (nrow(st) == 0) return(NULL)
# compute coordinates of new cell center at metrics resolution # compute coordinates of new cell center at metrics resolution
dummy <- data.frame(X = round((st[, 1] - 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] - res / 2) / res) * res + res / 2) Y = round((st[, 2] - start[2]- res / 2) / res) * res + start[2] + res / 2)
# compute metrics by grouping factor # compute metrics by grouping factor
dummy <- lapply(split(st, list(dummy$X, dummy$Y), sep = "_"), FUN = fun) dummy <- lapply(split(st, list(dummy$X, dummy$Y), sep = "_"), FUN = fun)
# convert to data.frame # convert to data.frame
......
...@@ -11,6 +11,7 @@ raster_metrics( ...@@ -11,6 +11,7 @@ raster_metrics(
fun = function(x) { fun = function(x) {
data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3])) data.frame(mean = mean(x[, 3]), sd = stats::sd(x[, 3]))
}, },
start = c(0, 0),
output = "raster" output = "raster"
) )
} }
...@@ -24,6 +25,8 @@ of r resolution if a raster is provided} ...@@ -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 the values contained in the initial raster (use x$layer to access raster
values) / data.frame (use x$colum_name to access values)} 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"} \item{output}{string. indicates the class of output object "raster" for a SpatRaster or "data.frame"}
} }
\value{ \value{
...@@ -39,6 +42,8 @@ as a data.frame with the XY coordinates of the larger cells, or as SpatRaster. ...@@ -39,6 +42,8 @@ as a data.frame with the XY coordinates of the larger cells, or as SpatRaster.
\examples{ \examples{
data(chm_chablais3) data(chm_chablais3)
chm_chablais3 <- terra::rast(chm_chablais3) chm_chablais3 <- terra::rast(chm_chablais3)
# replace NA with zeros
chm_chablais3[is.na(chm_chablais3)] <- 0
# raster metrics from raster # raster metrics from raster
metrics1 <- raster_metrics(chm_chablais3, res = 10) metrics1 <- raster_metrics(chm_chablais3, res = 10)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment