使用多个函数从栅格中提取值

时间:2021-02-15 09:02:31

标签: r extract

我正在使用这篇文章中解决方案中的代码:

How to pass multiple function in raster::extract()

functions_summ <- function(x, na.rm) c(mean = mean(x, na.rm=na.rm), min = min(x, na.rm=na.rm), max = max(x, na.rm=na.rm), sd = sd(x, na.rm = na.rm))

extraction <- extract( Stack_for_Classification,TrVal, fun=functions_summ, na.rm=TRUE, df = TRUE, sp = T) ## the sp = T. means that I am adding polygon values to the csv

nom <- sapply(TrVal@polygons, slot, "ID")

extraction <- data.frame(ID = nom, Value = extraction)

names(extraction) <- gsub(x = names(extraction), pattern = "\\Value.", replacement = "")  
typeof(extraction)

View(extraction)

Stack_for_Classification 是堆栈栅格,它具有我想使用多边形提取的值。 TrVal 是带有多边形的 shapefile。它有一个字段“类型”,其中包含三个标识值,我使用它来根据提取的值查看三种不同类型之间的差异。 问题是,使用前面的代码,我得到了后缀为“0,1,2”的均值、最大值、最小值和标准差。总共 12. 我可以使用字段“类型”来命名行,例如“mean_Type1”、“max_Type2”……等等? 我在使用 View(extraction) 后上传了一张图片

View(extraction) result

2 个答案:

答案 0 :(得分:0)

你可以这样做:

library(raster)

# simulate data -------------------------------------------------------------
r <- s <- t <- raster()
r[] <- 1:ncell(r)
s[] <- sample(1:10,ncell(s),replace = T)
t[] <- runif(ncell(t))

stacked <- stack(r,s,t)

#polys (from the SpatialPolygonsDataFrame example)
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3), "s3")
Srs4 = Polygons(list(Sr4), "s4")

SpP = SpatialPolygons(list(Srs1,Srs2,Srs3,Srs4), 1:4)
plot(SpP, col = 1:4, pbg="white")
types <- letters[1:4]
polys <- SpatialPolygonsDataFrame(SpP,
                               data = data.frame(
                                 types=types,
                                 row.names = row.names(SpP)
                               ))
                               #extract


plot(r)
plot(polys,add=T)

functions_summ <-
function(x, na.rm = T){
c(
  mean = mean(x, na.rm = na.rm),
  min = min(x, na.rm = na.rm),
  max = max(x, na.rm = na.rm),
  sd = sd(x, na.rm = na.rm)
)
}

ex <- extract(stacked,polys,fun=functions_summ,df=T)
rownames(ex) <- 
paste0(gsub("\\..","",rownames(ex)),"_",rep(unique(polys@data$types),each=4))
#convert rownames to column
ex$var <- rownames(ex)

但我认为不可能将此 data.frame 附加到多边形的 @data 插槽,因为您比多边形特征有更多的观察。您必须将数据重塑为宽格式才能合并两个数据库

答案 1 :(得分:0)

如果“类型”是多边形的一个属性,则这些多边形表示为行。单层就可以了

library(terra)
v <- shapefile(system.file("external/lux.shp", package="raster"))
r <- raster(v, res=.1)
values(r) <- 1:ncell(r)

f <- function(x, na.rm = T) {
       c(mean=mean(x, na.rm = na.rm),
        min=min(x, na.rm = na.rm),
        max=max(x, na.rm = na.rm),
        sd=sd(x, na.rm = na.rm)
    )
}
e <- extract(r, v, f)

x <- cbind(v, e)

但这不适用于多层(在 RasterStack 中);所以你必须在图层上循环。

使用 terra 1.2-1 版(当前为 development version),您可以做到

library(terra)
#terra version 1.2.1
r <- rast(system.file("ex/elev.tif", package="terra"))
x <- c(r, r/2)
names(x) <- c("a", "b")
v <- vect(system.file("ex/lux.shp", package="terra"))

f <- function(x, na.rm = T) {
       c(mean=mean(x, na.rm = na.rm),
        min=min(x, na.rm = na.rm),
        max=max(x, na.rm = na.rm),
        sd=sd(x, na.rm = na.rm)
    )
}

e <- extract(x, v, f)
head(e)
#  ID   a.mean a.min a.max     a.sd   b.mean b.min b.max     b.sd
#1  1 467.1052   339   547 34.58480 233.5526 169.5 273.5 17.29240
#2  2 333.8629   195   514 68.03058 166.9315  97.5 257.0 34.01529
#3  3 377.3712   256   517 77.14169 188.6856 128.0 258.5 38.57085
#4  4 373.6000   213   520 82.79129 186.8000 106.5 260.0 41.39564
#5  5 418.6490   293   511 48.35488 209.3245 146.5 255.5 24.17744
#6  6 314.9969   164   403 49.02558 157.4985  82.0 201.5 24.51279