我有一个智利的栅格网格,想要计算附近的陆地细胞的数量。在R中是否有一种方法可以计算光栅中相邻/相邻单元的数目?因此,大多数细胞应该得到值8(由于8个浪涌细胞),但沿海岸的细胞将得到较少。
发布于 2022-01-21 10:25:34
在自定义函数中使用相邻函数
我定义了一个简单的例子光栅,并使用一个简单的条件来定义土地细胞和水细胞(这应该根据您的数据进行调整):
rasterExample <- raster(matrix(runif(100, 0, 1),10,10))
getNumbersOfLandcells <- function(raster2analyze) {
raster2return <- raster2analyze
#get 0 for no land and 1 for land - this should be adjusted!
raster2analyze[raster2analyze < 0.5] <- 0
raster2analyze[raster2analyze >= 0.5] <- 1
Landcells <- which(values(raster2analyze) == 1)
for (cell in Landcells){
#count cells with 1
ncells <- adjacent(raster2analyze, cell=cell, direction=8,include=F,pairs=F)
nLand <- sum(raster2analyze[ncells], na.rm=T)
raster2return[cell] <- nLand
}
return(raster2return)
}
rasterLandcells <- getNumbersOfLandcells(rasterExample)https://stackoverflow.com/questions/70799523
复制相似问题