R星-如何根据旋转Angular 设置仿射值

j0pj023g  于 2023-10-13  发布在  Angular
关注(0)|答案(1)|浏览(109)

我尝试使用仿射变换值旋转一个R stars网格,类似于stars vignette中所示的内容,不同之处在于我知道我想要的旋转Angular ,但我不知道(或缺乏线性代数技能)如何将这些转换为一对仿射变换值“a1”和“a2”。
从上面的链接中,给出的示例显示:

attr(attr(s, "dimensions"), "raster")$affine = c(0.1, 0.1)

#The rotation angle, in degrees, is

atan2(0.1, 1) * 180 / pi
## [1] 5.710593

实际上,我需要在已知旋转Angular 的情况下反转这个过程。旋转的惯例是正逆时针旋转,0表示“向上”或“正北”。
下面是一个完整的reprex,请参阅设置仿射矩阵的注解块:

library(stars)

# define grid
xorg <- 576206  
yorg <- 4412742
nrows <- 8 
ncols <- 5
dim <- 10   # size of cell

####
rot <- 265   # Rotation angle in degrees, relative to "up/North, counterclockwise positive"
aff_mat <-  c(0,0)  # HOW DO I SET THIS TO ACHIEVE THE ROTATION ANGLE ABOVE?
#aff_mat <- c(?, ?)
####

# build a grid
x <- seq(xorg, xorg+((ncols-1)*dim), dim)
y <- seq(yorg, yorg+((nrows-1)*dim), dim)

d <- st_dimensions(x = x, y = y, .raster = c("x", "y"), affine = aff_mat)
m <- matrix(seq(1,length(x)*length(y),1),length(x),length(y))

r1 <- st_as_stars(r = m, dimensions = d)
#plot(r1, axes=TRUE)

# plot
library(ggplot2)
p <- ggplot() + 
  geom_stars(data = r1) + 
  coord_equal() +
  scale_fill_viridis_c() +
  theme_void() +
  theme(legend.position = 'none') 

p

它应该看起来像这样:

# output should look something like:
print(p, vp=grid::viewport(angle=rot))

f0brbegy

f0brbegy1#

stars作者在github上的回答:here

# define grid
library(stars)                                                                        
xorg <- 576206
yorg <- 4412742                                                                       
nrows <- 8                                                                            
ncols <- 5  
dim <- 10   # size of cell                                                            

####
rot <- 265   # Rotation angle in degrees, relative to "up/North, counterclockwise positive"
                                                              
####                                                                                  
                                                                                      
# build a grid                                                                        
x <- seq(xorg, xorg+((ncols-1)*dim), dim)                                             
y <- seq(yorg, yorg+((nrows-1)*dim), dim)                                             
                                                                                      
d <- st_dimensions(x = x, y = y, .raster = c("x", "y"))                               
m <- matrix(seq(1,length(x)*length(y),1),length(x),length(y))                         
                                                                                      
th = rot / 180 * pi # rad                                                             
R = matrix(c(cos(th), sin(th), -sin(th), cos(th)), 2) * dim
r1 <- st_as_stars(r = m, dimensions = d)                                              
                                                                                                                                     
                                                                                      
attr(attr(r1, "dimensions"), "raster")$affine = c(R[1,2], R[2,1])                     
attr(r1, "dimensions")$x$delta = R[1,1]                                               
attr(r1, "dimensions")$y$delta = R[2,2]                                               
plot(r1, axes=TRUE, reset = FALSE)

相关问题