Skip to content

Commit

Permalink
colorize fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Nov 1, 2023
1 parent b7b8295 commit 9494a5e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: terra
Type: Package
Title: Spatial Data Analysis
Version: 1.7-58
Date: 2023-10-30
Version: 1.7-59
Date: 2023-11-01
Depends: R (>= 3.5.0)
Suggests: parallel, tinytest, ncdf4, sf (>= 0.9-8), deldir, XML, leaflet, htmlwidgets
LinkingTo: Rcpp
Expand Down
19 changes: 10 additions & 9 deletions R/RGB.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,19 @@ rgb2col <- function(x, value, stretch=NULL, grays=FALSE, NAzero=FALSE, filename=
}

v <- cbind(id=1:ncell(x), values(x))
v <- median_cut(stats::na.omit(v))
v <- terra:::median_cut(stats::na.omit(v))

a <- aggregate(v[,-c(1:2)], list(v[,1]), median)
if (n==3) {
a$cols <- grDevices::rgb(a[,2], a[,3], a[,4], maxColorValue=255)
} else {
a$cols <- grDevices::rgb(a[,2], a[,3], a[,4], a[,5], maxColorValue=255)
}
m <- merge(v[,1:2], a[, c(1,n+2)], by=1)
# if (n==3) {
# a$cols <- grDevices::rgb(a[,2], a[,3], a[,4], maxColorValue=255)
# } else {
# a$cols <- grDevices::rgb(a[,2], a[,3], a[,4], a[,5], maxColorValue=255)
# }
m <- merge(v[,1:2], a, by=1)
m[,1] <- m[,1] - 1
r <- rast(x, 1)
r[m$id] <- m$group - 1
coltab(r) <- a$cols
r[m$id] <- m$group
coltab(r) <- m[,-2]
if (filename != "") {
r <- writeRaster(r, filename, overwrite, ...)
}
Expand Down

0 comments on commit 9494a5e

Please sign in to comment.