Use awk to convert GPS Position to Latitude & Longitude

霸气de小男生 提交于 2019-12-08 04:30:28

问题


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/\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))}}' 

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!


回答1:


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,%.4f\n", lat, lon); next  }

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



回答2:


Here it is in 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);


来源:https://stackoverflow.com/questions/32879691/use-awk-to-convert-gps-position-to-latitude-longitude

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!