1

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!

Community
  • 1
  • 1
utt50
  • 155
  • 2
  • 9
  • Why do you have these `\xb0` and `\xc2` extended ASCII codes in your sed patterns? What version of sed do you use? Did you look at http://stackoverflow.com/questions/19242275/re-error-illegal-byte-sequence-on-mac-os-x? – Renaud Pacalet Oct 01 '15 at 05:38
  • Does the EXIF output have `deg` or `°`? – svsd Oct 01 '15 at 08:58
  • The answer is here: http://stackoverflow.com/questions/18304358/convert-co-ordinate-d-m-s-to-decimal-degrees-using-awk – utt50 Oct 01 '15 at 17:33

2 Answers2

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 }'
svsd
  • 1,831
  • 9
  • 14
  • It seems in this case sed has no effect on the source string (the GPS Position). I'm trying this but the script isn't returning anything (except "Error on line 1" if I add your check): `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); if ($4 == "S") lat=-lat; lon=($5+$6/60+$7/3600); if ($8 == "W") lon=-lon; printf("%.4f,%.4f\n", lat, lon); }'` – utt50 Oct 01 '15 at 17:05
0

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);
utt50
  • 155
  • 2
  • 9