Converting geo coordinates from degree to decimal

后端 未结 7 929
滥情空心
滥情空心 2020-11-28 10:48

I want to convert my geographic coordinates from degrees to decimals, my data are as follows:

         lat     long
105252 30°25.264 9°01.331
105253 30°39.23         


        
7条回答
  •  时光说笑
    2020-11-28 11:11

    A bit of vectorization and matrix manipulation will make your function much simpler:

    x <- read.table(text="
           lat     long
    105252 30°25.264 9°01.331
    105253 30°39.237 8°10.811
    105255 31°37.760 8°06.040
    105258 31°41.190 8°06.557
    105259 31°41.229 8°06.622
    105260 31°38.891 8°06.281",
                    header=TRUE, stringsAsFactors=FALSE)
    
    x
    

    The function itself makes use of:

    • strsplit() with the regex pattern "[°\\.]" - this does the string split in one step
    • sapply to loop over the vector

    Try this:

    convert<-function(x){
      z <- sapply((strsplit(x, "[°\\.]")), as.numeric)
      z[1, ] + z[2, ]/60 + z[3, ]/3600
    } 
    

    Try it:

    convert(x$long)
    [1] 9.108611 8.391944 8.111111 8.254722 8.272778 8.178056
    

    Disclaimer: I didn't check your math. Use at your own discretion.

提交回复
热议问题