将坐标从非常规格式的度转换为十进制度 [英] Converting coordinates from degree with unconventional format to decimal degree

查看:191
本文介绍了将坐标从非常规格式的度转换为十进制度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换我的数据,以便可以将其绘制在地图上。例如,数据看起来像:

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), "°")

哪个返回和错误:

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

我想在ggplot(或其他空间包)中的地图上绘制这两个点

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

数据:

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))


推荐答案

您可以使用以下自定义函数(我假设 N S W E 。不确定 O 在经度中是什么意思):

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))

参考将地理坐标从度转换为十进制

这篇关于将坐标从非常规格式的度转换为十进制度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆