本文介绍了R 焦点(光栅包)函数,计算相对于移动窗口的中心单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的栅格数据集(实际上有几个).我正在寻找 R 的移动窗口过程(如光栅包中的焦点").然而,应用于窗口的函数需要相对于所述窗口的中心单元来计算.

I have a large raster data set (several actually). I'm looking for a moving window process for R (like 'focal' in the raster package). However, the function to apply to the window needs to be caclulated relative to the center cell of said window.

举一个简单的例子,我想要一个移动窗口函数,它告诉我窗口中有多少单元格在窗口中心单元格的某个值d"内.我怀疑我可以通过简单地查询中心单元格的值并围绕它编写一个函数来轻松地做到这一点,以便在 focus() 中使用.但是,我不确定如何查询窗口的中心单元格.

For a simple example, I would like a moving window function that tells me how many of the cells in the window are within some value 'd' of the center cell of the window. I suspect I could do this easily by simply querying the value of the center cell and writing a function around it to use in focal(). However, I'm not sure how to query that center cell of the window.

如果可能的话,那么我最终将需要根据另一个完美重叠的栅格(可能在堆栈或其他东西中)中的中心像元的值在一个栅格上运行此函数.

If that's possible, then I will eventually need to run this function on one raster based on the value of the center cell in another perfectly overlaid raster (which could be in a stack or something).

我对光栅工作很满意,但不太熟悉 R 中 {raster} 包中的 focus() 命令.希望有人能提供一些信息.

I'm comfortable with raster work, but not that familiar with the focal() command from the {raster} package in R. Hoping someone can provide some info.

推荐答案

library(raster)
# taking a 4x5 matrix as a simple example.
x <- matrix(1:20, 4)
> x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
# convert it to a rasterLayer
r <- raster(x)
d <- 15 # a value to use as reference with the function
# a criterion function to apply with focal (moving window)
f.rast <- function(x) length(x[x>d])
# apply the function to a 3x3 moving window
aggr <- as.matrix(focal(r, matrix(1,3,3), f.rast, pad = T, padValue = 0))
> aggr
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    2    2
[2,]    0    0    0    3    3
[3,]    0    0    1    4    4
[4,]    0    0    1    3    3

现在我认为这是理解过滤器d"的问题.

now I think it as a matter of understanding your filter "d".

这篇关于R 焦点(光栅包)函数,计算相对于移动窗口的中心单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:31