Skip to content

Commit

Permalink
trying to fix dontrun rasterCVA and rasterPCA
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstiDE committed Jan 16, 2024
1 parent 1f259df commit 0c37a95
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
28 changes: 22 additions & 6 deletions R/rasterCVA.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

}
2 changes: 0 additions & 2 deletions R/rasterPCA.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
4 changes: 1 addition & 3 deletions man/rasterCVA.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions man/rasterPCA.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c37a95

Please sign in to comment.