如何计算两个栅格图层中有多少像素共享相同的值?
我制作了具有相同范围的两层堆叠栅格,其中包含有关该区域中存在的两种不同事物的信息。两者都是二进制的;每个像素的值为 1(存在)或 0(不存在)。
这是栅格的描述:
> stacked
class : SpatRaster
dimensions : 166, 1622, 2 (nrow, ncol, nlyr)
resolution : 0.1666667, 0.1666667 (x, y)
extent : -131.5, 138.8333, 36.33333, 64 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
sources : memory
memory
names : lyr1, lyr1
min values : 0, 0
max values : 1, 1
如何使用条件来提取两层中 value = 1 的像素数?
当这两个区域是单独的栅格时,我尝试了一些方法,但没有成功,因此我使用extend()来扩大它们的范围,以便它们可以共享一个公共区域。我认为这意味着那里会有 NA 以及 1 和 0。
我尝试了以下操作:
> freq(stacked, value = 1)
layer value count
[1,] 1 1 12243
[2,] 2 1 14804
但这只是分别计算每个层中有多少个值为 1 的像素,而我需要的是两个层的值 = 1 的像素数,因此它们匹配。
非常感谢您的任何提示!
I have made a stacked raster of two layers with the same extent, which contain information about the presence of two different things across the area. Both are binary; the values are either 1 (presence) or 0 (absence) for each pixel.
This is the raster's description:
> stacked
class : SpatRaster
dimensions : 166, 1622, 2 (nrow, ncol, nlyr)
resolution : 0.1666667, 0.1666667 (x, y)
extent : -131.5, 138.8333, 36.33333, 64 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
sources : memory
memory
names : lyr1, lyr1
min values : 0, 0
max values : 1, 1
How can I use conditions to extract the number of pixels where value = 1 in both layers?
I tried a few things when the two areas were separate rasters, but had no success with that, so I used extend() to enlarge both their extents so that they could share a common area. I think this means there will be NAs in there as well as 1s and 0s.
I have tried the following:
> freq(stacked, value = 1)
layer value count
[1,] 1 1 12243
[2,] 2 1 14804
but this just counts how many pixels have a value of 1 in each of the layers separately, whereas what I need is the number of pixels in which the values for BOTH layers =1, so they match.
Many thanks for any tips!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我完全理解您的要求,请找到以下一种可能的解决方案。
Reprex
由 reprex 包于 2022 年 3 月 15 日创建(v2 .0.1)
If I fully understand your request, please find below one possible solution.
Reprex
Created on 2022-03-15 by the reprex package (v2.0.1)