joincounts.multi()
我的最终目标是使用R 包中的函数计算连接数spdep
。
我有一个非常大的栅格文件(nrows = 19663,ncols = 34073),像元大小为 30 m。它是具有两个值 0 和 1(以及许多 NoData 像元)的二进制栅格。它是整数格式。
首先,我需要生成一个nb
对象——即邻居列表。这个包有多种方法可以做到这一点。对于多边形数据,使用poly2nb
参数。对于网格单元,使用cell2nb
参数。显然后者不适用于非常大的栅格 - 它使我的计算机崩溃,而谷歌搜索表明其他人也遇到了同样的问题。
编辑:我决定在更小的网格上执行此功能。我将所有网格都放在一个文件夹中,并希望循环运行以在每个网格上运行它。不过我遇到了一些问题。这是我的代码:
#set working directory
setwd("C:/Users/myData/")
#get a list of all the raster files in that folder
ingrids <- list.files(getwd(), pattern=".TIF$", full.names=FALSE, recursive=FALSE)
#loop through each raster file, dropping the file extension and giving a unique filename to each, and doing the "cell2nb" function on each.
for (i in 1:length(ingrids)){
fileName <-strsplit(ingrids[i],split="\\.")[[1]][1]
outputFile<-paste(fileName, '_nb','.tif',sep='')
mydatanb <- cell2nb(17,17, type="rook", torus=FALSE)
mydataout <-writeRaster(BETUPAPnb, filename=outputFile, overwrite=TRUE)
}
我收到以下错误消息:
Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘writeRaster’ for signature ‘"nb", "character"’
我将不胜感激任何试图找出这个循环问题的帮助!