Skip to content

Commit ba57f9a

Browse files
committed
More comments
1 parent 7fe5587 commit ba57f9a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

R/layers.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,25 @@ addRasterLegend <- function(map, x, layer = 1, ...) {
341341
color = grDevices::rgb(ct$red/255, ct$green/255, ct$blue/255, ct$alpha/255)
342342
)
343343

344+
# Drop values that aren't part of the layer
345+
color_info <- base::subset(color_info, value %in% terra::values(x))
346+
344347
lvls <- terra::levels(x)[[layer]]
345348

346349
res <- if (is.data.frame(lvls)) {
347350
# Use the labels from levels(x), and look up the matching colors in the
348351
# color table
352+
353+
# The levels data frame can have varying colnames, just normalize them
349354
colnames(lvls) <- c("value", "label")
350355
base::merge(color_info, lvls, by.x = "value", by.y = 1)
351356
} else {
357+
# No level labels provided; use the values as labels
352358
cbind(color_info, label = color_info$value)
353359
}
354360

355-
# Drop values that aren't part of the layer
356-
res <- res[res[["value"]] %in% terra::values(x),]
361+
# At this point, res is a data frame with `value`, `color`, and `label` cols,
362+
# and values/colors not present in the raster layer have been dropped
357363

358364
addLegend(map, colors = res[["color"]], labels = res[["label"]], ...)
359365
}

0 commit comments

Comments
 (0)