From e59ceb509bba9f29aa5b9b7c6d4d31c4db767ada Mon Sep 17 00:00:00 2001 From: Konstantin Date: Tue, 23 Jan 2024 08:47:18 +0100 Subject: [PATCH] fixed a bug in gg.R causing tests to fail, switched back to extract --- R/ggR.R | 5 +++-- R/ggRGB.R | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/R/ggR.R b/R/ggR.R index d2049c2..75d3688 100644 --- a/R/ggR.R +++ b/R/ggR.R @@ -112,19 +112,20 @@ ggR <- function(img, layer = 1, maxpixels = 500000, alpha = 1, hue = 1, sat = 0 ex <- ext(img) - xfort <- spatSample(img[[layer]], maxpixels, ext = ex, method = "regular", as.raster = TRUE) + xfort <- spatSample(img[[layer]], maxpixels, ext = ex, method = "regular", as.raster = TRUE, na.rm = TRUE) ex <- as.vector(ext(xfort)) dimImg <- dim(xfort) df <- lapply(names(xfort), function(layer) { - df <- data.frame(as.data.frame(xfort[[layer]], xy = TRUE), + df <- data.frame(extract(xfort[[layer]], seq_along(values(xfort[[layer]])), xy = TRUE), layerName = factor(layer, levels = names(xfort))) colnames(df) <- c("x", "y", "value", "layerName") df }) df <- do.call(rbind, df) + if(forceCat & !is.factor(df$value)) df$value <- as.factor(df$value) if(is.character(df$value)) df$value <- factor(df$value) diff --git a/R/ggRGB.R b/R/ggRGB.R index 0351575..0578e68 100644 --- a/R/ggRGB.R +++ b/R/ggRGB.R @@ -65,6 +65,8 @@ ggRGB <- function(img, r = 3, g = 2, b = 1, scale, maxpixels = 500000, stretch = alpha = 1, coord_equal = TRUE, geom_raster = FALSE, nullValue = 0) { ## TODO: handle single value rasters (e.g. masks) + + img <- .toTerra(img) # RGB processing originally forked from raster::plotRGB (Author: Robert J. Hijmans) GPL3 verbose <- getOption("RStoolbox.verbose") @@ -73,7 +75,7 @@ ggRGB <- function(img, r = 3, g = 2, b = 1, scale, maxpixels = 500000, stretch = rgb <- unlist(.numBand(raster=img,r,g,b)) nComps <- length(rgb) - rr <- spatSample(img[[rgb]], maxpixels, ext = .toTerra(ext), method = "regular", as.raster = TRUE) + rr <- spatSample(img[[rgb]], maxpixels, ext = ext(img), method = "regular", as.raster = TRUE) ex <- as.vector(ext(rr)) RGB <- as.data.frame(rr, xy = TRUE)