迭代搜索和重新分类栅格中具有最小值的像素
iteratively search and reclass pixels with smallest value in a raster
我需要创建一个函数来:
- 在栅格中搜索包含最小值的像素;
- 在第一次迭代中,将半径等于栅格像元大小(2.5km)内的所有像素点赋值为1(包括值最小的像素点)
- 在第二次迭代中,select 具有下一个最小值的像素(不包括在步骤 ii 中编辑的像素 select)并搜索相同的半径并将这些值指定为 2。这继续直到没有更多像素剩余(如果半径内没有空闲像素,selection 停止)
听起来很复杂,但希望可行吗?这是我的光栅示例:
xy <- matrix(pnorm(900,40, 200),30,30)image(xy)
rast <- raster(xy)
# Give it lat/lon coords for 36-37°E, 3-2°S
extent(rast) <- c(36,37,-3,-2)
也许你可以使用下面的方法。我不会在非常大的栅格上尝试这个(这将永远花费)。但是对于您的示例,它工作正常 --- 如果您不必这样做太多次。
library(raster)
set.seed(0)
xy <- matrix(rnorm(900, 40, 200),30 , 30)
r <- raster(xy)
extent(r) <- c(36,37,-3,-2)
rorig <- r
x <- r
i <- 1
while (TRUE) {
# cell with min value
m <- which.min(x)
## are there, and do you care about ties? Do they have the same level?
## If not, you can do
## m[1]
## or sample
## m <- sample(m, 1)
# focal and four adjacent cells
a <- adjacent(r, m, 4, FALSE, include=TRUE)
# exclude those that have already been affected
w <- which(!is.na(x[a]))
a <- a[w]
# assign the value
r[a] <- i
# set assigned cells to NA
x[a] <- NA
# stop when done
if (is.na(maxValue(x))) break
i <- i + 1
}
plot(r)
plot(rorig, r)
我需要创建一个函数来:
- 在栅格中搜索包含最小值的像素;
- 在第一次迭代中,将半径等于栅格像元大小(2.5km)内的所有像素点赋值为1(包括值最小的像素点)
- 在第二次迭代中,select 具有下一个最小值的像素(不包括在步骤 ii 中编辑的像素 select)并搜索相同的半径并将这些值指定为 2。这继续直到没有更多像素剩余(如果半径内没有空闲像素,selection 停止)
听起来很复杂,但希望可行吗?这是我的光栅示例:
xy <- matrix(pnorm(900,40, 200),30,30)image(xy)
rast <- raster(xy)
# Give it lat/lon coords for 36-37°E, 3-2°S
extent(rast) <- c(36,37,-3,-2)
也许你可以使用下面的方法。我不会在非常大的栅格上尝试这个(这将永远花费)。但是对于您的示例,它工作正常 --- 如果您不必这样做太多次。
library(raster)
set.seed(0)
xy <- matrix(rnorm(900, 40, 200),30 , 30)
r <- raster(xy)
extent(r) <- c(36,37,-3,-2)
rorig <- r
x <- r
i <- 1
while (TRUE) {
# cell with min value
m <- which.min(x)
## are there, and do you care about ties? Do they have the same level?
## If not, you can do
## m[1]
## or sample
## m <- sample(m, 1)
# focal and four adjacent cells
a <- adjacent(r, m, 4, FALSE, include=TRUE)
# exclude those that have already been affected
w <- which(!is.na(x[a]))
a <- a[w]
# assign the value
r[a] <- i
# set assigned cells to NA
x[a] <- NA
# stop when done
if (is.na(maxValue(x))) break
i <- i + 1
}
plot(r)
plot(rorig, r)