R focus(光栅包):如何将过滤器应用于背景数据的子集? [英] R focal (raster package): how to apply filter to subsets of background data?

查看:96
本文介绍了R focus(光栅包):如何将过滤器应用于背景数据的子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你们中的任何人是否可以帮助我完成以下有关R raster软件包中的focus()函数的任务.

I was wondering if any of you could help me with the following task dealing with the focal() function in the R raster package.

默认情况下,focus()函数将遍历给定栅格(此后称为背景"栅格)的每个像元,并将给定函数应用于用户定义的移动窗口所定义的相邻值.为了在大型栅格/研究区域上优化并加快计算速度,我仅在背景"栅格在移动"所覆盖的范围内具有某些值(例如,大于零)时才应用此函数(过滤器)窗口"并跳过所有其他焦点单元.这样,过滤器将不需要花费时间来计算不需要的任何焦点值.

By default, the focal() function will run through each cell of a given raster ('background' raster hereafter) and apply a given function to the neighboring values as defined by a user-defined moving window. In order to optimize and speed up my computation on large rasters/study areas, I would like to apply this function (filter) only when the 'background' raster has some values (e.g. greater than zero) within the extent covered by the 'moving window' and skip all the other focal cells. This way, the filter would not spend time computing any focal value where there is no need to.

下面是可复制的小示例和在线注释:

Below a reproducible small example and in-line comments:

library(raster)

x <- matrix(1:25, ncol=5)
x[c(1,2,3,6,7,8,11,12,13)] <- 0
r <- raster(x)

#Apply filter to focal cells and return values using a 3x3 moving window...ONLY IF 
#ALL values found within the window are > 0. Skip focal cell otherwise.

r3 <- focal(r, w=matrix(1/9,nrow=3,ncol=3), FUN=sum)

我应该如何更改此功能以达到预期的效果?

How should I change this function to have the desired outcome?

推荐答案

Windows幻灯片在所有焦点像素位置都可运行.无法跳过/跳转位置.但是,您可以检查是否所有元素/矩阵单元格都满足您的阈值条件,如下所示:

The windows slide operates @ all focal pixels locations. Skipping/Jumping locations is not possible. However, you can check, whether all the elements/matrix cells satisfy your threshold condition as below:

myfunc = function (x){
  if(all(x > threshold)){
    print(x)
    x = sum(x)
  }else{
    x = 0}
}
r3 <- focal(x=r>1, w=matrix(1/9,nrow=3,ncol=3), fun=sum)

这篇关于R focus(光栅包):如何将过滤器应用于背景数据的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆