我有一个栅格砖,在岛屿上有多层气候数据,被NA海洋细胞包围。我想将一些海洋细胞设置为附近海岸的价值。这样做有一个惊人的麻烦。在我看来,代码应该类似于下面,但我的内存不足。栅格大约2500x2500。
# dummy stack with three layers
slogo <- stack(system.file("external/rlogo.grd", package="raster"))
# let's say cell 5 is the one I want to change
newvals <- 1000*(1:3)
for(i in 1:nlayers(slogo)) slogo[[i]][5] <- newvals[i]
后续步骤是将栅格写入单独的ascii文件;所以另一个hacky解决方案可能是剪切到文本文件并替换单个值...
编辑:
也许有人可以通过重置内存分配限制来推荐一个解决方案?这是我在大型栅格上运行后显示的错误消息。
Error: cannot allocate vector of size 504.1 Mb
In addition: Warning messages:
1: In readBin(raster@file@con, what = dtype, n = nc, dsize, dsign, :
Reached total allocation of 3979Mb: see help(memory.size)
2: In readBin(raster@file@con, what = dtype, n = nc, dsize, dsign, :
Reached total allocation of 3979Mb: see help(memory.size)
3: closing unused connection 4 (C:\Users\jcw\AppData\Local\Temp\R_raster_jcw\r_tmp_2015-08-16_130255_2000_11926.gri)
4: In getBilData(object, r = startrow, nrows = nrows, c = startcol, :
Reached total allocation of 3979Mb: see help(memory.size)
5: In getBilData(object, r = startrow, nrows = nrows, c = startcol, :
Reached total allocation of 3979Mb: see help(memory.size)
6: In getBilData(object, r = startrow, nrows = nrows, c = startcol, :
Reached total allocation of 3979Mb: see help(memory.size)
7: In getBilData(object, r = startrow, nrows = nrows, c = startcol, :
Reached total allocation of 3979Mb: see help(memory.size)
答案 0 :(得分:1)
你非常接近,这只是一个索引问题:
library(raster)
# dummy stack with three layers
slogo <- brick(system.file("external/rlogo.grd", package="raster"))
## Check values of all three bands at cell 5:
slogo[5]
收率:
red green blue
[1,] 255 255 255
## Set values of all three bands to zero:
slogo[5][1:3] <- 0
slogo[5]
收率:
red green blue
[1,] 0 0 0
另一种可能使OP超过一次处理所有波段的内存问题的替代方案:
## Alternatively, load each band into memory separately, do the
## replacement and write each band back to disk as a separate GeoTiff:
for (i in 1:nbands(slogo)) {
r <- raster(slogo, layer=i)
r[5] <- 0
writeRaster(r, file=paste0("band_", i, ".tif"), format="GTiff")
}
答案 1 :(得分:0)
您可以使用raster::update
功能更改磁盘上光栅文件中的值(进行备份!)