繁体   English   中英

为什么从 terra R package 相交 function 不给出所有组合?

[英]Why intersect function from terra R package not giving all the combinations?

我想计算两个分类栅格的每种可能组合下的面积。 我正在使用以下代码

library(terra)

#First create two rasters
r1 <- r2 <- rast(nrow=100, ncol=100)

#Assign random cell values
set.seed(123)
values(r1) <- runif(ncell(r1), min=0, max=1)
values(r2) <- runif(ncell(r2), min=0, max=1)

# classify the values into two groups
m_r1 <- c(min(global(r1, "min", na.rm=TRUE)), 0.2, 1,
               0.2, max(global(r1, "max", na.rm=TRUE)), 2)

m_r2 <- c(min(global(r2, "min", na.rm=TRUE)), 0.2, 1,
              0.2, max(global(r2, "max", na.rm=TRUE)), 2)

#Reclassify the rasters
rclmat_r1 <- matrix(m_r1, ncol=3, byrow=TRUE)
rc_r1 <- classify(r1, rclmat_r1, include.lowest=TRUE)

rclmat_r2 <- matrix(m_r2, ncol=3, byrow=TRUE)
rc_r2 <- classify(r2, rclmat_r2, include.lowest=TRUE)

plot(rc_r1)
plot(rc_r2)

#Convert to polygons
r1_poly <- as.polygons(rc_r1, dissolve=TRUE)
r2_poly <- as.polygons(rc_r2, dissolve=TRUE)
plot(r1_poly)
plot(r2_poly)

#Perform intersections
x <- intersect(r1_poly, r2_poly)
x
#> class       : SpatVector 
#> geometry    : polygons 
#> dimensions  : 2747, 2  (geometries, attributes)
#> extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 
#> names       : lyr.1 lyr.1
#> type        : <int> <int>
#> values      :     1     1
#>                   1     2
#>                   2     1

正如您从 output 中看到的那样,缺少一个组合,即 2-2。 为什么会这样? 当我尝试使用expanse(x)计算每个组合的面积时,它会返回一个很长的结果。 对于以下组合,如何获得以 km2 为单位的面积?

Combination Area (km2)
1-1
1-2
2-1
2-2 

对于此示例,最好保留栅格数据。

x = 10 * rc_r1 + rc_r2
a = cellSize(x, unit="km")
zonal(a, x, sum)
#  lyr.1      area
#1    11  19886611
#2    12  81946082
#3    21  84763905
#4    22 323469024
 

通过乘以 10,第一层中的值变为 10(如果它们是 1)或 20(如果它们是 2)。 如果您随后添加第二层,您将得到 10 + 1 或 2 和 20 + 1 或 2,因此您最终得到四个类别:11、12、21 和 22。这些显示第一个栅格中的值(第一个数字) 和第二个光栅(第二个数字)。

当您显示 SpatVector 时,仅打印前三个记录,并且有一个 2-2 记录。 然而,相交没有正常工作,我现在已经解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM