1

为了对大型栅格数据集执行 kmean 聚类分析,我尝试使用该函数将RasterBrick对象转换为对象,但是当我将 .grd 文件读回时,所有信息都丢失了。big.matrixbrickR

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函数。知道我该怎么做吗?

4

1 回答 1

4

示例数据

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")
于 2017-12-04T03:05:22.357 回答