将地理坐标转换为十进制值

时间:2013-12-12 20:51:10

标签: regex bash coordinates

我有一个csv文件,其中包含地理名称和地理坐标。这是一个例子:

"Afghanistan","33 00 N","65 00 E"
"Akrotiri","34 37 N","32 58 E"
"Albania","41 00 N","20 00 E"
"Algeria","28 00 N","3 00 E"
"American Samoa","14 20 S","170 00 W"
"Andorra","42 30 N","1 30 E"
"Angola","12 30 S","18 30 E"
"Korea, North","40 00 N","127 00 E"
"Korea, South","37 00 N","127 30 E"

解剖第一行,我们有名称(阿富汗),纬度(33°00'N)和经度(65°00'E)。我正在寻找的结果如下:

"Afghanistan","33","65"
"Akrotiri","34.62","32.97"
"Albania","41","20"
"Algeria","28","3"
"American Samoa","-14.34","-170"
"Andorra","42.5","1.5"
"Angola","-12.5","18.5"
"Korea, North","40","127"
"Korea, South","37","127.5"

它只是携带整个度数,将分钟数加上小数(除以60和圆)并使用该字符决定结果值是正数(N,E)还是负数(S,W)。

  • 度数纬度范围为0-90,
  • 度经度范围为0-180和
  • 两种情况下的
  • 分钟范围为0-59

我尝试了一套非常有限的正则表达式知识,但却失败了。我甚至没有接近有用的东西。

这就是为什么我转向你的正则大师,请与我分享你的一些知识。

谢谢

1 个答案:

答案 0 :(得分:3)

使用awk:

awk -F'",' '{
   gsub(/"/, "", $2);
   gsub(/"/, "", $3);
   split($2, a, " ");
   split($3, b, " ");
   s=a[3]=="N"? "":"-";
   t=b[3]=="E"? "":"-";
   printf "%s\",\"%s%.2f\",\"%s%.2f\"\n", 
       $1, s,(a[1]+a[2]/60), t,(b[1]+b[2]/60)
}' file

"Afghanistan","33.00","65.00"
"Akrotiri","34.62","32.97"
"Albania","41.00","20.00"
"Algeria","28.00","3.00"
"American Samoa","-14.33","-170.00"
"Andorra","42.50","1.50"
"Angola","-12.50","18.50"
"Korea, North","40.00","127.00"
"Korea, South","37.00","127.50"