Use awk to convert GPS Position to Latitude & Longitude
I am writing a bash script that renames files based on EXIF headers. exiftool returns the following GPS Position string, which I need to format into Latitude/Longitude coordinates for use with Google Maps API.
GPS Position : 40 deg 44' 49.36" N, 73 deg 56' 28.18" W
Google Maps:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611
This is my code
awk -v FS="[ t]" '{print $0,substr($1,length($1),1)substr($2,length($2),1)}' $1
| sed 's/xc2xb0([0-9]{1,2}).([NEWS])/ 1 0 2/g;s/xc2xb0([NEWS])/ 0 0 1/g;s/[^0-9NEWS]/ /g'
| awk '{if ($9=="NE") {printf ("%.4ft%.4fn",$1+$2/60+$3/3600,$5+$6/60+$7/3600)}
else if ($9=="NW") {printf ("%.4ft%.4fn",$1+$2/60+$3/3600,-($5+$6/60+$7/3600))}
else if ($9=="SE") {printf ("%.4ft%.4fn",-($1+$2/60+$3/3600),$5+$6/60+$7/3600)}
else if ($9=="SW") {printf ("%.4ft%.4fn",-($1+$2/60+$3/3600),-($5+$6/60+$7/3600))}}'
I'm getting this error:
sed: RE error: illegal byte sequence
What I need is a valid awk command to strip the “deg” and NSEW text, and divide by 3600 and 60 per this post:
how to convert gps north and gps west to lat/long in objective c
40 deg 44' 49.36" N, 73 deg 56' 28.18" W > 40.7470444,-073.9411611
Please help!
For this particular case, I would write it as below if special characters were a problem. Ofcourse it has the disadvantage that error checks would not be as stringent.
# 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,%.4fn", lat, lon); next }
{ print "Error on line " NR; exit 1 }'
这里是在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);
链接地址: http://www.djcxy.com/p/47308.html
上一篇: Java Pass方法作为参数
下一篇: 使用awk将GPS位置转换为经度和纬度