为了对大型栅格数据集执行kmean聚类分析,我尝试使用RasterBrick
函数将big.matrix
对象转换为brick
对象,但是当我阅读时。将grd文件重新导入R
,所有信息都将丢失。
library(raster)
library(bigmemory)
library(biganalytics)
#initialize raster
one <- raster(matrix(rnorm(400), 20, 20))
two <- raster(matrix(rnorm(400), 20, 20))
three <- raster(matrix(rnorm(400), 20, 20))
#save brick object as .grd file
brick(one, two, three, filename = "test")
#read .grd file in as big.matrix
big_matrix <- as.big.matrix("test.grd", type = "double")
#check dimensions
dim(big_matrix)
#perform kmeans
bigkmeans(big_matrix, 3)
我可以在我的目录中看到.grd和.gri文件,但我无法弄清楚如何阅读它们,或者如何将.grd文件提供给bigkmean
函数。知道怎么做这个吗?
答案 0 :(得分:3)
示例数据
library(raster)
library(bigmemory)
b <- brick(system.file("external/rlogo.grd", package="raster"))
如果文件不是那么大,你可以
x <- as.big.matrix(values(b))
否则,这是您可以使用的功能。
r2bm <- function(from, filename="") {
b <- big.matrix(ncell(from), nlayers(from), backingfile=filename )
nc <- ncol(from)
tr <- blockSize(from)
for (i in 1:tr$n) {
start <- ((tr$row[i]-1) * nc) + 1
end <- start + (tr$nrows[i] * nc) - 1
b[start:end, ] <- getValues(from, row=tr$row[i], nrows=tr$nrows[i])
}
b
}
现在使用它
y <- r2bm(b, "bg.dat")