如何将栅格的像元值与该栅格的先前像元进行比较?二进制 r
How do I compare cell values of a raster with previous cells of this raster? binary r
我很新,是使用 R 的初学者。
那是我的问题:
我有一个包含很多像元的大型栅格。它是二进制栅格,因此只有 0 和 1。我必须遍历整个光栅并找到 0。如果单元格 [i,j] 是 0,那么我需要成对查看它的 4 个邻居。
我只是想用一个小的 7x7 矩阵试试这个。
我的想法是使用这样的循环:
nr3=0
for (i in 1:7)
{for (j in 1:7)
{if (m[i,j]==0)
{if (m[i-1,j]!=0&&m[i,j-1]!=0)
{nr3++}
if (m[i-1,j]!=0&&m[i,j+1]!=0)
{nr3++}
if (m[i,j+1]!=0&&m[i+1,j]!=0)
{nr3++}
if (m[i+1,j]!=0&&m[i,j-1]!=0)
{nr3++} }}}
所以这就是必须的。
但是有这个错误:
Error in if (m[i-1,j]!=0&&m[i,j-1]!=0 {: missing value where TRUE/FALSE needed
我能看出问题所在。在边界,你无法比较所有的邻居。
这就是为什么我尝试使用
for (i in 2:6)
for (j in 2:6)
成功了。但问题是有些东西不见了。
那我该怎么办?
顺便说一句,我希望有另一种可能性来解决这个任务。也许我不需要循环?我可以想象这对于非常大的栅格来说不是一个很好的解决方案。
有人有想法吗?
利用 raster
库。这应该比你的循环方法更快:
虚拟矩阵
library(raster)
#create a dummy raster
m <- raster(matrix(c(0,1,1,0,0,1,0,1,0), nrow=3))
> as.matrix(m)
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 1 0 1
[3,] 1 1 0
焦点 window 有 4 个邻居
#define the size of your moving window (focal)
f <- matrix(c(0,1,0,1,1,1,0,1,0), nrow=3)
使用函数raster::focal
用于成对比较和 <<-
赋值:
nr3 <- 0
focal(m,
w = f,
pad = T,
padValue = 0,
fun = function(x){
#x[5] corresponds to the center pixel in the moving window
if(x[5]==0){
#x[2],x[4],x[6] and x[8] are the four neighbours
if(x[2]!=0 & x[4]!=0){
nr3 <<- nr3 + 1
}
if(x[8]!=0 & x[4]!=0){
nr3 <<- nr3 + 1
}
if(x[8]!=0 & x[6]!=0){
nr3 <<- nr3 + 1
}
if(x[2]!=0 & x[6]!=0){
nr3 <<- nr3 + 1
}
}
}
)
输出:
> nr3
[1] 3
注:
您必须在此处使用 <<-
赋值才能在函数环境之外修改变量。引用 Hadley:
The regular assignment arrow, <-
, always creates a variable in the
current environment. The deep assignment arrow, <<-
, never creates a
variable in the current environment, but instead modifies an existing
variable found by walking up the parent environments.
我很新,是使用 R 的初学者。 那是我的问题:
我有一个包含很多像元的大型栅格。它是二进制栅格,因此只有 0 和 1。我必须遍历整个光栅并找到 0。如果单元格 [i,j] 是 0,那么我需要成对查看它的 4 个邻居。
我只是想用一个小的 7x7 矩阵试试这个。
我的想法是使用这样的循环:
nr3=0
for (i in 1:7)
{for (j in 1:7)
{if (m[i,j]==0)
{if (m[i-1,j]!=0&&m[i,j-1]!=0)
{nr3++}
if (m[i-1,j]!=0&&m[i,j+1]!=0)
{nr3++}
if (m[i,j+1]!=0&&m[i+1,j]!=0)
{nr3++}
if (m[i+1,j]!=0&&m[i,j-1]!=0)
{nr3++} }}}
所以这就是必须的。 但是有这个错误:
Error in if (m[i-1,j]!=0&&m[i,j-1]!=0 {: missing value where TRUE/FALSE needed
我能看出问题所在。在边界,你无法比较所有的邻居。 这就是为什么我尝试使用
for (i in 2:6)
for (j in 2:6)
成功了。但问题是有些东西不见了。
那我该怎么办? 顺便说一句,我希望有另一种可能性来解决这个任务。也许我不需要循环?我可以想象这对于非常大的栅格来说不是一个很好的解决方案。 有人有想法吗?
利用 raster
库。这应该比你的循环方法更快:
虚拟矩阵
library(raster)
#create a dummy raster
m <- raster(matrix(c(0,1,1,0,0,1,0,1,0), nrow=3))
> as.matrix(m)
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 1 0 1
[3,] 1 1 0
焦点 window 有 4 个邻居
#define the size of your moving window (focal)
f <- matrix(c(0,1,0,1,1,1,0,1,0), nrow=3)
使用函数raster::focal
用于成对比较和 <<-
赋值:
nr3 <- 0
focal(m,
w = f,
pad = T,
padValue = 0,
fun = function(x){
#x[5] corresponds to the center pixel in the moving window
if(x[5]==0){
#x[2],x[4],x[6] and x[8] are the four neighbours
if(x[2]!=0 & x[4]!=0){
nr3 <<- nr3 + 1
}
if(x[8]!=0 & x[4]!=0){
nr3 <<- nr3 + 1
}
if(x[8]!=0 & x[6]!=0){
nr3 <<- nr3 + 1
}
if(x[2]!=0 & x[6]!=0){
nr3 <<- nr3 + 1
}
}
}
)
输出:
> nr3
[1] 3
注:
您必须在此处使用 <<-
赋值才能在函数环境之外修改变量。引用 Hadley:
The regular assignment arrow,
<-
, always creates a variable in the current environment. The deep assignment arrow,<<-
, never creates a variable in the current environment, but instead modifies an existing variable found by walking up the parent environments.