简体   繁体   中英

Convert North and East coordinates to longitude and latitude in R

Not sure of all the geographic terms, but I am looking for a way in R to convert from a coordinate like this:

48° 26′ 5″ N, 7° 46′ 36″ E

to longitude and latitude. From geohack I know the answer for these coordinates will be...

48.434722, 7.776667

char2dms (character to degrees, minutes, seconds) should help you out

library(sp)

as.numeric(char2dms("48° 26' 5\"N", chd = "°", chm = "'", chs='"'))
# [1] 48.43472

as.numeric(char2dms("7° 46' 36\"E", chd = "°", chm = "'", chs='"'))
# [1] 7.776667

The arguments chd, chm and chs determine the characters that identify the degrees, minutes and seconds, respectively. The \\ character (called an escape character) is necessary in R to indicate that the " is part of the string.

You can use strsplit to separate the initial string into latitude and longitude.

pos <- "48° 26' 5\"N , 7° 46'  36\" E"
pos <- unlist(strsplit(pos, ","))
as.numeric(char2dms(pos, chd = "°", chm = "'", chs='"'))  
# [1] 48.434722  7.776667

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM