如何计算两个栅格图层中有多少像素共享相同的值?

发布于 2025-01-14 07:19:32 字数 841 浏览 2 评论 0原文

我制作了具有相同范围的两层堆叠栅格,其中包含有关该区域中存在的两种不同事物的信息。两者都是二进制的;每个像素的值为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2025-01-21 07:19:32

如果我完全理解您的要求,请找到以下一种可能的解决方案。

Reprex

library(terra)

# Build a dummy Spatraster with two layers containing only 1 and 0
set.seed(0) # for reproducibility
r <- rast(nrows=10, ncols=10, nlyrs=2)
values(r) <- sample(c(1,0), 200, replace = TRUE)

r
#> class       : SpatRaster 
#> dimensions  : 10, 10, 2  (nrow, ncol, nlyr)
#> resolution  : 36, 18  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 
#> source      : memory 
#> names       : lyr.1, lyr.2 
#> min values  :     0,     0 
#> max values  :     1,     1


# If you use terra::freq(), you get the number of cells with 0 and 1 for each layer
terra::freq(r)
#>      layer value count
#> [1,]     1     0    52
#> [2,]     1     1    48
#> [3,]     2     0    47
#> [4,]     2     1    53

# So, one approach is to sum the two layers and retrieve the number of cells with 2
# (here 24 cells have the value 1 in the two layers)

terra::freq(sum(r))
#>      layer value count
#> [1,]     1     0    23
#> [2,]     1     1    53
#> [3,]     1     2    24

# OR more directly:
terra::freq(sum(r), value = 2)[, 'count']
#> count 
#>    24

reprex 包于 2022 年 3 月 15 日创建(v2 .0.1)

If I fully understand your request, please find below one possible solution.

Reprex

library(terra)

# Build a dummy Spatraster with two layers containing only 1 and 0
set.seed(0) # for reproducibility
r <- rast(nrows=10, ncols=10, nlyrs=2)
values(r) <- sample(c(1,0), 200, replace = TRUE)

r
#> class       : SpatRaster 
#> dimensions  : 10, 10, 2  (nrow, ncol, nlyr)
#> resolution  : 36, 18  (x, y)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 
#> source      : memory 
#> names       : lyr.1, lyr.2 
#> min values  :     0,     0 
#> max values  :     1,     1


# If you use terra::freq(), you get the number of cells with 0 and 1 for each layer
terra::freq(r)
#>      layer value count
#> [1,]     1     0    52
#> [2,]     1     1    48
#> [3,]     2     0    47
#> [4,]     2     1    53

# So, one approach is to sum the two layers and retrieve the number of cells with 2
# (here 24 cells have the value 1 in the two layers)

terra::freq(sum(r))
#>      layer value count
#> [1,]     1     0    23
#> [2,]     1     1    53
#> [3,]     1     2    24

# OR more directly:
terra::freq(sum(r), value = 2)[, 'count']
#> count 
#>    24

Created on 2022-03-15 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文