Skip to content

Commit

Permalink
fixes #1060
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Mar 9, 2023
1 parent d4d5c44 commit ff083d4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
19 changes: 12 additions & 7 deletions R/RGB.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@


setMethod ("has.RGB" , "SpatRaster",
function(x) {
x@pnt$rgb
function(x, strict=TRUE) {
if (strict) {
x@pnt$rgbtype == "rgb"
} else {
x@pnt$rgbtype != ""
}
}
)


setMethod("set.RGB", signature(x="SpatRaster"),
function(x, value) {
function(x, value=1:3, type="rgb") {
if (is.null(value[1]) || is.na(value[1])) {
x@pnt$removeRGB()
} else {
stopifnot(all(value %in% 1:nlyr(x)))
if (length(value) == 3) {
x@pnt$setRGB(value[1]-1, value[2]-1, value[3]-1, -99, "rgb")
x@pnt$setRGB(value[1]-1, value[2]-1, value[3]-1, -99, type)
} else if (length(value) == 4) {
x@pnt$setRGB(value[1]-1, value[2]-1, value[3]-1, value[4]-1, "rgb")
x@pnt$setRGB(value[1]-1, value[2]-1, value[3]-1, value[4]-1, type)
} else {
error("set.RGB", "value must have length 3 or 4")
}
Expand All @@ -30,9 +35,9 @@ setMethod("set.RGB", signature(x="SpatRaster"),
)

setMethod("RGB<-", signature(x="SpatRaster"),
function(x, value) {
function(x, type="rgb", value) {
x@pnt <- x@pnt$deepcopy()
set.RGB(x, value)
set.RGB(x, value, type)
x
}
)
Expand Down
13 changes: 10 additions & 3 deletions R/replace_layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ setMethod("$<-", "SpatRaster",

setReplaceMethod("[[", c("SpatRaster", "character"),
function(x, i, value) {
if (inherits(value, "SpatRaster")) {
if (inherits(value, "numeric")) {
r <- rast(x, nlyr=length(i))
value <- init(r, value)
names(value) <- i
} else if (inherits(value, "SpatRaster")) {
if (nlyr(value) != length(i)) {
error("`[[`", "length of names must be equal to the number of layers")
}
Expand All @@ -78,8 +82,11 @@ setReplaceMethod("[[", c("SpatRaster", "character"),

setReplaceMethod("[[", c("SpatRaster", "numeric"),
function(x, i, value) {
if (!inherits(value, "SpatRaster")) {
error("`[[<-`", "Expected a SpatRaster as replacement value")
if (inherits(value, "numeric")) {
r <- rast(x, nlyr=length(i))
value <- init(r, value)
} else if (!inherits(value, "SpatRaster")) {
error("`[[<-`", "Expected a SpatRaster or numeric as replacement value")
}
if (nlyr(value) < length(i)) {
if (nlyr(value) > 1) {
Expand Down
8 changes: 6 additions & 2 deletions R/replace_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ setReplaceMethod("[", c("SpatRaster", "ANY", "ANY", "ANY"),
k <- NA
}

if (m[1] & m[2]) {
if (all(m)) {
return(.replace_all(x, value))
}

Expand Down Expand Up @@ -324,10 +324,14 @@ setReplaceMethod("[", c("SpatRaster", "ANY", "ANY", "ANY"),
j <- make_replace_index(j, ncol(x), "j")
i <- cellFromRowColCombine(x, i, j)
}
} else { #if (!m[2]) {
} else if (!m[2]) {
#col
print(j)
print("?")
j <- make_replace_index(j, ncol(x), "j")
i <- cellFromRowColCombine(x, 1:nrow(x), j)
} else {
i <- 1:ncell(x)
}
return(.replace_cell(x, i, k, value))
}
Expand Down
8 changes: 5 additions & 3 deletions R/show.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,11 @@ setMethod ("show" , "SpatRaster",
cat("source :", sources[1], "\n")
}
}
rgb <- RGB(object)
if (!is.null(rgb)) {
cat(paste("colors", toupper(object@pnt$rgbtype), " :"), paste(rgb, collapse=", "), "\n")
rgbtype <- object@pnt$rgbtype
if (rgbtype != "") {
rdgb <- RGB(object)
if (is.null(rdgb)) rdgb <- 1:3
cat(paste("colors", toupper(object@pnt$rgbtype), " :"), paste(rdgb, collapse=", "), "\n")
}
hasct <- object@pnt$hasColors()
if (any(hasct)) {
Expand Down
6 changes: 4 additions & 2 deletions man/RGB.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
\title{Layers representing colors}

\description{
With \code{RGB} you can get or set the layers to be used as Red, Green and Blue when plotting a SpatRaster. Currently, a benefit of this is that \code{\link[terra]{plot}} will send the object to \code{\link{plotRGB}}
With \code{RGB} you can get or set the layers to be used as Red, Green and Blue when plotting a SpatRaster. Currently, a benefit of this is that \code{\link[terra]{plot}} will send the object to \code{\link{plotRGB}}. You can also associated the layers with another color space (HSV, HSI or HSL)

With \code{colorize} you can convert a three-layer RGB SpatRaster into other color spaces. You can also convert it into a single-layer SpatRaster with a color-table.
}

\usage{
\S4method{RGB}{SpatRaster}(x)

\S4method{RGB}{SpatRaster}(x)<-value
\S4method{RGB}{SpatRaster}(x, type="rgb")<-value

\S4method{colorize}{SpatRaster}(x, to="hsv", alpha=FALSE, stretch=NULL,
grays=FALSE, NAzero=FALSE, filename="", overwrite=FALSE, ...)
Expand All @@ -37,6 +37,8 @@ With \code{colorize} you can convert a three-layer RGB SpatRaster into other col

\item{value}{vector of three (or four) positive integers indicating the layers that are red, green and blue (and optionally a fourth transparency layer). Or \code{NULL} to remove the RGB settings}

\item{type}{character. The color space. One of "rgb" "hsv", "hsi" and "hsl"}

\item{to}{character. The color space to transform the values to. If \code{x} has RGB set, you can transform these to "hsv", "hsi" and "hsl", or use "col" to create a single layer with a color table. You can also use "rgb" to back transform to RGB}

\item{alpha}{logical. Should an alpha (transparency) channel be included? Only used if \code{x} has a color-table and \code{to="rgb"}}
Expand Down
14 changes: 7 additions & 7 deletions src/Makevars.ucrt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ PKG_LIBS = \

CXX_STD = CXX

all: clean winlibs
#all: clean winlibs

winlibs:
cp -r "$(R_TOOLS_SOFT)/share/gdal" ../inst/
cp -r "$(R_TOOLS_SOFT)/share/proj" ../inst/
#winlibs:
# cp -r "$(R_TOOLS_SOFT)/share/gdal" ../inst/
# cp -r "$(R_TOOLS_SOFT)/share/proj" ../inst/

clean:
rm -f $(SHLIB) $(OBJECTS)
#clean:
# rm -f $(SHLIB) $(OBJECTS)

.PHONY: all winlibs clean
#.PHONY: all winlibs clean
1 change: 1 addition & 0 deletions src/raster_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4916,6 +4916,7 @@ SpatRaster SpatRaster::hsx2rgb(SpatOptions &opt) {
out.setNames(nms);
out.rgb = true;
out.rgblyrs = {0,1,2};
out.rgbtype = "rgb";

if (!readStart()) {
out.setError(getError());
Expand Down

0 comments on commit ff083d4

Please sign in to comment.