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# '
4243# ' sd_plot_shape(sd, "c", col=4)
4344NULL
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
0 commit comments