Skip to content

Instantly share code, notes, and snippets.

@codegordi
Created October 5, 2012 21:48
Show Gist options
  • Save codegordi/3842618 to your computer and use it in GitHub Desktop.
Save codegordi/3842618 to your computer and use it in GitHub Desktop.
a simple coordinate conversion function
### a simple coordinate conversion function to convert latitude, longitude in DMS to decimal-degress
### christina gutierrez aka github@codegordi
#library(gsubfn)
require(gsubfn)
options(digits=15)
# point.set == data set with at least latitute and longitude in degrees-(arc)minutes-seconds[-fractional seconds];
# no projection or datum assumed;
# format of DD[D]:MM:SS[*] is assumed
# e.g. coords <- c("122:45:45", "-69:38:27")
# e.g coords <- c("122:45:45.84", "-69:38:27.76")
x <- point.set$LON.DMS # set x - LONGITUDE (in separate line here for demonstration)
y <- point.set$LAT.DMS # set y - LATITUDE (in separate line here for demonstration)
# define function
fxn <- function(d, m, s, dd = as.numeric(d), mm = as.numeric(m), ss = as.numeric(s)) sign(dd) * (abs(dd) + mm / 60 + ss / 3600)
# convert coodinates and append to data frame
point.set$LON.DEC <- strapply(x, "(.*):(.*):(.*)", fxn, simplify = TRUE)
point.set$LAT.DEC <- strapply(y, "(.*):(.*):(.*)", fxn, simplify = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment