Skip to content

Commit

Permalink
handle arbitrary ordering of IDs in SpatRaster; #484
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jun 17, 2022
1 parent 75cd3b2 commit 04bd227
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion R/raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ get_terra_levels = function(x, min_v) {
# create factor levels, as used by stars, from SpatRaster levels in a data.frame
# see https://github.com/r-spatial/stars/pull/484
levels = x[[1]]
o = order(levels) # may be arbitrary
levels = levels[o] # sorts
if (any(levels < 0))
stop("negative IDs in SpatRaster levels not supported")
ex = setdiff(0:max(levels), levels)
exclude = rep(FALSE, max(levels) + 1)
exclude[ex + 1] = TRUE # 0-based vector
list(levels = levels, labels = x[[2]], exclude = exclude)
list(levels = levels, labels = x[[2]][o], exclude = exclude)
}

#' @name st_as_stars
Expand Down

0 comments on commit 04bd227

Please sign in to comment.