Skip to content

Commit 8629751

Browse files
committed
>2D & multi-scale adjustment in centroids,label
1 parent 80f40cb commit 8629751

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

R/centroids.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,34 @@ setMethod("centroids", "SpatialDataLabel", \(x,
3636
as=c("data.frame", "matrix")) {
3737
as <- match.arg(as)
3838
y <- data(x)
39+
if (length(dim(y)) > 2) {
40+
# max-projection
41+
ax <- match(c("y", "x"), axes(x, "name"))
42+
y <- apply(y, ax, max)
43+
}
3944
y <- as(y, "dgCMatrix")
4045
i <- summary(y)
4146
# flip dimensions so that columns=x, rows=y
42-
# TODO: should these be offset by 0.5?
4347
i[, c(1, 2)] <- i[, c(2, 1)]-0.5
4448
xy <- tapply(i[, -3], i[[3]], colMeans)
4549
xy <- do.call(rbind, xy)
4650
xy <- cbind(xy, as.integer(rownames(xy)))
4751
dimnames(xy) <- list(NULL, c("x", "y", "i"))
52+
# multi-scale adjustment
53+
sf <- .get_multiscale_scale(x)
54+
xy[,1] <- xy[,1]*tail(sf, 1)
55+
xy[,2] <- xy[,2]*tail(sf, 2)[1]
56+
# offset
57+
wh <- metadata(x)$wh
58+
if (!is.null(wh)) {
59+
xy[,1] <- xy[,1]+wh[[1]][1]
60+
xy[,2] <- xy[,2]+wh[[2]][1]
61+
}
62+
# output
4863
if (as == "matrix") return(xy)
4964
xy <- as.data.frame(xy)
50-
xy$i <- factor(xy$i); xy
65+
xy$i <- factor(xy$i)
66+
return(xy)
5167
})
5268

5369
#' @export

0 commit comments

Comments
 (0)