Skip to content

Commit

Permalink
comb
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jan 9, 2025
1 parent ed631f0 commit 379d738
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
5 changes: 1 addition & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ S3method(str, SpatRaster)
S3method(str, SpatExtent)
S3method(str, SpatGraticule)

export(add_box, add_legend, add_grid, add_mtext, focalMat, extractAlong, gdal, getGDALconfig, graticule, halo, libVersion, setGDALconfig, map.pal, map_extent, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers, vrt_tiles, names, round)



export(add_box, add_legend, add_grid, add_mtext, combineLevels, focalMat, extractAlong, gdal, getGDALconfig, graticule, halo, libVersion, setGDALconfig, map.pal, map_extent, north, sbar, terraOptions, tmpFiles, makeVRT, mem_info, free_RAM, same.crs, shade, gdalCache, fileBlocksize, vector_layers, vrt_tiles, names, round)


22 changes: 22 additions & 0 deletions R/levels.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ setMethod("levels<-", signature(x="SpatRaster"),
)


combineLevels <- function(x) {
lv <- levels(x)
lv <- lv[sapply(lv, is.data.frame)]
un <- unique(sapply(lv, names))
if (length(un) > 2) {
lv <- lapply(lv, function(i) { colnames(i) <- colnames(lv[[1]]); i})
}
lv <- try(do.call(rbind, lv), silent=TRUE)
if (inherits(lv, "try-error")) { # should not happen anymore
error("panel", "cannot use combine categories")
}
lv <- unique(lv)
if (length(unique(lv[,1])) < nrow(lv)) {
error("panel", "cannot combine conflicting categories")
}
lv <- lv[order(lv[,1]), ]
nms <- names(x)
x <- categories(x, 0, lv)
names(x) <- nms
x
}


setMethod ("set.cats" , "SpatRaster",
function(x, layer=1, value, active=1) {
Expand Down
17 changes: 1 addition & 16 deletions R/panel.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,7 @@ setMethod("panel", signature(x="SpatRaster"),
# }
categorical <- FALSE
if (is.null(type) && any(is.factor(x))) {
lv <- levels(x)
lv <- lv[sapply(lv, is.data.frame)]
un <- unique(sapply(lv, names))
if (length(un) > 2) {
lv <- lapply(lv, function(i) { colnames(i) <- colnames(lv[[1]]); i})
}
lv <- try(do.call(rbind, lv), silent=TRUE)
if (inherits(lv, "try-error")) { # should not happen anymore
error("panel", "cannot use combine categories")
}
lv <- unique(lv)
if (length(unique(lv[,1])) < nrow(lv)) {
error("panel", "cannot use rasters with conflicting categories")
}
lv <- lv[order(lv[,1]), ]
x <- categories(x, 0, lv)
x <- combineLevels(x)
categorical <- TRUE
}

Expand Down
2 changes: 1 addition & 1 deletion R/plot_vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ setMethod("dots", signature(x="SpatVector"),
stopifnot(length(ylim) == 2)
out$lim[3:4] <- sort(ylim)
}
} else if (!add) {
} else if ((!add) && is.null(xpd)) {
xpd <- TRUE
}
out$ngeom <- nrow(x)
Expand Down
5 changes: 5 additions & 0 deletions man/factors.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
\alias{categories,SpatRaster-method}
\alias{addCats}
\alias{addCats,SpatRaster-method}
\alias{shareLevels}


\title{Categorical rasters}
Expand All @@ -33,6 +34,8 @@ To set categories for the first layer of a SpatRaster, you can provide \code{lev
\code{droplevels} removes categories that are not used (declared but not present as values in the raster) if \code{levels=NULL}.

\code{addCats} adds additional categories to a layer that already is categorical. It adds new variables, not new levels of an existing categorical variable.

\code{combineLevels} combines the levels of all layers of \code{x} and sets them to all layers. That fails if there are labeling conflicts between layers
}

\usage{
Expand All @@ -47,6 +50,8 @@ To set categories for the first layer of a SpatRaster, you can provide \code{lev
\S4method{droplevels}{SpatRaster}(x, level=NULL, layer=1)

\S4method{addCats}{SpatRaster}(x, value, merge=FALSE, layer=1)

combineLevels(x)
}


Expand Down

0 comments on commit 379d738

Please sign in to comment.