# 7.3 Landscape Metrics within buffers

1. Exercise 7.3 - Download and extract zip folder into your preferred location
2. Set working directory to the extracted folder in R under File - Change dir...
3. First we need to load the packages needed for the exercise

library(SDMTools)
library(raster)
library(rgeos)
library(plyr)

4. Now open the script "PatchAnalystScript.R" and run code directly from the script

5. Load vegetation raster layer textfile clipped in ArcMap

crops <-raster("crop2012utm12.tif")
plot(crops)
class(crops)
as.matrix(table(values(crops)))
proj4string(crops)

# reclassify the values into 9 groups
# all values between 0 and 20 equal 1, etc.
m <- c(-Inf,0,NA,2, 7, 2, 20, 60, 3, 60, 70, 4, 110, 132, 5, 133, 150, 6, 151, 172, 7, 180, 183, 8, 189, 191, 9,192,Inf,NA)
rclmat <- matrix(m, ncol=3, byrow=TRUE)
rc <- reclassify(crops, rclmat)
plot(rc)
rc
as.matrix(table(values(rc)))

6. The code above looks at patch and class metrics for the entire study area or within large polygons. However, what if we wanted to compare difference in landscape metrics among all deer? The question is how do you we want to go about doing this? We could run metrics within the home range of each animal or within a buffered circle for each location. To begin this, let’s return to our mule deer dataset and import the locations, cleanup, and make buffers around each location.

summary(muleys\$id)
#Remove outlier locations
newmuleys <-subset(muleys, muleys\$Long > -110.90 & muleys\$Lat > 37.80)
muleys <- newmuleys
newmuleys <-subset(muleys, muleys\$Long < -107)
muleys <- newmuleys
muleys\$GPSFixTime<-as.POSIXct(muleys\$GPSFixTime, format="%Y.%m.%d%H:%M:%S")

7. Here we can use code to create a function using the using the plyr package that will let us select all location for each deer, create buffers around each deer, and then run landscape metrics within these merged buffers.

buff3rd <- function(muleys) {
coords<-data.frame(x = muleys\$X, y = muleys\$Y)
deer.spdf <- SpatialPointsDataFrame(coords=coords, data = muleys,
proj4string = CRS("+proj=utm +zone=12 +datum=NAD83 +units=m +no_defs
+datum=GRS80 +towgs84=0,0,0"))
settbuff <- gBuffer(deer.spdf, width=1000, byid=FALSE)
buffclip <- mask(rc, settbuff)
buff.data <- PatchStat(buffclip)
newline <- muleys\$id
bind <-cbind(newline[1], buff.data)
}

results <- dlply(muleys, .(id), buff3rd)
results
class(results)

#Now convert results of class "List" to class "data frame"
df <- do.call(rbind.data.frame, results)
df
"Export to a table if needed"
write.table(df, "FragCombined.txt")

"Read back into R if needed
data

8. The code above looks at patch and class metrics for each deer by combining all buffers into one polygon for each deer (i.e., to define available habitat in 3rd order selection). However, what if we wanted to compare difference in patch statistics among all deer by averaging metrics across buffers?

#First we need to create buffers then re-assign a new ID for each deer #and each buffer generated. We then can apply the function to our dataset.

coords<-data.frame(x = muleys\$X, y = muleys\$Y)
deer.spdf <- SpatialPointsDataFrame(coords=coords, data = muleys,
proj4string = CRS("+proj=utm +zone=12 +datum=NAD83 +units=m +no_defs +datum=GRS80 +towgs84=0,0,0"))
setbuff <- gBuffer(deer.spdf, width=1000, byid=TRUE)
setbuff
muleys\$newID <- paste(muleys\$id, setbuff@plotOrder, sep="_")

buff3rdA <- function(muleys) {
bufclip <- mask(rc, setbuff)
buf.data <- PatchStat(bufclip)
}

results2 <- ddply(muleys, .(newID), buff3rdA)
results2