如何重新分配 R 星对象中的单元格/像素值

问题描述

我是 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)

plot

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...