问题描述
我是 R 中星包的新手,我正在尝试研究如何为二维星对象(光栅包中的光栅)中的单元格分配新值。使用光栅我可以做如下
> library("raster")
> library('stars')
> tif = system.file("tif/L7_ETMs.tif",package = "stars")
> ras<-brick(tif)[[2]] #create raster layer from 2nd layer of multilayer tif
> ras[1,1:5] #see first 5 values of top row of ras
[1] 56 57 52 45 52
> ras[1,1:4] <-100 #replace first 4 values of top row of ras
> ras[1,1:5]
[1] 100 100 100 100 52
根据星包手册中的 st_subset 说明,赋值遵循“x[i]
> tif = system.file("tif/L7_ETMs.tif",package = "stars")
> st<-read_stars(tif)[,2] #create raster layer from second layer of multilayer tif
> unlist(st[,1:5,1,]) #see first 5 values of top row of st
L7_ETMs.tif1 L7_ETMs.tif2 L7_ETMs.tif3 L7_ETMs.tif4 L7_ETMs.tif5
56 57 52 45 52
> st[,1:4,]<-100 #replace first 4 values of top row of ras
Error in `[<-.stars`(`*tmP*`,value = 100) :
unused arguments (alist(1:4,))
非常感谢
解决方法
可以通过底层 stars
(如果是单波段)或 matrix
(如果是多波段)访问和修改 array
值,使用 r[[1]]
访问。
例如:
library(stars)
# Reading 2nd band
tif = system.file("tif/L7_ETMs.tif",package = "stars")
r = read_stars(tif,RasterIO = list(bands = 2))
# Accessing values (as matrix)
r[[1]][20:140,20:30]
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 65 66 67 64 61 59 56 39 40 40 40
## [2,] 62 56 52 48 44 42 38 41 40 40 40
## [3,] 50 44 41 40 40 39 40 41 43 42 41
## ...
# Modifying values
r[[1]][20:140,20:30] = 100
# Plot
plot(r)