# Generate the checksum for some data # (Checksum is all the data XOR'd, then turned into hex) def generate_checksum(data): csum = 0 for c in data: csum = csum ^ ord(c) hex_csum = "%02x" % csum return hex_csum.upper() # Format a NMEA timestamp into something friendly def format_time(time): hh = time[0:2] mm = time[2:4] ss = time[4:] return "%s:%s:%s UTC" % (hh,mm,ss) # Format a NMEA date into something friendly def format_date(date): months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') dd = date[0:2] mm = date[2:4] yy = date[4:6] yyyy = int(yy) + 2000 return "%s %s %d" % (dd, months[(int(mm)-1)], yyyy) ############################################################################# # Get the location from a GGA sentence def get_gga_location(data): '''$GPGGA,042327,3722.1926,N,12656.7660,E,1,03,04.5,00000.0,M,018.1,M,,*41''' d = data.split(',') ret = {} ret['type'] = 'GGA' ret['lat'] = getKML_lat("%s%s" % (d[1],d[2])) ret['long'] = getKML_long("%s%s" % (d[3],d[4])) ret['time'] = format_time(d[0]) ret['alti'] = '%s%s'%(d[8],d[9]) return ret # Get the location from a GLL sentence def get_gll_location(data): d = data.split(',') ret = {} ret['type'] = 'GLL' ret['lat'] = getKML_lat("%s%s" % (d[0],d[1])) ret['long'] = getKML_long("%s%s" % (d[2],d[3])) ret['time'] = format_time(d[4]) return ret # Get the location from a RMC sentence def get_rmc_location(data): '''$GPRMC,042327,A,3722.1926,N,12656.7660,E,000.0,019.3,071206,,,A*70''' d = data.split(',') ret = {} ret['type'] = 'RMC' ret['lat'] = getKML_lat("%s%s" % (d[2],d[3])) ret['long'] = getKML_long("%s%s" % (d[4],d[5])) ret['time'] = format_time(d[0]) ret['date'] = format_date(d[8]) return ret ############################################################################# def getKML_lat(s): '3721.1122N' h = s[:2] b = s[2:-1] p = s[-1] d_h = eval(h) d_b = eval(b) d = d_h + (d_b/60) if p == 'N': return '%.8f'%d else: return '-%.8f'%d def getKML_long(s): #print 'getKML_long',nmea_long '12655.5694E' h = s[:3] b = s[3:-1] p = s[-1] d_h = eval(h) d_b = eval(b) d = d_h + (d_b/60) if p == 'E': return '%.8f'%d else: return '-%.8f'%d def getLocation(nmeaLine): data = nmeaLine.strip() if not data: return ''#continue # Ensure it starts with $GP if not data[0:3] == '$GP': return ''#continue # If it has a checksum, ensure that's correct # (Checksum follows *, and is XOR of everything from # the $ to the *, exclusive) if data[-3] == '*': exp_checksum = generate_checksum(data[1:-3]) if not exp_checksum == data[-2:]: print "Invalid checksum %s, expecting %s" % (data[-2:], exp_checksum) return ''#continue # Strip the checksum data = data[:-3] # Grab the parts of the sentence talker = data[1:3] sentence_id = data[3:6] sentence_data = data[7:] #print l location = {} if sentence_id == 'GGA': location = get_gga_location(sentence_data) elif sentence_id == 'GLL': location = get_gll_location(sentence_data) elif sentence_id == 'RMC': location = get_rmc_location(sentence_data) else: return ''#continue return location ############################################################################# if __name__ == '__main__': dataFile = 'Data\\WG20061207132002.log' for data in open(dataFile): data = data.strip() if not data: continue location = getLocation(data) if location: print '%s,%s,%s'%(location['type'],location['lat'],location['long'])