#' Convert EPSG codes to CRS objects
#'
#' Creates a proper CRS S4 object from a valid EPSG code given as an integer.
#'
#' @param x Integer.
#'
#' @return An object of class \linkS4class{CRS}.
#'
#' @examples
#' epsg_to_crs(3347)
#' epsg_to_crs(4326)
#'
#' @import rgdal
#' @importFrom methods new
#'
#' @export
epsg_to_crs <- function(x)
{
stopifnot(is.numeric(x))
x <- as.integer(x)
uprojargs <- paste0("+init=epsg:", x)
projargs <- checkCRSArgs(uprojargs)
if (projargs[[1L]]) {
res <- new("CRS", projargs = projargs[[2L]])
} else {
stop("no PROJ.4 parameters found for EPSG:", x, ".",
call. = FALSE)
}
res
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.