#!/usr/bin/python
"""
Script to read nmea data from a GPS mouse
Copyright (C) 2008 Andreas Goelzer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
This program reads data from a nmea device (uses the GGA sentence),
and creates a google kml file with the current position and the path
leading there. Additionally, a gpx file is generated containing the
track data with time information.
"""
from serial import Serial;
from time import strftime, gmtime;
from optparse import OptionParser;
hist = "";
gpx = "";
kmlmask="""
Your Position%sPrevious positionsPath that lead you here%s"""
# absolute
gpxmask = """%s%s"""
def gpstime():
return strftime("%Y-%m-%dT%H:%M:%SZ",gmtime());
def readlatlon(value,inverse):
lat = float(value);
lat2 = int(lat) / 100;
lat = lat2 + (lat - 100 * lat2)/ 60;
if inverse:
lat = -lat;
return lat;
def calcchecksum(checkstr):
cs = ord(checkstr[0]);
for i in range(1,len(checkstr)):
cs ^= ord(checkstr[i])
return cs;
class sentencevalidator:
"Check checksum and other stuff and split to fields"
def __init__(self,line):
if(line[0] == '$' and line[-5] == '*' and calcchecksum(line[1:-5]) == int(line[-4:-2],16)):
self.valid = True;
self.sentence = line[3:6];
self.data = line[7:-5].split(',');
else :
self.valid = False;
parser = OptionParser(version="%prog 0.1");
parser.add_option("-s", "--serial", dest="serial",
help="serial terminal of the gps unit", default='/dev/ttyUSB0', metavar="FILE");
parser.add_option("-g", "--gpxfile", dest="gpxfile",
help="GPX output file", default="/tmp/track" + gpstime() + ".gpx", metavar="FILE");
parser.add_option("-k", "--kmlfile", dest="kmlfile",
help="KML output file", default="/tmp/posdata.kml", metavar="FILE");
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="print debug messages");
(options, args) = parser.parse_args();
gps = Serial(options.serial, 4800, timeout=1);
#on sirf chips, activate all sentences
for i in range(1,5):
cmd = "PSRF103,0%d,00,05,01" % i;
realcmd="$%s*%x\r\n" % (cmd,calcchecksum(cmd));
print realcmd;
gps.write(realcmd)
try:
while 1:
line = gps.readline()
if(options.verbose): print line,
s = sentencevalidator(line);
if(s.valid and s.sentence == "GGA" and int(s.data[5]) != 0):
lat = readlatlon(s.data[1],s.data[2]=='S');
lon = readlatlon(s.data[3],s.data[4]=='W');
alt = float(s.data[8]);
gpx += "\n" + '%f' % (lon,lat,alt,gpstime())
position = "%f, %f, %f" % (lon,lat,alt);
print position
hist += "\n" + position;
f=open("/tmp/posdata.kml",'w');
f.write(kmlmask % (position,hist));
f.close();
except:
0
f=open(options.gpxfile,'w');
f.write(gpxmask % (gpstime(),gpx));
f.close();
gps.close()