diff --git a/R/rasterCVA.R b/R/rasterCVA.R index 9b38787..c868812 100644 --- a/R/rasterCVA.R +++ b/R/rasterCVA.R @@ -21,15 +21,13 @@ #' Returns a SpatRaster with two layers: change vector angle and change vector magnitude #' @export #' @examples -#' \dontrun{ #' library(terra) #' pca <- rasterPCA(lsat)$map -#' -#' ## Do change vector analysis +#' +#' ## Do change vector analysis #' cva <- rasterCVA(pca[[1:2]], pca[[3:4]]) #' cva #' plot(cva) -#' } rasterCVA <- function(x, y, tmf = NULL, nct = NULL, ...) { x <- .toTerra(x) y <- .toTerra(y) @@ -42,7 +40,7 @@ rasterCVA <- function(x, y, tmf = NULL, nct = NULL, ...) { if(!is.null(tmf) && !is.null(nct)) { stop("'tmf' and 'nct' are exclusive options, cannot use both.", call. = FALSE) } - + if(!is.null(tmf)) { maxMag <- sqrt(sum((as.numeric(t(terra::global(x, "max", na.rm=T))) - as.numeric(t(terra::global(y, "max", na.rm=T))) )^2))*2 medianBuckets <- seq(1e-10, maxMag, length.out = 2e5) @@ -98,10 +96,28 @@ rasterCVA <- function(x, y, tmf = NULL, nct = NULL, ...) { nct <- tmf * medianEstimate rm(RStoolbox_rasterCVAEnv) } - out <- do.call(terra::clamp, c(list(x = out, lower=nct), ellips)) + out <- terra::clamp(out, lower = nct, ... = ellips) names(out) <- c("angle", "magnitude") } return(out) } + +test <- function(){ + devtools::load_all() + + r <- rast(val = 0, ncol = 2, nrow = 10) + r1 <- r2 <- c(r, r) + s <- 4 + x <- c(0,s,s,s,0,-s,-s,-s, NA, 0) + y <- c(s,s,0,-s,-s,-s,0,s, 0, NA) + r2[[1]][] <- c(x, x + sign(x)*2) + r2[[2]][] <- c(y, y + sign(y)*2) + + expectedDf <- as.matrix(data.frame(angle = c(0,45,90,135,180,225,270,315,NA,NA), + magnitude = c(rep(c(s, sqrt(2*s^2)), 4), NA,NA, rep(c((2+s), sqrt(2*(2+s)^2)), 4), NA,NA))) + + rasterCVA(r1,r2, tmf = 0) + +} diff --git a/R/rasterPCA.R b/R/rasterPCA.R index fc03ee1..0ddbe1d 100644 --- a/R/rasterPCA.R +++ b/R/rasterPCA.R @@ -24,7 +24,6 @@ #' @return Returns a named list containing the PCA model object ($model) and a SpatRaster with the principal component layers ($object). #' @export #' @examples -#' \dontrun{ #' library(ggplot2) #' library(reshape2) #' ggRGB(rlogo, 1,2,3) @@ -43,7 +42,6 @@ #' plots <- lapply(1:3, function(x) ggR(rpc$map, x, geom_raster = TRUE)) #' grid.arrange(plots[[1]],plots[[2]], plots[[3]], ncol=2) #' } -#' } rasterPCA <- function(img, nSamples = NULL, nComp = nlyr(img), spca = FALSE, maskCheck = TRUE, ...){ img <- .toTerra(img) diff --git a/man/rasterCVA.Rd b/man/rasterCVA.Rd index e3dbdb3..7ef16da 100644 --- a/man/rasterCVA.Rd +++ b/man/rasterCVA.Rd @@ -33,13 +33,11 @@ For example for a given pixel in image A and B for the red and nir band the chan Angles are returned *in degree* beginning with 0 degrees pointing 'north', i.e. the y-axis, i.e. the second band. } \examples{ -\dontrun{ library(terra) pca <- rasterPCA(lsat)$map -## Do change vector analysis +## Do change vector analysis cva <- rasterCVA(pca[[1:2]], pca[[3:4]]) cva plot(cva) } -} diff --git a/man/rasterPCA.Rd b/man/rasterPCA.Rd index 051c0e4..6e94307 100644 --- a/man/rasterPCA.Rd +++ b/man/rasterPCA.Rd @@ -47,7 +47,6 @@ Standardised PCA (SPCA) can be useful if imagery or bands of different dynamic r has the same effect as using normalised bands of unit variance. } \examples{ -\dontrun{ library(ggplot2) library(reshape2) ggRGB(rlogo, 1,2,3) @@ -67,4 +66,3 @@ if(require(gridExtra)){ grid.arrange(plots[[1]],plots[[2]], plots[[3]], ncol=2) } } -}