Skip to content

Instantly share code, notes, and snippets.

@btupper
Created November 28, 2016 15:17
Show Gist options
  • Save btupper/609b9fc79f5df557a53156e183d49b44 to your computer and use it in GitHub Desktop.
Save btupper/609b9fc79f5df557a53156e183d49b44 to your computer and use it in GitHub Desktop.
Access the cs2cs application from R
#' Access the cs2cs function from R
#'
#' @seealso http://proj4.org/index.html
#'
#' @param x numeric or character input value for x (longitude)
#' @param y numeric or character input value for y (latitude)
#' @param from character input coordinate system definition
#' @param to character output coordinate system definition
#' @param extra character extra arguments for cs2cs
#' @param app character the fully qualified path to the cs2cs executable
#' @param charcater string of one or more elements
cs2cs <- function(x = -70.2, y = 43.8,
from = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0",
to = "+proj=lcc +lon_0=-107 +lat_0=50 +x_0=5632642.22547 +y_0=4612545.65137 +lat_1=50 +lat_2=50 +ellps=WGS84",
extra = '-w3',
app = Sys.which('cs2cs')){
if (!file.exists(app[1])) stop("Application cs2cs not found")
ff <- tempfile()
cat(x, " ", y, "\n", sep = "", file = ff)
CMD <- paste(app, extra, from, "+to ", to, ff)
cat(CMD, "\n")
r <- system(CMD, intern = TRUE)
unlink(ff)
strsplit(r, '[\t ]')[[1]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment