R/epsg_to_crs.R

Defines functions epsg_to_crs

#' 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
}
jeanmathieupotvin/scr documentation built on Dec. 3, 2019, 8:53 p.m.