R/bufferluse.R

Defines functions bufferluse

#' Create buffers and extract a series of landscape metrics
#' Input is a raster and a matrix of with site coordinates
#' @inrast = input raster
#' @inpoint = column containing species names
#' @bufsize = buffer radius size
#' @rastcell= cell size of the raster
#'

bufferluse<-function(inrast,inpoint,bufsize,rastcell){
    results<-NULL
    inpoint<-inpoint[inpoint$Islandnew %in% names(inrast),]
    # loop through sites
    for (i in 1:dim(inpoint)[1]){
      # subset points using Island info from raster list
      points <- inpoint[i,]
      # subset raster map using point info (Island name)
      tmpr<-inrast[c(as.character(unique(points$Islandnew)))][[1]]
        cat(paste("... Processing site",i," ...",sep=" "))
        cat("\n")
        # create buffer
        for(j in 1:length(bufsize)){
            cat(paste("... Buffer size",bufsize[j]," ...",sep=" "))
            cat("\n")
            pbuf <- gBuffer(points, width=bufsize[j])
                tmpr1<-crop(tmpr,pbuf)
                buf <- mask(tmpr, pbuf)
                buf1<-trim(buf)
                buf2<-rasterFromXYZ(rastertodf(buf1))
            #extract information from buffer
            tmp<-ClassStat(buf2,cellsize=rastcell)
            resdf<-data.frame(cat=tmp$class,prop=tmp$prop.landscape,Island=points@data$Islandnew,
                              Database.code=points@data$Database.code,size=bufsize[j])
                results<-rbind(resdf,results)
        } # end of j loop (buffer size)
    } # end of i loop (sites)
    # get results into a list
    results$cat<-paste("l_",results$cat,sep="")
    size.l<-unique(results$size)
    results1<-vector("list",length(size.l))
    names(results1)<-size.l
    for (i in 1:length(size.l)){
      # subset size
        tmp<-subset(results,size==size.l[i])
        # island list
        isl.l<-unique(tmp$Island)
        # temporary list containing results for each island
        isl.res<-vector("list",length(isl.l))
        names(isl.res)<-isl.l
        for (x in 1:length(isl.l)){
          tmp1<-subset(tmp,Island==isl.l[x])
          tmp1 %>%
            dplyr::select(-size) %>%
            tidyr::spread(cat,prop,fill=0) ->tmp2
          isl.res[[x]]<-tmp2
        }
        results1[[i]]<-isl.res
    }
    return(results1)
}
marcog77/Rmacdiv documentation built on March 20, 2018, 5:25 a.m.