Skip to content

Commit

Permalink
fixed a bug in gg.R causing tests to fail, switched back to extract
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstiDE committed Jan 23, 2024
1 parent 85996cd commit e59ceb5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 3 additions & 2 deletions R/ggR.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion R/ggRGB.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit e59ceb5

Please sign in to comment.