2015-10-01 78 views
1

我在寫一個基於EXIF標題重命名文件的bash腳本。 exiftool返回以下GPS位置字符串,我需要將其格式化爲緯度/經度座標,以便與Google Maps API一起使用。使用awk將GPS位置轉換爲經度和緯度

GPS Position : 40 deg 44' 49.36" N, 73 deg 56' 28.18" W 

谷歌地圖:

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611 

這是我的代碼

awk -v FS="[ \t]" '{print $0,substr($1,length($1),1)substr($2,length($2),1)}' $1 \ 
| sed 's/\xc2\xb0\([0-9]\{1,2\}\).\([NEWS]\)/ \1 0 \2/g;s/\xc2\xb0\([NEWS]\)/ 0 0 \1/g;s/[^0-9NEWS]/ /g' \ 
| awk '{if ($9=="NE") {printf ("%.4f\t%.4f\n",$1+$2/60+$3/3600,$5+$6/60+$7/3600)} \ 
else if ($9=="NW") {printf ("%.4f\t%.4f\n",$1+$2/60+$3/3600,-($5+$6/60+$7/3600))} \ 
else if ($9=="SE") {printf ("%.4f\t%.4f\n",-($1+$2/60+$3/3600),$5+$6/60+$7/3600)} \ 
else if ($9=="SW") {printf ("%.4f\t%.4f\n",-($1+$2/60+$3/3600),-($5+$6/60+$7/3600))}}' 

我得到這個錯誤:

sed: RE error: illegal byte sequence 

我需要的是一個有效的awk命令去除「deg」和NSEW文本,併除以3600而60%的這篇文章:

how to convert gps north and gps west to lat/long in objective c

40度44' 49.36" N,73度56' 28.18" W> 40.7470444,-073.9411611

請幫幫忙!

+0

爲什麼你在你的sed的模式,這些'\ xb0'和'\ xc2'擴展ASCII碼?你使用什麼版本的sed?你看看http://stackoverflow.com/questions/19242275/re-error-illegal-byte-sequence-on-mac-os-x? –

+0

EXIF輸出是否有「deg」或「°」? – srvsh

+0

答案就在這裏: http://stackoverflow.com/questions/18304358/convert-co-ordinate-dms-to-decimal-degrees-using-awk – utt50

回答

1

對於這種特殊情況,如果特殊字符是一個問題,我會按如下所示寫入。當然它的缺點是錯誤檢查不會那麼嚴格。

# remove the string till the colon and characters other than numbers, dots, spaces and NSEW 
sed 's!^.*:\|[^0-9\. NSEW]!!g' filename | 
# calculate the latitude and longitude with some error checks 
awk '/^\s*([0-9.]+\s+){3}[NS]\s+([0-9.]+\s+){3}[EW]\s*$/ { 
     lat=($1+$2/60+$3/3600); if ($4 == "S") lat=-lat; 
     lon=($5+$6/60+$7/3600); if ($8 == "W") lon=-lon; 
     printf("%.4f,%.4f\n", lat, lon); next } 

     { print "Error on line " NR; exit 1 }' 
+0

它似乎在這種情況下的sed上沒有任何影響源字符串(GPS位置)。我正在嘗試這個,但腳本沒有返回任何東西(除了「第1行的錯誤」,如果我添加了你的支票): 'echo'40 deg 44'49.36 \「N,73 deg 56'28.18 \」W「 | awk'/^\s*([0-9.]+\s+){3}[NS]\s+([0-9.]+\s+){3}[EW]\s*$/ {lat =($ 1 + $ 2/60 + $ 3/3600);如果($ 4 ==「S」)lat = -lat; lon =($ 5 + $ 6/60 + $ 7/3600);如果($ 8 ==「W」)lon = -lon; printf(「%。4f,%。4f \ n」,lat,lon); }'' – utt50

0

這是在PHP中:

$parts = explode(" ",str_replace(array("deg",",","'","\""),"",$argv[1])); 

print_r($parts); 

$lat_deg = $parts[0]; 
$lat_min = $parts[1]; 
$lat_sec = $parts[2]; 
$lat_dir = $parts[3]; 

$lon_deg = $parts[4]; 
$lon_min = $parts[5]; 
$lon_sec = $parts[6]; 
$lon_dir = $parts[7]; 

if ($lat_dir == "N") { 
    $lat_sin = "+"; 
    } else { 
    $lat_sin = "-"; 
    } 

if ($lon_dir == "N") { 
    $lon_sin = "+"; 
    } else { 
    $lon_sin = "-"; 
    } 

$latitiude = $lat_sin.($lat_deg+($lat_min/60)+($lat_sec/3600)); 
$longitude = $lon_sin.($lon_deg+($lon_min/60)+($lon_sec/3600)); 

echo substr($latitiude,0,-5).",".substr($longitude,0,-5);