R/sp_bounding_box.R

Defines functions sp_bounding_box

Documented in sp_bounding_box

#' Function to extract bounding box from a spatial or raster object. 
#' 
#' @author Stuart K. Grange
#' 
#' @param sp Spatial or raster object object. 
#' 
#' @return Numeric vector with length of four. Coordinates order is: xmin, xmax, 
#' ymin, ymax. 
#' 
#' @export
sp_bounding_box <- function(sp) {
  
  # Extents will be different type of objects for the different 
  if (is.sp(sp)) {
    
    # Get boundary
    x <- sp@bbox
    
    # Make a vector, xmin, xmax, ymin, ymax
    x <-  c(x[1, 1], x[1, 2], x[2, 1], x[2, 2])
    
  } else if (is.ra(sp)) {
    
    # Get boundary and make vector
    x <- raster::extent(sp)
    x <- as.vector(x)
    
  } else {
    
    stop("Data type not recognised...", call. = FALSE)
    
  }
  
  return(x)
  
}
skgrange/gissr documentation built on Feb. 24, 2024, 2:55 p.m.