I am trying to extract values from a rasterstack and append those to an existing dataframe. The values are a collection of metrics (PatchStat from r package SDMtools) which I am able to extract into list format, but I am stuck trying to bind the values to my existing dataframe.
Input data:
library(sp)
library(sf)
library(raster)
library(dplyr)
library(SDMTools)
mydata <- read.table(header=TRUE, text = "
animal X Y ord.year
1 pb_20414 157978.9 2323819 2009168
2 pb_20414 156476.3 2325586 2009168
3 pb_06817 188512.0 2299679 2006263
4 pb_06817 207270.9 2287248 2006264")
# add rasters
s <- stack(system.file("external/rlogo.grd", package="raster"))
names(s) <- c('masie_ice_r00_v01_2009168_4km', 'masie_ice_r00_v01_2006263_4km', 'masie_ice_r00_v01_2006264_4km')
# Create sp object
projection <-CRS('+proj=stere +lat_0=90 +lat_ts=60 +lon_0=-80 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +units=m + datum=WGS84 +no_defs +towgs84=0,0,0') # matches MASIE raster
coords <- cbind(mydata$X, mydata$Y)
mydata.sp <- SpatialPointsDataFrame(coords = coords, data = mydata, proj4string = projection)
# Create sf object
mydata.sf <- st_as_sf(mydata)
mydata.buf30 <- st_buffer(mydata.sf, 30000)
My goal is to match each GPS point (X,Y) with the correct GeoTIFF by date (mydata$ord.year
), crop the raster to a (spatially explicit) 30 km buffer, run PatchStat in program SDMtools for R, and append the results to the original dataframe. The catch is that PatchStat results are provided in a dataframe, so I am having trouble matching those results to my existing dataframe.
Here is an example of results provided when I run PatchStat:
patchID n.cell n.core.cell n.edges.perimeter n.edges.internal area core.area perimeter
2 3 73 13 86 206 73 13 86
perim.area.ratio shape.index frac.dim.index core.area.index
2 1.178082 2.388889 1.430175 0.1780822
Here is what I have been able to do so far:
# separate date component of TIF name to correspond to mydata$ord.year
stack <- list()
date<-vector()
for (i in 1:length(rasterlist)) {
stack[[i]]<-raster(rasterlist[i])
tt<-unlist(strsplit(names(stack[[i]]), "[_]"))
date[i]<-tt[which(nchar(tt)==max(nchar(tt)))]
}
st <- stack(stack) # Create rasterstack object
# crop raster to buffer
mydata.sp <- as(mydata.sf, 'Spatial') # back to sp object
# pull raster data from GeoTIFF that corresponds to ordinal date
pat <- list()
for (i in 1:nrow(mydata.sp)) {
st2<-st[[which(date==mydata.sp$ord.year[i])]]
GeoCrop <- raster::crop(st2, mydata.sp[i,])
GeoCrop_mask <- raster::mask(GeoCrop, mydata.sp[i,])
pat[[i]] <- PatchStat(GeoCrop_mask)}
Additionally, I have eliminated one of the two land cover types so that each element in the list has only one row:
pat2 <- lapply(pat, `[`, -1,) # remove first row in each list element so only one row remains (using program plyr for R)
Now, I would like to match these rows to my original dataframe, so that pat2[[1]]
is appended to mydata.sp[1,]
like this (assuming a,b, and c are columns of metadata within my original SpatialPointsDataFrame). I would like all the columns of data from PatchStat added but to save time and space, I only included the first three here:
a b c PatchID n.cell n.core.cell
1 2 3 3 73 13
Note: If possible, I would love for this whole process to be included in the for
loop to minimize room for error and also processing time.
Thanks so much!