R/get_coordinates.R

Defines functions get_coordinates

Documented in get_coordinates

#' get coordinates from a variety of different object classes
#' 
#' @param x coordinates. \code{sf} 'POINT' or 'MULTIPOINT', \code{SpatVector}, \code{data.frame} or \code{matrix} containing the locations coordinates
#' 
#' @author Joseph Lewis
#' 
#' @return \code{matrix} matrix of coordinates
#' 
#' @export

get_coordinates <- function(x) { 
  
  if(inherits(x, "sf")) { 
    coords <- sf::st_coordinates(x)[, 1:2, drop = FALSE]
  }
  else if (inherits(x, "SpatVector")) { 
    coords <- terra::crds(x)
  }
  else if (inherits(x, "data.frame")) { 
    coords <- as.matrix(x)
  }
  else if (inherits(x, "matrix")) { 
    coords <- x
  }
  else if (inherits(x, "numeric")) { 
    coords <- matrix(x, nrow = 1)
  }
  
  return(coords)
  
}

Try the leastcostpath package in your browser

Any scripts or data that you put into this service are public.

leastcostpath documentation built on Oct. 10, 2023, 1:06 a.m.