Skip to content

Commit

Permalink
more fixes for #659
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jul 24, 2024
1 parent b09e86e commit 01a3700
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion R/mdim.R
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ write_mdim = function(x, filename, driver = detect.driver(filename), ...,
st_as_stars.mdim = function(.x, ..., downsample = 0, debug = FALSE,
envir = parent.frame()) {
d = st_dimensions(.x)
if (length(downsample) == 1) { # implies only spatial coordinates
ds = dim(.x) * 0
ds[1:2] = downsample
downsample = ds
}
step = rep_len(downsample, length(dim(.x))) + 1
offset = sapply(d, function(x) x$from) - 1
count = (sapply(d, function(x) x$to) - offset) %/% step
Expand All @@ -445,7 +450,7 @@ st_as_stars.mdim = function(.x, ..., downsample = 0, debug = FALSE,
for (i in seq_along(l))
l[[i]] = read_mdim(.x[[i]], names(.x)[i], offset = offset, count = count, step = step, proxy = FALSE, ...)
ret = do.call(c, l)
if (is.na(st_crs(ret)) && !is.na(st_crs(.x))) # as it was set after reading
if (!is.na(st_crs(.x)))
st_crs(ret) = st_crs(.x)
# post-process:
cl = attr(.x, "call_list")
Expand Down
10 changes: 10 additions & 0 deletions R/stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,21 @@ st_crs.dimensions = function(x, ...) {
else
stop(paste("crs of class", class(value), "not recognized"))

drop_if_units = function(x) {
if (inherits(x, "units"))
units::drop_units(x)
else
x
}
# set CRS in dimensions:
xy = attr(x, "raster")$dimensions
if (!all(is.na(xy))) { # has x/y spatial dimensions:
x[[ xy[1] ]]$refsys = value
x[[ xy[2] ]]$refsys = value
x[[ xy[1] ]]$offset = drop_if_units(x[[ xy[1] ]]$offset)
x[[ xy[2] ]]$offset = drop_if_units(x[[ xy[2] ]]$offset)
x[[ xy[1] ]]$delta = drop_if_units(x[[ xy[1] ]]$delta)
x[[ xy[2] ]]$delta = drop_if_units(x[[ xy[2] ]]$delta)
}

if (!all(is.na(xy)) && !is.na(x[[ xy[1] ]]$refsys) && !is.na(value) && st_crs(x) != value)
Expand Down

0 comments on commit 01a3700

Please sign in to comment.