Skip to content

Commit 3405aae

Browse files
committed
more docs
1 parent 39abc83 commit 3405aae

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export(ct_data)
1111
export(ct_name)
1212
export(ct_rotate)
1313
export(ct_scale)
14+
export(ct_transform)
1415
export(ct_translate)
1516
export(ct_type)
1617
export(data)

R/Zattrs.R

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,13 @@ method(ct_args, list(Zattrs, class_any)) <- \(x, i) {
8686
i <- which(ct_name(x) == i)
8787
l <- ct_data(x)[[i]]
8888
t <- l[[ct_type(x)[[i]]]]
89-
# TODO: not sure how to best return yet;
90-
# all scalar or vector, but not for affine
91-
t <- lapply(t, as.list)
92-
lapply(t, unlist)
93-
# if (is.list(t[[1]])) {
94-
# do.call(rbind, t)
95-
# } else {
96-
# unlist(t)
97-
# }
89+
# # TODO: not sure how to best return yet;
90+
# # all scalar or vector, but not for affine
91+
# t <- lapply(t, as.list)
92+
# lapply(t, unlist)
93+
if (is.list(t[[1]])) {
94+
do.call(rbind, t)
95+
} else {
96+
unlist(t)
97+
}
9898
}
99-
100-
#' @rdname Zattrs
101-
#' @importFrom S7 class_any
102-
#' @export
103-
ct_args <- new_generic("ct_args", c("x", "i"))

R/trans.R

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#' @name coord-trans
22
#' @title Coordinate transformations
33
#'
4-
#' @aliases ct_translate ct_scale ct_rotate
4+
#' @aliases ct_transform ct_translate ct_scale ct_rotate
55
#'
66
#' @param x \code{SpatialData} element.
7+
#' @param i target coordinate system.
78
#' @param t transformation data.
89
#' @param ... ignored.
910
#'
@@ -42,13 +43,36 @@
4243
#' sd_plot_shape(sd, "c", col=4)
4344
NULL
4445

46+
# transform ----
47+
48+
#' @rdname coord-trans
49+
#' @export
50+
ct_transform <- new_generic("ct_transform", c("x", "i"))
51+
52+
method(ct_transform, list(sdArrayFrame, class_character)) <- \(x, i) {
53+
i <- match.arg(i, ct_name(x))
54+
i <- match(i, ct_name(x))
55+
ct_transform(x, i)
56+
}
57+
58+
method(ct_transform, list(sdArrayFrame, class_numeric)) <- \(x, i) {
59+
t <- ct_args(x, i)
60+
switch(ct_type(x)[i],
61+
identity=return(x),
62+
scale=ct_scale(x, t),
63+
rotation=ct_rotate(x, t),
64+
translation=ct_translate(x, t),
65+
stop("not supported."))
66+
}
67+
4568
# translate ----
4669

4770
#' @rdname coord-trans
4871
#' @export
49-
ct_translate <- new_generic("ct_translate", "x")
72+
ct_translate <- new_generic("ct_translate", c("x", "t"))
5073

51-
method(ct_translate, PointFrame) <- \(x, t) {
74+
method(ct_translate, list(PointFrame, class_numeric)) <- \(x, t) {
75+
stopifnot(length(t) == 2, is.finite(t))
5276
y <- NULL # R CMD check
5377
x@data <- x@data |>
5478
mutate(x=x+t[1]) |>
@@ -57,7 +81,8 @@ method(ct_translate, PointFrame) <- \(x, t) {
5781
}
5882

5983
#' @importFrom sfarrow read_sf_dataset
60-
method(ct_translate, ShapeFrame) <- \(x, t) {
84+
method(ct_translate, list(ShapeFrame, class_numeric)) <- \(x, t) {
85+
stopifnot(length(t) == 2, is.finite(t))
6186
x@data <- read_sf_dataset(x@data)
6287
x@data$geometry <- x@data$geometry+t
6388
return(x)
@@ -67,10 +92,10 @@ method(ct_translate, ShapeFrame) <- \(x, t) {
6792

6893
#' @rdname coord-trans
6994
#' @export
70-
ct_scale <- new_generic("ct_scale", "x")
95+
ct_scale <- new_generic("ct_scale", c("x", "t"))
7196

7297
#' @importFrom dplyr mutate
73-
method(ct_scale, PointFrame) <- \(x, t) {
98+
method(ct_scale, list(PointFrame, class_numeric)) <- \(x, t) {
7499
y <- NULL # R CMD check
75100
x@data <- x@data |>
76101
mutate(x=x*t[1]) |>
@@ -81,7 +106,7 @@ method(ct_scale, PointFrame) <- \(x, t) {
81106
# NOTE: this shifts the origin if it's not (0,0);
82107
# could fix, but unclear what's expected.
83108
#' @importFrom sfarrow read_sf_dataset
84-
method(ct_scale, ShapeFrame) <- \(x, t) {
109+
method(ct_scale, list(ShapeFrame, class_numeric)) <- \(x, t) {
85110
x@data <- read_sf_dataset(x@data)
86111
x@data$geometry <- x@data$geometry*t(t)
87112
return(x)
@@ -91,14 +116,14 @@ method(ct_scale, ShapeFrame) <- \(x, t) {
91116

92117
#' @rdname coord-trans
93118
#' @export
94-
ct_rotate <- new_generic("ct_rotate", "x")
119+
ct_rotate <- new_generic("ct_rotate", c("x", "t"))
95120

96121
# rotation matrix to rotate points
97122
# counter-clockwise through an angle 't'
98123
.R <- \(t) matrix(c(cos(t), -sin(t), sin(t), cos(t)), 2, 2)
99124

100125
#' @importFrom dplyr mutate select
101-
method(ct_rotate, PointFrame) <- \(x, t) {
126+
method(ct_rotate, list(PointFrame, class_numeric)) <- \(x, t) {
102127
y <- .y <- .x <- NULL # R CMD check
103128
R <- .R(t*pi/180)
104129
x@data <- x@data |>
@@ -111,7 +136,7 @@ method(ct_rotate, PointFrame) <- \(x, t) {
111136
}
112137

113138
#' @importFrom sfarrow read_sf_dataset
114-
method(ct_rotate, ShapeFrame) <- \(x, t) {
139+
method(ct_rotate, list(ShapeFrame, class_numeric)) <- \(x, t) {
115140
R <- .R(t*pi/180)
116141
x@data <- read_sf_dataset(x@data)
117142
x@data$geometry <- x@data$geometry * R

man/Zattrs.Rd

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/coord-trans.Rd

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)