Skip to content

Commit

Permalink
handle color tables read through GDAL
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Mar 2, 2020
1 parent ec174e9 commit 1e29822
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 13 deletions.
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# version 0.4-1

* handle color tables read through GDAL; #128

* handle dimension `crs` specially, for proxy objects now.

* handle new-style `crs` objects from upcoming sf, moving away from proj4strings

* handle `crs` objects as `refsys` element in a spatial dimensions, rather than proj4string
* handle full `crs` objects as `refsys` element in a spatial dimensions, rather than proj4string only

* `st_raster_type(x)` reveals the raster type of `x`; #248, https://github.com/mtennekes/tmap/issues/368

Expand Down
9 changes: 7 additions & 2 deletions R/dimensions.R
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,14 @@ identical_dimensions = function(lst) {
TRUE
}

combine_dimensions = function(dots, along) {
dims = attr(dots[[1]], "dimensions")
combine_dimensions = function(dots, along, check_dims_identical = TRUE) {
dims = st_dimensions(dots[[1]])
if (along > length(dims)) {
if (length(dots) > 1 && check_dims_identical) {
for (i in 2:length(dots))
if (!identical(dims, st_dimensions(dots[[i]])))
stop(paste("dimensions of element", 1, "and", i, "are not identical"))
}
dims[[along]] = create_dimension(from = 1, to = length(dots), values = names(dots))
} else {
offset = lapply(dots, function(x) attr(x, "dimensions")[[along]]$offset)
Expand Down
10 changes: 7 additions & 3 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ image.stars = function(x, ..., band = 1, attr = 1, asp = NULL, rgb = NULL,

ar = unclass(x[[ attr ]]) # raw data matrix/array

co = attr(ar, "colors")
# rearrange ar:
others = setdiff(seq_along(dim(ar)), c(dimxn, dimyn))
ar = aperm(ar, c(dimxn, dimyn, others))
Expand Down Expand Up @@ -307,9 +308,12 @@ image.stars = function(x, ..., band = 1, attr = 1, asp = NULL, rgb = NULL,
stopifnot(isTRUE(rgb) || inherits(rgb, "data.frame"))
# rgb has col 1: index, col 2: label, col 3-5: R, G, B
# ar = as.vector(ar[ , , 1]) # flattens x/y to 1-D index vector
mat = if (isTRUE(rgb))
ar
else {
mat = if (isTRUE(rgb)) {
if (!is.null(co))
structure(co[as.vector(ar)], dim = dim(ar))
else
ar
} else {
ar = as.vector(ar) # flattens x/y to 1-D index vector
rgb = grDevices::rgb(rgb[match(ar, rgb[[1]]), 3:5], maxColorValue = maxColorValue)
structure(rgb, dim = xy)
Expand Down
4 changes: 2 additions & 2 deletions R/proxy.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ combine_along_crs_proxy = function(dots) {
# erase offset:
erase_offset = function(x) {
d = st_dimensions(x)
xy = attr(d, "raster")$dimensions
d[[ xy[1] ]]$offset = d[[ xy[2] ]]$offset = NA
# xy = attr(d, "raster")$dimensions
# d[[ xy[1] ]]$offset = d[[ xy[2] ]]$offset = NA
st_set_crs(st_stars_proxy(x, d), NA)
}
l = lapply(dots, erase_offset)
Expand Down
7 changes: 7 additions & 0 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ read_stars = function(.x, ..., options = character(0), driver = character(0),
newdims = lengths(meta_data$dim_extra)
if (length(newdims) && !proxy)
dim(data) = c(dim(data)[1:2], newdims)

ct = meta_data$color_tables
if (!proxy && any(lengths(ct) > 0)) {
ct = ct[[ which(length(ct) > 0)[1] ]]
co = apply(ct, 1, function(x) rgb(x[1], x[2], x[3], x[4], maxColorValue = 255))
data = structure(data + 1, levels = 1:256, colors = co, class = "factor")
}

dims = if (proxy) {
if (length(meta_data$bands) > 1)
Expand Down
2 changes: 1 addition & 1 deletion man/st_join.stars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/proxy.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ if (f != "") {

try(ret <- c(l[[1]], l[[2]], l[[3]], along = list(times = as.Date("1981-09-01") + 0:2)))
#print(ret)
ret = adrop(adrop(c(l[[1]], l[[2]], l[[3]], along = "times")))
print(ret)
#ret = adrop(adrop(c(l[[1]], l[[2]], l[[3]], along = "times")))
#print(ret)
ret <- st_redimension(l[[1]], along = list(times = as.Date("1981-09-01") + 0:1))
print(ret)
}
4 changes: 2 additions & 2 deletions tests/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ if (f != "") {
print(ret)
ret = adrop(c(l[[1]], l[[2]], l[[3]], along = list(times = as.Date("1981-09-01") + 0:2)))
print(ret)
ret = adrop(adrop(c(l[[1]], l[[2]], l[[3]], along = "times")))
print(ret)
#ret = adrop(adrop(c(l[[1]], l[[2]], l[[3]], along = "times")))
#print(ret)
}

st_dimensions(list(matrix(1, 4, 4))) # st_dimensions.default
Expand Down

0 comments on commit 1e29822

Please sign in to comment.