Archive

Posts Tagged ‘convert’

awk’ing lat/lon

January 10th, 2013 No comments

Here’s how one can convert latitude/longitude values from degrees to decimal format using GNU awk.

# description: convert dec values to lat/lon
# license: GPL 3.0
# input: "N010.00.21.698 E094.24.35.999"
# output: "10.006027 94.41

awk '{
lat=$1;lon=$2;
split (lat, latitude, ".");
latpre=substr(latitude[1],1,1);
latdeg=substr(latitude[1],2);
latmin=latitude[2];
latsec=latitude[3];
latmil=latitude[4];
latdec=latdeg+(latmin/60)+(latsec/3600)+(latmil/3600000);
if (latpre == "S"){
latdec=latdec*-1;
}

split (lon, longitude, ".");
lonpre=substr(longitude[1],1,1);
londeg=substr(longitude[1],2);
lonmin=longitude[2];
lonsec=longitude[3];
lonmil=longitude[4];
londec=londeg+(lonmin/60)+(lonsec/3600)+(lonmil/3600000);
if (lonpre == "W"){
londec=londec*-1;
}

printf "%.7f %.7f\n", latdec, londec;
}'

usage:
echo "N010.00.21.698 E094.24.35.999" | ./awk_converter.sh
output:
10,0060272 94,4099997