#' Extend Raster
#'
#' extend_raster will extend a Raster* object by identifying each point which is NA based on a set of provided co-ordinates.
#' It will then find the nearest non-NA cell and extend the raster so that no observation records sit on NA pixels.
#' This function is best employed on islands where species' observation records may not overlap with an underlying climate layer (e.g. due to resolution differences).
#'
#' @param xy xy coordinates specifying cell centroids. Ensure that column headings are lower case and labelled as longitude/latitude or lat/long or xy.
#' @param raster Raster which will be extended
#' @param output.table Logical. If output.table=TRUE then a data.frame will be displayed instead of a raster. If output.table=FALSE then an extended raster will be displayed. Default is output.table=FALSE
#' @param fact Integer. This argument disaggregates raster* (i.e. increases the reshighres.split<-split.raster(raster=global_climate[[1]], ppside = 10, save = TRUE, plot=FALSE, raster.name="highres_split_")
#' @param only.na Logical. If TRUE, the function will only calculate new values for NA cells. When TRUE, processing time is greatly reduced.
#' @keywords None.
#' @export
#' @examples Coming soon.
extend_raster<-function(xy, raster, only.na=TRUE, output.table=FALSE, fact = 1){
# Checks if raster contains only NA cells
# If raster is all NA, disaggregates so that it has the correct resolution and returns highres raster
if (is.na(maxValue(raster)) == TRUE){
blank.raster<-disaggregate(raster, fact=fact)
message("NOTE: This raster contains only NA cells (i.e. it's water). Raster resolution set to fact=", paste0(fact), ".")
cat("Processing has completed successfully.")
return(blank.raster)
}
else raster<-raster
# Extend raster function if the raster contains land
if (is.na(maxValue(raster)) == FALSE){
# If land is detected then:
# Check to only include xy coordinates in data frame
if(ncol(xy) >2){
xy<-xy[ , which(names(xy) %in% c("longitude","latitude","long", "lat", "x","y"))]
}
else xy<-xy
if (only.na==TRUE){
points.na<-which(!is.na(extract(raster[[1]], xy)))
indx <- !(seq_len(nrow(xy)) %in% points.na)
xy <- xy[indx,, drop=T]
#row.names(xy) <- seq_len(nrow(xy))
}
else xy<-xy
if (nrow(xy) == 0){
blank.raster<-disaggregate(raster, fact=fact)
message("NOTE: This raster is either 100% land or contains zero NA cells; there is nothing to extend. Raster resolution set to fact=", paste0(fact), ".")
cat("Processing has completed successfully.")
return(blank.raster)
}
else
raster<-raster
highres<-disaggregate(raster, fact=fact)
r<-highres
extracted<-extract(r,xy, method="simple")
sampled<-apply(X = xy, MARGIN = 1, FUN = function(xy) r@data@values[which.min(replace(distanceFromPoints(r, xy), is.na(r), NA))])
sampled<-lapply(sampled, FUN = "mean")
sampled<-unlist(sampled)
df<-data.frame(xy)
df2<-data.frame(xy,sampled)
df3<-data.frame(xy,extracted,sampled)
extended.raster<-rasterize(df, r, field=df2$sampled, update=TRUE)
# Output table containing xy coordinates, the extracted value (i.e. this will be NA) and the new, extended value
# Useful if running across nodes as this will transfer a binary dataframe rather than a raster object. Can be later rasterized
if(output.table==TRUE){
message("Output saved as a dataframe. Set output.table=FALSE to return a raster image.")
cat("Processing has completed successfully.")
df3
}
# Returns a raster object
else return(extended.raster)
cat("Processing has completed sucessfully. Output saved as raster image.")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.