Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Feb 11, 2025
1 parent 6a56d85 commit e7e1e9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
3 changes: 3 additions & 0 deletions R/mdim.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ read_mdim = function(filename, variable = character(0), ..., options = character
filename = enc2utf8char(maybe_normalizePath(filename, np = normalize_path))
ret = gdal_read_mdim(filename, variable, options, rev(offset), rev(count), rev(step), proxy, debug)

if (identical(variable, "?"))
return(ret) # RETURNS

if (length(ret$dimensions) == 1 && length(ret$array_list) == 1 && is.data.frame(ret$array_list[[1]]))
return(ret$array_list[[1]]) ## composite data: RETURNS

Expand Down
45 changes: 20 additions & 25 deletions R/rasterize.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,31 @@ st_as_stars.data.frame = function(.x, ..., dims = coords, xy, y_decreasing = TRU
if (any(is.na(xy)))
stop("xy coordinates not found in data")
}
stopifnot(length(dims) >= 1, all(dims >= 1), !any(is.na(dims)))
stopifnot(length(dims) >= 1, all(dims >= 1), !any(is.na(dims)), is.numeric(xy))

index = NULL
dimensions = list()
this_dim = 1
for (i in dims) {
v = .x[[i]]
if (inherits(v, "sfc")) {
if (!requireNamespace("digest", quietly = TRUE))
stop("package digest required, please install it first") # nocov
dig = sapply(st_equals(v), digest::digest)
uv = unique(dig) # no need to sort
ix = match(dig, uv) # but look up "hash collision"
} else {
suv = if (is.factor(v))
levels(v)
else if (is.character(v))
unique(v)
else # numeric:
sort(unique(v), decreasing = y_decreasing && i == xy[2])
ix = match(v, suv)
}
ix = if (inherits(v, "sfc")) {
if (!requireNamespace("digest", quietly = TRUE))
stop("package digest required, please install it first") # nocov
dig = sapply(st_equals(v), digest::digest)
uv = unique(dig) # no need to sort
dimensions[[this_dim]] = create_dimension(values = v[match(uv, dig)])
match(dig, uv) # but look up "hash collision"
} else {
suv = if (is.factor(v))
levels(v)
else if (is.character(v))
unique(v)
else # numeric:
sort(unique(v), decreasing = y_decreasing && i == xy[2])
dimensions[[this_dim]] = create_dimension(values = suv, is_raster = i %in% xy)
match(v, suv)
}
index = cbind(index, ix)
dimensions[[this_dim]] = if (inherits(v, "sfc"))
create_dimension(values = v[match(uv, dig)])
else
create_dimension(values = suv, is_raster = i %in% xy)
this_dim = this_dim + 1
}
names(dimensions) = names(.x)[dims]
Expand All @@ -242,11 +240,8 @@ st_as_stars.data.frame = function(.x, ..., dims = coords, xy, y_decreasing = TRU
else
array(NA, dim = dim(d))
m[index] = x # match order
if(inherits(x, "sfc"))
array(
st_sfc(m, crs = st_crs(x), precision = st_precision(x)),
dim = dim(d)
)
if (inherits(x, "sfc"))
array(st_sfc(m, crs = st_crs(x), precision = st_precision(x)), dim = dim(d))
else
m
}
Expand Down

0 comments on commit e7e1e9c

Please sign in to comment.