diff --git a/R/SpatRasterDataset.R b/R/SpatRasterDataset.R index 957d91c1e..6b523bc4a 100644 --- a/R/SpatRasterDataset.R +++ b/R/SpatRasterDataset.R @@ -178,7 +178,7 @@ setReplaceMethod("[", c("SpatRasterDataset", "numeric", "missing"), if (j == (length(x)+1)) { x@ptr$add(value@ptr, "", "", "", FALSE) } else { - x@ptr$replace(j-1, value@ptr) + x@ptr$replace(j-1, value@ptr, FALSE) } } messages(x, "`[`") diff --git a/src/spatRasterMultiple.cpp b/src/spatRasterMultiple.cpp index a6d24665b..0c2b0a73d 100644 --- a/src/spatRasterMultiple.cpp +++ b/src/spatRasterMultiple.cpp @@ -407,7 +407,7 @@ SpatRasterStack SpatRasterStack::crop(SpatExtent e, std::string snap, bool expan return out; } -void SpatRasterStack::replace(unsigned i, SpatRaster x) { +void SpatRasterStack::replace(unsigned i, SpatRaster x, bool setname) { if (i > (ds.size()-1)) { setError("invalid index"); return; @@ -422,9 +422,12 @@ void SpatRasterStack::replace(unsigned i, SpatRaster x) { } ds[i] = x; - names[i] = x.getNames()[0]; - long_names[i] = x.getLongSourceNames()[0]; - units[i] = x.getUnit()[0]; +// for clause for #1604 + if (setname) { + names[i] = x.getNames()[0]; + long_names[i] = x.getLongSourceNames()[0]; + units[i] = x.getUnit()[0]; + } } SpatRaster SpatRasterStack::collapse() { diff --git a/src/spatRasterMultiple.h b/src/spatRasterMultiple.h index f02fec3de..5dc8d4056 100644 --- a/src/spatRasterMultiple.h +++ b/src/spatRasterMultiple.h @@ -70,7 +70,7 @@ class SpatRasterStack { SpatRasterStack subset(std::vector x); SpatRasterStack crop(SpatExtent e, std::string snap, bool expand, SpatOptions &opt); - void replace(unsigned i, SpatRaster x); + void replace(unsigned i, SpatRaster x, bool setname); SpatRaster collapse(); SpatRaster summary_numb(std::string fun, std::vector add, bool narm, SpatOptions &opt); SpatRaster summary(std::string fun, bool narm, SpatOptions &opt);