问题
My input is a tab-separated text file with lat long in D-M-S. I require output to be in decimal degrees I have code in php, but this is very slow to calculate. Can this be done quicker using awk?
node name id latitude longitude seq
nodex name1 70 N53-24-31.126 W6-20-46.982 59126
nodex name2 173 N53-20-28.885 W6-14-52.400 16190X
nodex name3 173 N53-20-28.885 W6-14-52.400 16191T
My PHP code with Formula:
if ($dirLat == 'N') {$signLat = '+';} Else {$signLat = '-';}
if ($dirLat == 'E') {$signLon = '+';} Else {$signLon = '-';}
$latitudeDecimalDeg = $signLat . ($degLat + ($minLat/60) + ($secLat/3600));
$longitudeDecimalDeg = $signLon . ($degLon + ($minLon/60) + ($secLon/3600));
回答1:
I am pretty certain that awk would be quicker. This is fairly easily accomplished with awk, split each field with substr
and split
. I turned it into a function for easier reuse:
function dms2deg(s) {
dir = (substr(s, 1, 1) ~ /^[NE]/) ? 1 : -1
dms = substr(s, 2)
split(dms, arr, "-")
return dir * (arr[1] + arr[2]/60 + arr[3]/3600)
}
If you have put the above into dms2deg and the data is in infile, you would use awk like this:
awk -f dms2deg -e 'NR>1 { print dms2deg($4), dms2deg($5) }' infile
Output:
53.4086 -6.34638
53.3414 -6.24789
53.3414 -6.24789
Or if you wanted to replace the existing fields:
awk -f dms2deg -e 'NR>1 { $4 = dms2deg($4); $5 = dms2deg($5) } $1=$1' infile
Output:
node name id latitude longitude seq
nodex name1 70 53.4086 -6.34638 59126
nodex name2 173 53.3414 -6.24789 16190X
nodex name3 173 53.3414 -6.24789 16191T
Note that this does not preserve white-space, however column would take care of that:
awk -f dms2deg -e 'NR>1 { $4 = dms2deg($4); $5 = dms2deg($5) } $1=$1' infile | column -t
Output:
node name id latitude longitude seq
nodex name1 70 53.4086 -6.34638 59126
nodex name2 173 53.3414 -6.24789 16190X
nodex name3 173 53.3414 -6.24789 16191T
回答2:
I really don't know if it'll be faster or not, but IMHO here's the fastest way you can do it in awk and preserve spacing:
$ cat tst.awk
BEGIN{ FS="[- ]+" }
NR>1 {
sub( $7"-"$8"-"$9, ($7~/^E/?"+":"-") (substr($7,2) + $8/60 + $9/3600) )
sub( $4"-"$5"-"$6, ($4~/^N/?"+":"-") (substr($4,2) + $5/60 + $6/3600) )
}
1
$ cat file
node name id latitude longitude seq
nodex name1 70 N53-24-31.126 W6-20-46.982 59126
nodex name2 173 N53-20-28.885 W6-14-52.400 16190X
nodex name3 173 N53-20-28.885 W6-14-52.400 16191T
$ awk -f tst.awk file
node name id latitude longitude seq
nodex name1 70 +53.4086 -6.34638 59126
nodex name2 173 +53.3414 -6.24789 16190X
nodex name3 173 +53.3414 -6.24789 16191T
I'd be interested to know if it is faster than your PHP script or not so if you get a chance to compare them, please post the results.
If you don't care about preserving spacing you can just use:
BEGIN{ FS="[- ]+" }
NR == 1 { print; next }
{ print $1, $2, $3, ($4~/^N/?"+":"-") (substr($4,2) + $5/60 + $6/3600), ($7~/^E/?"+":"-") (substr($7,2) + $8/60 + $9/3600), $10 }
回答3:
awk -F"[- ]+" 'NR==1 {print;next} {s1=($4~/N/)?1:-1;s2=($7~/E/)?1:-1;print $1,$2,$3,s1*(substr($4,2)+$5/60+$6/3600),s2*(substr($7,2)+$8/60+$9/3600),$10}' file
node name id latitude longitude seq
nodex name1 70 53.4086 -6.34638 59126
nodex name2 173 53.3414 -6.24789 16190X
nodex name3 173 53.3414 -6.24789 16191T
来源:https://stackoverflow.com/questions/18304358/convert-co-ordinate-d-m-s-to-decimal-degrees-using-awk