Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将地理坐标从度转换为十进制_R_Gis - Fatal编程技术网

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

R 将地理坐标从度转换为十进制,r,gis,R,Gis,我想将我的地理坐标从度转换为小数,我的数据如下: 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 我有这个代码,但我不明白为什么它不起作用: convert<-function(coo

我想将我的地理坐标从度转换为小数,我的数据如下:

         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
我有这个代码,但我不明白为什么它不起作用:

convert<-function(coord){
tmp1=strsplit(coord,"°")
tmp2=strsplit(tmp1[[1]][2],"\\.")
dec=c(as.numeric(tmp1[[1]][1]),as.numeric(tmp2[[1]]))
return(dec[1]+dec[2]/60+dec[3]/3600) 
} 
don_convert=don1
for(i in 1:nrow(don1)){don_convert[i,2]=convert(as.character(don1[i,2]));              don_convert[i,3]=convert(as.character(don1[i,3]))}

convert一点矢量化和矩阵操作将使您的函数更简单:

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
试试看:

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


免责声明:我没有检查你的数学。请自行决定使用。

尝试使用
sp
库中的
char2dms
功能。它还具有其他功能,可以额外进行十进制转换

library("sp")
?char2dms

正如Jim Lewis之前所评论的,似乎您使用的是浮点分钟数。然后,您只需在上连接两个元素

dec=c(作为.numeric(tmp1[[1]][1]),作为.numeric(tmp2[[1]]))

43°21'8.02形式的度数、分和秒
as.character()
返回“43°21'8.02\”,我将您的函数更新为

convert<-function(coord){
  tmp1=strsplit(coord,"°")
  tmp2=strsplit(tmp1[[1]][2],"'")
  tmp3=strsplit(tmp2[[1]][2],"\"")
  dec=c(as.numeric(tmp1[[1]][1]),as.numeric(tmp2[[1]][1]),as.numeric(tmp3[[1]]))
  c<-abs(dec[1])+dec[2]/60+dec[3]/3600
  c<-ifelse(dec[1]<0,-c,c)
  return(c)
}

convert另一个不太优雅的选项,使用子字符串而不是strsplit。这仅在所有位置的位数相同时才有效。对于负坐标,只需乘以-1即可获得正确的小数位数

x$LatDD<-(as.numeric(substring(x$lat, 1,2))
+ (as.numeric(substring(x$lat, 4,9))/60))
x$LongDD<-(as.numeric(substring(x$long, 1,1))
       + (as.numeric(substring(x$long, 3,8))/60))

x$LatDD使用CRAN提供的
测量
软件包,该软件包已具备单位转换功能,因此您无需自行制作:

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)
设置data.frame后,请执行以下操作:

# change the degree symbol to a space
x$lat = gsub('°', ' ', x$lat)
x$long = gsub('°', ' ', x$long)

# convert from decimal minutes to decimal degrees
x$lat = measurements::conv_unit(x$lat, from = 'deg_dec_min', to = 'dec_deg')
x$long = measurements::conv_unit(x$long, from = 'deg_dec_min', to = 'dec_deg')
最终产品:

                    lat             long
105252 30.4210666666667 9.02218333333333
105253         30.65395 8.18018333333333
105255 31.6293333333333 8.10066666666667
105258          31.6865 8.10928333333333
105259         31.68715 8.11036666666667
105260 31.6481833333333 8.10468333333333

谢谢@Gord Stephen和@CephBirk的回答。确实帮了我的忙。 我想我应该提到,我还发现
measurements::conv_unit
不处理“E/W”“N/S”条目,它需要正/负度数。 我的坐标是字符串
“1 1 1W”
,需要首先转换为
“-1 1”

我想我会分享我的解决方案

df <- c("1 1 1E", "1 1 1W", "2 2 2N","2 2 2S")  
measurements::conv_unit(df, from = 'deg_min_sec', to = 'dec_deg')
[1] "1.01694444444444" NA                 NA                 NA  
Warning message:
In split(as.numeric(unlist(strsplit(x, " "))) * c(3600, 60, 1),  :
  NAs introduced by coercion

ewns <- ifelse( str_extract(df,"\\(?[EWNS,.]+\\)?") %in% c("E","N"),"+","-")
dms <- str_sub(df,1,str_length(df)-1)
df2 <- paste0(ewns,dms)

df_dec <- measurements::conv_unit(df2, 
                                  from = 'deg_min_sec', 
                                  to = 'dec_deg'))
df_dec
[1] "1.01694444444444"  "-1.01694444444444" "2.03388888888889"  "-2.03388888888889"
as.numeric(df_dec)
[1]  1.016944 -1.016944  2.033889 -2.033889

df查看包
OSMscale
中的命令
degree

转换函数似乎需要度、分和秒,但您的输入似乎有度和浮点分,没有秒字段。而且它似乎无法正确处理负值。不确定e不过,这些问题中的一个与您的问题有关。这可能是最简单的解决方案(您还可以免费获得其他坐标转换)。我想补充的是,该功能现在已被移动到
measurements
包中,并且在
birk
中被弃用。这是一个对我更有效的答案。非常感谢!请查看帮助文件中的一些示例:```d将输出包装为.numeric()
,如果您想转换类似
的内容35d8'2.222 \“E”
转换为
35.13395
。您需要罗盘的尾随方向。正如问题注释所述,输入是度和十进制分钟,而不是度、分和秒。如果仅在
°
上拆分并删除
z[3,]/3600
部分,则此答案有效。
df <- c("1 1 1E", "1 1 1W", "2 2 2N","2 2 2S")  
measurements::conv_unit(df, from = 'deg_min_sec', to = 'dec_deg')
[1] "1.01694444444444" NA                 NA                 NA  
Warning message:
In split(as.numeric(unlist(strsplit(x, " "))) * c(3600, 60, 1),  :
  NAs introduced by coercion

ewns <- ifelse( str_extract(df,"\\(?[EWNS,.]+\\)?") %in% c("E","N"),"+","-")
dms <- str_sub(df,1,str_length(df)-1)
df2 <- paste0(ewns,dms)

df_dec <- measurements::conv_unit(df2, 
                                  from = 'deg_min_sec', 
                                  to = 'dec_deg'))
df_dec
[1] "1.01694444444444"  "-1.01694444444444" "2.03388888888889"  "-2.03388888888889"
as.numeric(df_dec)
[1]  1.016944 -1.016944  2.033889 -2.033889