View Single Post
Posts: 67 | Thanked: 36 times | Joined on May 2010 @ Claremont (LA), California
#14
It turns out that when gpsjinni exports a KML file, it swaps the latitude and longitude\ (putting the longitude first, while the convention is to put latitude first). I suspect that this very easily fixed bug is the cause of a lot of the problems people have been having.

Until the bug is fixed, the following script can be used to correct a saved KML file:

Code:
#!/bin/bash
#
# Fix a gpsjinni file that has swapped latitude/longitude information.
#
USAGE='Usage: fixjinni kml-files'

TMP=/tmp/fixjinni.$$
trap "rm -f $TMP; exit 1" 1 2 15
trap "rm -f $TMP; exit 0" 13

for file
do
    awk '{
	if ($1 == "</coordinates>")
	    {
	    incoords = 0
	    print
	    }
	else if ($0 ~ /<coordinates>/)
	    {
	    gt = index($0, ">")
	    prefix = substr($0, 1, gt)
	    suffix = substr($0, gt + 1)
	    if (suffix == "")
		{
		incoords = 1
		print
		next
		}
	    lt = index(suffix, "<")
	    coords = substr(suffix, 1, lt - 1)
	    suffix = substr(suffix, lt)
	    split(coords, data, ",")
	    print prefix data[2] "," data[1] "," data[3] suffix
	    }
	else if (incoords)
	    {
	    split($1, data, ",")
	    print "          " data[2] "," data[1] "," data[3]
	    }
	else
	    print
	}' \
      "$file" \
      > $TMP
    (trap "" 1 2 13 15; cp $TMP "$file")
    rm -f $TMP
done