Skip to content

Commit

Permalink
?
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Oct 1, 2024
1 parent 4cd5310 commit 05ca6e0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- `as.list<SpatRasterDataset>` sets the names of the list [#1513](https://github.com/rspatial/terra/issues/1513)
- a SpatVectorCollection can now be subset with its names; and if made from a list it takes the names from the list. [1515](https://github.com/rspatial/terra/issues/1515) by jedgroev
- argument `fill_range` to plot<SpatRaster> and `plot<SpatVector>` to use the color of the extreme values of the specified range [#1553](https://github.com/rspatial/terra/issues/1553) by Mike Koontz

- plet<SpatRaster> can now handle rasters with a "local" (Cartesian) CRS. [#1570](https://github.com/rspatial/terra/issues/1570) by Augustin Lobo.

## new

Expand Down
3 changes: 2 additions & 1 deletion R/focal.R
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@ function(x, w=3, fun="ols", ..., fillvalue=NA, filename="", overwrite=FALSE, wop
outnl <- funopt$nl
} else {
# need to test
out <- fun(1:msz, sample(msz))
X <- matrix(sample(msz * (nl-1), replace=TRUE), ncol=nl-1)
out <- fun(1:msz, X)
outnl <- length(out)
if (is.null(wopt$names) && (length(names(out)) == outnl)) {
wopt$names <- names(out)
Expand Down
22 changes: 19 additions & 3 deletions R/names.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,28 @@ setMethod("names<-", signature(x="SpatRasterDataset"),
x@ptr <- x@ptr$deepcopy()
if (is.null(value)) {
value <- rep("", length(x))
}
x@ptr$names <- enc2utf8(as.character(value))
x
}
if (is.list(value)) {
nl <- nlyr(x)
if (lenght(value) == 1) {
if (length(unique(nl)) > 1) {
error("names<-", "the number of layers varies between datasets")
}
x@ptr$set_layernames(enc2utf8(as.character(value[[1]])), -1)
} else {
if (length(value) != length(x)) {
error("names<-", "the number of list elements does not match the number of datasets")
}
for (i in seq_along(length(x))) x@ptr$set_layernames(enc2utf8(as.character(value[[i]]), i-1))
}
} else {
x@ptr$names <- enc2utf8(as.character(value))
}
messages("names<-", x)
}
)


setMethod("set.names", signature(x="SpatRasterDataset"),
function(x, value, index=1:length(x), validate=FALSE) {
value <- .names_check(x, value, index, validate, length)
Expand Down
12 changes: 5 additions & 7 deletions R/plot_let.R
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,6 @@ setMethod("plet", signature(x="SpatRaster"),

#checkLeafLetVersion()

if (has.RGB(x)) {
x <- colorize(x, "col")
}

if (is.na(crs(x)) | (grepl("^Cartesian", .name_or_proj4(x)))) {
tiles <- ""
e <- ext(x)
Expand All @@ -481,8 +477,11 @@ setMethod("plet", signature(x="SpatRaster"),
m <- max(rx, ry)
ext(x) <- c(0, rx/m, 0, ry/m)
crs(x) <- "EPSG:3857"
}
notmerc <- FALSE

} else {
notmerc <- isTRUE(crs(x, describe=TRUE)$code != "3857")
}

# if (!is.null(add)) {
# if (inherits(add, "SpatVector")) {
Expand Down Expand Up @@ -541,9 +540,8 @@ setMethod("plet", signature(x="SpatRaster"),
}


notmerc <- isTRUE(crs(x, describe=TRUE)$code != "3857")

if (nlyr(x) == 1) {
if (has.RGB(x) | nlyr(x) == 1) {
map <- leaflet::addRasterImage(map, x, colors=col, opacity=alpha, project=notmerc)
if (!is.null(legend)) {
if (!all(hasMinMax(x))) setMinMax(x)
Expand Down
3 changes: 2 additions & 1 deletion R/subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ setMethod("subset", signature(x="SpatVector"),
r <- if (missing(subset)) {
TRUE
} else {
r <- eval(substitute(subset), d, parent.frame())
e <- substitute(subset)
r <- eval(e, d, parent.frame())
if (!is.logical(r)) error("subset", "argument 'subset' must be logical")
r & !is.na(r)
}
Expand Down
30 changes: 30 additions & 0 deletions src/spatRasterMultiple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,36 @@ void SpatRasterStack::set_units(std::vector<std::string> u) {
units = u;
}
}

void SpatRasterStack::set_layernames(std::vector<std::string> nms, long id) {
if (id < 0) {
for (size_t i=0; i<ds.size(); i++) {
if (ds[i].nlyr() != nms.size()) {
setError("length of names does not match the number of layers");
} else {
ds[i].setNames(nms);
}
}
} else {
if (ds[id].nlyr() != nms.size()) {
setError("length of names does not match the number of layers");
} else {
ds[id].setNames(nms);
}
}
}

std::vector<std::vector<std::string>> SpatRasterStack::get_layernames() {
size_t nd = ds.size();
std::vector<std::vector<std::string>> out(nd);
for (size_t i = 0; i<nd; i++) {
out[i] = ds[i].getNames();
}
return out;
}



std::vector<std::string> SpatRasterStack::filenames() {
size_t n =0;
for (size_t i=0; i<ds.size(); i++) {
Expand Down
6 changes: 5 additions & 1 deletion src/spatRasterMultiple.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ class SpatRasterStack {
SpatExtent getExtent();

std::vector<std::string> get_names();
void set_names(std::vector<std::string> nms);
void set_names(std::vector<std::string> nms);
std::vector<std::string> get_longnames();
void set_longnames(std::vector<std::string> nms);
std::vector<std::string> get_units();
void set_units(std::vector<std::string> u);
std::vector<std::string> filenames();

void set_layernames(std::vector<std::string> nms, long id);
std::vector<std::vector<std::string>> get_layernames();

bool readStart();
bool readStop();
unsigned nsds();
Expand Down

0 comments on commit 05ca6e0

Please sign in to comment.