[R] converting text coordinates to decimal degrees
Barry Rowlingson
B.Rowlingson at lancaster.ac.uk
Tue Jun 1 13:29:58 CEST 2004
Mikkel Grum wrote:
> I receive GPS readings in a text string such as
> "0121.6723S 03643.6893E" and need the coordinates as
> decimal degrees in two separate variables: "-1.361205"
> and "36.728155". How do I do this in R?
you'd use some string processing functions, like strsplit, nchar,
maybe grep and regexp, to build a function like this:
convertCoord <- function(coordString){
bits <- strsplit(coordString," ")
lat <- bits[[1]][1]
lon <- bits[[1]][2]
mdCon <- function(mdstring){
d <- as.numeric(substr(mdstring,1,2))
m <- as.numeric(substr(mdstring,3,99))/60
return(d+m)
}
latD <- mdCon(substr(lat,1,nchar(lat)-1))
latSign <- ifelse(substr(lat,nchar(lat),nchar(lat))=="N",1,-1)
lonD <- mdCon(substr(lon,1,nchar(lon)-1))
lonSign <- ifelse(substr(lon,nchar(lon),nchar(lon))=="E",1,-1)
c(lonD*lonSign, latD*latSign)
}
- this works for the single test case you've given us:
> convertCoord("0121.6723S 03643.6893E")
[1] 13.728155 -1.361205
- but lots of assumptions are coded into my function. Any deviation
from that precise format will break it!
Anyway, that should give you an idea.
Baz
More information about the R-help
mailing list