Search code examples
rdataframedplyrstringrgeo

Converting coordinates from degree with unconventional format to decimal degree


I am trying to convert my data so that it can be plotting on a map. For example the data looks like:

# A tibble: 2 x 2
  Latitud           Longitud        
  <chr>             <chr>           
1 10º 35' 28.98'' N 3º 41' 33.91'' O
2 10º 35' 12.63'' N 3º 45' 46.22'' O

I am trying to mutate it using the following:

df %>% 
  mutate(
    Latitud = str_replace_all(Latitud, "''", ""),
    lat_edit = sp::char2dms(Latitud), "°")

Which returns and error:

Error in if (any(abs(object@deg) > 90)) return("abs(degree) > 90") : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In asMethod(object) : NAs introduced by coercion

I would like to plot these two points on a map in ggplot (or another spatial package)

Data:

structure(list(Latitud = c("40º 25' 25.98'' N", "40º 25' 17.63'' N"
), Longitud = c("3º 42' 43.91'' O", "3º 40' 56.22'' O")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -2L))

Solution

  • You can use the following custom function (I am assuming N, S, W, E. Not sure what O means in longitude):

    angle2dec <- function(angle) {
      angle <- as.character(angle)
      angle <- ifelse(grepl("S|W", angle), paste0("-", angle), angle)
      angle <- trimws(gsub("[^- +.0-9]", "", angle))
      x <- do.call(rbind, strsplit(angle, split=' '))
      x <- apply(x, 1L, function(y) {
        y <- as.numeric(y)
        (abs(y[1]) + y[2]/60 + y[3]/3600) * sign(y[1])
      })
      return(x)
    }
    

    Applying on the data:

    df1[] <- lapply(df1, angle2dec)
    
    df1
    #>     Latitud  Longitud
    #> 1 -40.42388  3.712197
    #> 2  40.42156 -3.682283
    

    Plotting:

    library(ggplot2)
    
    ggplot(df1, aes(x = Longitud, y = Latitud)) +
      geom_point()
    


    Slightly Modified Data to Show for Different Hemispheres:

    df1 <- structure(list(Latitud = c("40<U+623C><U+3E61> 25' 25.98'' S", 
                                      "40<U+623C><U+3E61> 25' 17.63'' N"), 
                          Longitud = c("3<U+623C><U+3E61> 42' 43.91'' E",
                                       "3<U+623C><U+3E61> 40' 56.22'' W")), 
                     class = c("tbl_df", "tbl", "data.frame"), 
                     row.names = c(NA, -2L))
    

    In reference to Converting geo coordinates from degree to decimal .