Home > Mobile >  Why am I losing columns when extracting raster data with ```terra::extract```?
Why am I losing columns when extracting raster data with ```terra::extract```?

Time:04-21

I created a raster stack with rasters containing different vegetation measurements (i.e., canopy height, veg density). I extracted data from that raster stack to a SpatVector containing GPS points and corresponding data. The output contains the raster data, but does not contain any of the SpatVector data. Sample code below. I am not sure how to add the raster data to the question.

structure(list(Id = c("A1", "A1", "A1", "A1", "A1", "A1", "A1", 
"A1", "A1", "A1"), DateTime_Local = c("2019-06-18 14:00:00", 
"2019-06-18 14:30:00", "2019-06-18 15:00:00", "2019-06-18 15:30:00", 
"2019-06-18 16:00:00", "2019-06-18 16:30:00", "2019-06-18 17:00:00", 
"2019-06-18 17:30:00", "2019-06-18 18:00:00", "2019-06-18 18:30:00"
), Temp_C = c(23.484, 23.388, 23.196, 23.677, 24.738, 24.738, 
24.641, 26.097, 27.37, 28.357), Temp_F = c(74.2712, 74.0984, 
73.7528, 74.6186, 76.5284, 76.5284, 76.3538, 78.9746, 81.266, 
83.0426), Type = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Long = c(-97.47462153, 
-97.47462153, -97.47462153, -97.47462153, -97.47462153, -97.47462153, 
-97.47462153, -97.47462153, -97.47462153, -97.47462153), Lat = c(26.58459955, 
26.58459955, 26.58459955, 26.58459955, 26.58459955, 26.58459955, 
26.58459955, 26.58459955, 26.58459955, 26.58459955), Long.1 = c(651903.662642045, 
651903.662642045, 651903.662642045, 651903.662642045, 651903.662642045, 
651903.662642045, 651903.662642045, 651903.662642045, 651903.662642045, 
651903.662642045), Lat.1 = c(2941332.22211244, 2941332.22211244, 
2941332.22211244, 2941332.22211244, 2941332.22211244, 2941332.22211244, 
2941332.22211244, 2941332.22211244, 2941332.22211244, 2941332.22211244
)), row.names = c(NA, -10L), class = "data.frame")
BG_vect <- vect(BG.sf) #SF object containing GPS coordinates and point data

BG.extracted <- terra::extract(veg_stk, BG_vect, fun = mean)
summary(BG.extracted)

CodePudding user response:

I think all you need to do is merge the resulting data.framefrom terra::extract() back to your SpatVector object. I created a reproducible example with your data. Note, your spatial data only seems to include one point location, so I altered the "Lat" and "Long" columns with some randomly selected locations. I also assumed these data were in the WGS84 Lat/Long coordinate system (EPSG:4269). From that I created a fake raster of canopy height data.

library(terra)
library(sf)

spvect<-structure(list(Id = c("A1", "A1", "A1", "A1", "A1", "A1", "A1", 
                              "A1", "A1", "A1"), DateTime_Local = c("2019-06-18 14:00:00", 
                                                                    "2019-06-18 14:30:00", "2019-06-18 15:00:00", "2019-06-18 15:30:00", 
                                                                    "2019-06-18 16:00:00", "2019-06-18 16:30:00", "2019-06-18 17:00:00", 
                                                                    "2019-06-18 17:30:00", "2019-06-18 18:00:00", "2019-06-18 18:30:00"
                              ), Temp_C = c(23.484, 23.388, 23.196, 23.677, 24.738, 24.738, 
                                            24.641, 26.097, 27.37, 28.357), Temp_F = c(74.2712, 74.0984, 
                                                                                       73.7528, 74.6186, 76.5284, 76.5284, 76.3538, 78.9746, 81.266, 
                                                                                       83.0426), Type = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Long = c(-97.47462153, 
                                                                                                                                                  -97.47462153, -97.47462153, -97.47462153, -97.47462153, -97.47462153, 
                                                                                                                                                  -97.47462153, -97.47462153, -97.47462153, -97.47462153), Lat = c(26.58459955, 
                                                                                                                                                                                                                   26.58459955, 26.58459955, 26.58459955, 26.58459955, 26.58459955, 
                                                                                                                                                                                                                   26.58459955, 26.58459955, 26.58459955, 26.58459955), Long.1 = c(651903.662642045, 
                                                                                                                                                                                                                                                                                   651903.662642045, 651903.662642045, 651903.662642045, 651903.662642045, 
                                                                                                                                                                                                                                                                                   651903.662642045, 651903.662642045, 651903.662642045, 651903.662642045, 
                                                                                                                                                                                                                                                                                   651903.662642045), Lat.1 = c(2941332.22211244, 2941332.22211244, 
                                                                                                                                                                                                                                                                                                                2941332.22211244, 2941332.22211244, 2941332.22211244, 2941332.22211244, 
                                                                                                                                                                                                                                                                                                                2941332.22211244, 2941332.22211244, 2941332.22211244, 2941332.22211244
                                                                                                                                                                                                                                                                                   )), row.names = c(NA, -10L), class = "data.frame")
spvect$Long<-runif(nrow(spvect), -97.5, -96.5)
spvect$Lat<-runif(nrow(spvect), 26, 27)
BG.sf<-sf::st_as_sf(spvect, coords=c("Long", "Lat"), crs=4269)
BG.sf[,"Ind"]<-rownames(BG.sf)
BG.vect<-vect(BG.sf)
rst<-rast(extent=ext(BG.vect), nrow=100, ncol=100,  crs=crs(BG.vect))
values(rst)<-rnorm(10000, 100, 12)
names(rst)<-"Canopy Height"
extrctd<-extract(rst,BG.vect)
BG.Final<-terra::merge(BG.vect, extrctd, by.x="Ind", by.y="ID")
  • Related