首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代搜索和重新分类栅格中最小值的像素

迭代搜索和重新分类栅格中最小值的像素
EN

Stack Overflow用户
提问于 2015-04-09 12:08:23
回答 1查看 156关注 0票数 0

我需要创建一个函数来:

  1. 搜索包含最小值的栅格中的像素;
  2. 在第一次迭代中,将半径内的所有像素分配为栅格大小(2.5km),值为1(包括具有最小值的像素)。
  3. 在第二次迭代中,选择具有下一个最小值的像素(不包括步骤ii中选定的像素),并搜索相同的半径并将其赋值为2。

听起来很复杂,但希望有可能?这里是我的光栅的一个例子:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-12 16:52:44

也许您可以使用下面的内容。我不会在很大的栅格上尝试这个(那将永远)。但就你的例子来说,它很好--如果你不必做太多次的话。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29538093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档