R Under development (unstable) (2024-11-05 r87288 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > options(rgdal_show_exportToProj4_warnings = "none") > suppressPackageStartupMessages(library(stars)) > > if (suppressPackageStartupMessages(require(sp, quietly = TRUE)) && require("raster", quietly = TRUE) && + suppressPackageStartupMessages(require("terra", quietly = TRUE))) { + + tif = system.file("tif/L7_ETMs.tif", package = "stars") + (x = read_stars(tif)) + (r = as(x, "Raster")) + (y = st_as_stars(r)) + + # single band: + x = adrop(x[,,,1]) + r = as(x, "Raster") + (y = st_as_stars(r)) + + # proxy: + (x = read_stars(tif, proxy = TRUE)) + (r = as(x, "Raster")) + (y = st_as_stars(r)) + (y2 = st_as_stars(r, proxy = TRUE)) + } stars_proxy object with 1 attribute in 1 file(s): $L7_ETMs.tif [1] "[...]/L7_ETMs.tif" dimension(s): from to offset delta refsys point x/y x 1 349 288776 28.5 SIRGAS 2000 / UTM zone 25S FALSE [x] y 1 352 9120761 -28.5 SIRGAS 2000 / UTM zone 25S FALSE [y] band 1 6 NA NA NA NA > > if (require(terra, quietly = TRUE)) { + ## terra ------------- + stopifnot(require(terra)) + (x = read_stars(tif)) + (r = as(x, "SpatRaster")) + (y = st_as_stars(r)) + + # single band: + x = adrop(x[,,,1]) + r = as(x, "SpatRaster") + (y = st_as_stars(r)) + + # proxy: + (x = read_stars(tif, proxy = TRUE)) + (r = as(x, "SpatRaster")) + (y = st_as_stars(r)) + (y2 = st_as_stars(r, proxy = TRUE)) + + f = system.file("tif/lc.tif", package = "stars") + lc = read_stars(system.file("tif/lc.tif", package = "stars")) + levels(lc[[1]]) + r = rast(lc) + e = attr(lc[[1]], "exclude") + + if (packageVersion("terra") >= "1.5.48") { + + print(all.equal(terra::levels(r)[[1]][,2], terra::levels(rast(f))[[1]][!e,2])) + print(all.equal(terra::coltab(r)[[1]][!e,], terra::coltab(rast(f))[[1]][!e,], check.attributes = FALSE)) + + r<- rast(nrow=10, ncol=10) + set.seed(5) + values(r)<- sample(1:3, size = ncell(r), replace = TRUE) + levels(r) <- data.frame(ID=1:3, category = c("c1", "c2", "c3")) + + r2<- rast(nrow=10, ncol=10) + set.seed(6) + values(r2)<- sample(c(0,2,3), size = ncell(r2), replace = TRUE) + levels(r2) <- data.frame(ID=c(0,2,3), category = c("x1", "x2", "x3")) + + r3<- c(r,r2) + names(r3)<- c("lyr.1", "lyr.2") + + r3_stars<- st_as_stars(r3) + + st_redimension(r3_stars) |> print() + rast(r3_stars) |> levels() |> print() + + m = matrix(1:121, 11, 11) + bb = st_bbox(c(xmin=0, xmax=11, ymin=0, ymax=11)) + s = st_as_stars(bb, values = m) + r = rast(s) + aggregate(r, 2) |> st_as_stars() -> r.s + stopifnot(isTRUE(all.equal(r.s[,1:5,1:5], st_downsample(s, 1, FUN = mean)))) + } + } [1] TRUE [1] TRUE stars object with 3 dimensions and 1 attribute attribute(s): lyr.1.lyr.2 c1:34 c2:32 c3:34 x1:28 x2:37 x3:35 dimension(s): from to offset delta refsys values x/y x 1 10 -180 36 WGS 84 (CRS84) NULL [x] y 1 10 90 -18 WGS 84 (CRS84) NULL [y] new_dim 1 2 NA NA NA lyr.1, lyr.2 [[1]] IDs categories 1 1 c1 2 2 c2 3 3 c3 [[2]] IDs categories 1 0 x1 2 2 x2 3 3 x3 > > > proc.time() user system elapsed 6.00 0.21 6.20