简体   繁体   English

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

[英]Extract values using several functions from raster

I am using the code from the solution in this post:我在这篇文章中使用了解决方案中的代码:

How to pass multiple function in raster::extract() 如何在 raster::extract() 中传递多个 function

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 is the stack raster which has the values I want to extract using polygons. Stack_for_Classification 是堆栈栅格,它具有我想使用多边形提取的值。 TrVal is the shapefile with polygons. TrVal 是带有多边形的 shapefile。 It has a field "Type" with three identity values which I am using to see the differences among the three different types according to the values extracted.它有一个“类型”字段,其中包含三个标识值,我用它来根据提取的值查看三种不同类型之间的差异。 The question is that with the previous code I get the mean, max, min and sd with a suffix of "0,1,2".问题是,使用前面的代码,我得到了后缀为“0,1,2”的平均值、最大值、最小值和标准差。 In total, 12. Can I use the field "Type" for naming the rows, such as "mean_Type1", "max_Type2".. and so on?总共12个。我可以使用字段“Type”来命名行,例如“mean_Type1”,“max_Type2”..等等? I upload an image after using View(extraction)我在使用 View(extraction) 后上传了一张图片

View(extraction) result查看(提取)结果

You can do something like this:你可以这样做:

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)

but I don't think it is possible to attach this data.frame to the @data slot of the polygons, because you have more observation than polygon features.但我认为不可能将此 data.frame 附加到多边形的@data插槽,因为您的观察比多边形特征更多。 You have to reshape the data to a wide format to merge the two database您必须将数据重塑为宽格式才能合并两个数据库

If "Type" is an attribute of the polygons, these are represented as rows.如果“类型”是多边形的属性,则这些表示为行。 With a single layer you can do单层你可以做

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)

But this does not work for multiple layers (in a RasterStack);但这不适用于多层(在 RasterStack 中); so you would have to loop over the layers.所以你必须遍历这些层。

With terra version 1.2-1 (currently the development version ), you can do使用terra版本 1.2-1(目前是开发版),你可以做

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

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

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