0
votes

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!

2
this is pretty straightforward but, as with all R questions, you need to provide some code with simple example data (created by code or from an R package) to be able to answer.Robert Hijmans
facepalm, noob here. I've edited accordingly.NorthLattitude

2 Answers

0
votes

Well I did this very ugly thing and got what I wanted. But I don't like it. If anyone has a better idea I'd love to hear it!

# Change objects to df  
pat2 <- lapply(pat, `[`, -1,) # remove first row in each list element

library(plyr) # ldply command

pat3 <- ldply (pat2, data.frame)

pat4 <- bind_cols(pb, pat3)
0
votes

Thanks for your effort to provide example data. But it is still incomplete (it refers to files that we do not have. You could to this

library(raster)
library(SDMTools)

s <- stack(system.file("external/rlogo.grd", package="raster")) 
s <- round(s / 50) # to have fewer patches
names(s) <- c('masie_ice_r00_v01_2009168_4km', 'masie_ice_r00_v01_2006263_4km', 'masie_ice_r00_v01_2006264_4km')

df <- data.frame(ord.year=c("2009168", "2009168", "2006263", "2006264"))
pts <- SpatialPoints(cbind(c(20,40,60,80), c(20,40,60,20)))
crs(pts) <- crs(s)
pts <- SpatialPointsDataFrame(pts, df)

Make a buffer

b <- buffer(pts, 15, dissolve=FALSE)

Get matching names

nms <- names(s)
nms <- gsub('masie_ice_r00_v01_', '', nms)
nms <- gsub('_4km', '', nms)

Loop to match names, and put results in a list

p <- list()
for (i in 1:length(b)) {
    j <- which(b$ord.year[i] == nms)
    r <- s[[j]]
    z <- crop(r, b[i,])
    z <- mask(z, b[i,])
    p[[i]] <- PatchStat(z)
}

Note that each element of p has a data.frame with multiple rows and columns.

 p[[1]]
 #patchID n.cell n.core.cell n.edges.perimeter n.edges.internal area core.area perimeter perim.area.ratio shape.index frac.dim.index core.area.index
 #1       1     53           5                68              144   53         5        68        1.2830189    2.266667       1.427207      0.09433962
 #2       2    123           8               182              310  123         8       182        1.4796748    3.956522       1.586686      0.06504065
 #3       3    149          31               190              406  149        31       190        1.2751678    3.800000       1.543074      0.20805369
 #4       4     54           2               114              102   54         2       114        2.1111111    3.800000       1.679578      0.03703704
 #5       5    337         206               146             1202  337       206       146        0.4332344    1.972973       1.236172      0.61127596

If you only want the first rows

 pp <- t(sapply(p, function(i) i[1,]))

Combining this with the orginal data.frame is now trivial

 dfpp <- cbind(df, pp)