如何使用 terra 包将栅格堆栈中提取的值添加到 Spatial 对象的 data.frame 中?

问题描述

我想使用 terra 包将栅格堆栈中提取的值添加到空间对象的 data.frame

f <- system.file("ex/logo.tif",package="terra")
r <- rast(f)
#Plot the raster
plot(r,1:3)

#Create a vector file
points <- cbind.data.frame(Longitude = 40,Latitude = 40,val = 0.5)
points <- rbind.data.frame(points-1,points,points+1)
points_vect <- vect(points,geom=c("Longitude","Latitude"),crs=crs(r,proj=T),type = "points")

#Extract the values from raster using the point vector
terra::extract(r,points_vect,xy = T,method = "simple")

#>   ID red green blue  x  y
#> 1  1 255   255  253 30 30
#> 2  2 149   159  186 40 40
#> 3  3 146   156  207 50 50

正如您从输出 val 中看到的那样,我们过去常常使用 sp 包的 raster aurgument 来获取文件。现在如何在提取的 data.frame 中添加 val 字段?

解决方法

由于 terra::extract 按照向量中元素的顺序提取值,您可以简单地将字段 cbind 到提取的数据帧中,或者将所需字段添加为带有 $ 的列>

ex <- terra::extract(r,points_vect,xy = T,method = "simple")
ex$val <- points_vect$val

编辑

提取栅格值的最快选项是 exact_extract 包中的 exactextract,它有一个参数(称为 append_cols),用于将矢量字段附加到由萃取。 exact_extract 仅接受多边形数据(作为 sf 对象)作为输入,但您可以通过对点应用缓冲区并汇总值来解决此问题。看下面的例子

library(exactextractr)
library(sf)

point_sf <- st_as_sf(points_vect)
point_sf <- st_buffer(point_sf,1)
ex <- exactextractr::exact_extract(stack(r),point_sf,fun="mean",append_cols="val")
#you can summarise values using a vector of functions
ex <- exactextractr::exact_extract(stack(r),fun=c("mean","median","max","min"),append_cols="val")
,

只要是用点提取(或者用线和多边形的汇总函数),就可以用cbindterra

示例数据

f <- system.file("ex/logo.tif",package="terra")
r <- rast(f)
points <- data.frame(Longitude = 40,Latitude = 40,val = 0.5)
points <- rbind(points-1,points,points+1)
points_vect <- vect(points,geom=c("Longitude","Latitude"),crs=crs(r))
points_vect
# class       : SpatVector 
# geometry    : points 
# dimensions  : 3,3  (geometries,attributes)
# extent      : 39,41,39,41  (xmin,xmax,ymin,ymax)
# coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
# names       : Longitude Latitude   val
# type        :     <num>    <num> <num>
# values      :        39       39  -0.5
#                      40       40   0.5
#                      41       41   1.5

使用extract

v <- terra::extract(r,method = "simple")
v
#  ID red green blue  x  y
#1  1 247   254  255 39 39
#2  2 149   159  186 40 40
#3  3 132   141  182 41 41

并将提取的值 vpoints_vect 结合

x <- cbind(points_vect,v)
x     
# class       : SpatVector 
# geometry    : points 
# dimensions  : 3,9  (geometries,ymax)
# coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
# names       : Longitude Latitude   val    ID   red green  blue     x     y
# type        :     <num>    <num> <num> <num> <num> <num> <num> <num> <num>
# values      :        39       39  -0.5     1   247   254   255    39    39
#                      40       40   0.5     2   149   159   186    40    40
#                      41       41   1.5     3   132   141   182    41    41

或者像这样替换原来的值

values(points_vect) <- v
points_vect
# class       : SpatVector 
# geometry    : points 
# dimensions  : 3,6  (geometries,ymax)
# coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
# names       :    ID   red green  blue     x     y
# type        : <num> <num> <num> <num> <num> <num>
# values      :     1   247   254   255    39    39
#                   2   149   159   186    40    40
#                   3   132   141   182    41    41
    

或者像这样创建一个新的 SpatVector

newpts <- vect(v,c("x","y"),crs=crs(r))
newpts
# class       : SpatVector 
# geometry    : points 
# dimensions  : 3,ymax)
# coord. ref. : +proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs 
# names       :    ID   red green  blue     x     y
# type        : <num> <num> <num> <num> <num> <num>
# values      :     1   247   254   255    39    39
#                   2   149   159   186    40    40
#                   3   132   141   182    41    41