繁体   English   中英

如何从 RasterStack 中提取像元值并将其应用于新栅格

[英]How to extract cell values from RasterStack and apply them to a new raster

我正在尝试从 RasterStack 中获取 >=1 的所有单元格值并将它们应用于新的空白栅格。 我不确定是否通过 for 循环尝试这个,或者是否有办法通过简单的光栅计算来做到这一点。 到目前为止,我有以下内容:

dir <- "C://Users//path"
files<- list.files(path = dir,
                   full.names = TRUE, pattern = ".tif$")
raster_stack <- stack(files)
s <- subset(raster_stack, 38:48)

从这里开始,我不确定如何进行。 任何见解将不胜感激。 先感谢您。

library(raster)
#> Loading required package: sp
x1 <- raster(ncol=10, nrow=10, xmn=-10, xmx=10, ymn=-10, ymx=10)
x2 <- raster(ncol=10, nrow=10, xmn=-10, xmx=10, ymn=-10, ymx=10)
x1[,c(1,5,9)] <- 1
x2[c(1,5,9),] <- 1
plot(x1)

plot(x2)

s <- stack(c(x1, x2))
t <- sum(s)
plot(t)

如果您想添加特定图层,则:

t <- s$layer.1 + s$layer.2

代表 package (v2.0.1) 于 2022 年 1 月 25 日创建

使用这些示例数据

library(terra)
s <- rast(system.file("ex/logo.tif", package="terra"))
s <- round(s/100)

您可以这样做以获得 TRUE / FALSE 栅格

x <- s == 1 
plot(x)

或者这个来获得一个值(在这种情况下为 1)/ NA 光栅

y <- ifel(s==1, s, NA)

您也可以使用classify

z <- classify(s, cbind(1,1), othersNA=TRUE)

暂无
暂无

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

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