#!/usr/bin/env python # Generate JSON from a LOCUS log file # (c) 2013 Don Coleman import locus import json import datetime coords = locus.parseFile('sample.log') # filter out bad data coords = [c for c in coords if c.fix > 0 and c.fix < 5] class Encoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, locus.Coordinates): return obj.__dict__ if isinstance(obj, datetime.datetime): return obj.strftime("%Y-%m-%dT%H:%M:%S%z") return json.JSONEncoder.default(self, obj) print json.dumps(coords, cls = Encoder, sort_keys=True, indent=4)
#!/usr/bin/env python # Generate JSON from a LOCUS log file # (c) 2013 Don Coleman import locus import json import datetime coords = locus.parseFile('gps_raw.txt') # sample.log # filter out bad data coords = [c for c in coords if c.fix > 0 and c.fix < 5] class Encoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, locus.Coordinates): return obj.__dict__ if isinstance(obj, datetime.datetime): return obj.strftime("%Y-%m-%dT%H:%M:%S%z") return json.JSONEncoder.default(self, obj) print json.dumps(coords, cls=Encoder, sort_keys=True, indent=4)
#!/usr/bin/env python # Generate KML from a LOCUS log file # KML template is based on the output from # http://learn.adafruit.com/custom/ultimate-gps-parser # # usage: python kml.py > example.kml # open the generated example.kml in Google Earth # # (c) 2013 Don Coleman import locus coords = locus.parseFile('sample.log') # filter out any bad records coords = [c for c in coords if c.fix > 0 and c.fix < 5] # format each coordinate for kml data = map(lambda c: "%3.14f,%3.14f,%s" % (c.longitude, c.latitude, c.height), coords) template = """<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>GPS Path</name> <description>Path parsed from GPS data.</description> <Style id="yellowLineGreenPoly"> <LineStyle> <color>7f00ffff</color> <width>4</width>
#!/usr/bin/env python # Generate KML from a LOCUS log file # KML template is based on the output from # http://learn.adafruit.com/custom/ultimate-gps-parser # # usage: python kml.py > example.kml # open the generated example.kml in Google Earth # # (c) 2013 Don Coleman import locus coords = locus.parseFile("sample.log") # filter out any bad records coords = [c for c in coords if c.fix > 0 and c.fix < 5] # format each coordinate for kml data = map(lambda c: "%3.14f,%3.14f,%s" % (c.longitude, c.latitude, c.height), coords) template = """<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Document> <name>GPS Path</name> <description>Path parsed from GPS data.</description> <Style id="yellowLineGreenPoly"> <LineStyle> <color>7f00ffff</color> <width>4</width> <gx:labelvisibility>0</gx:labelvisibility>
def getGPS(): # Variable to indicate when file headers been printed global HEADERS_PRINTED # CSV file named using the current date CSV_FILE = USB_PATH + datetime.datetime.now().strftime('%Y-%m-%d') + '.csv' # wrties twice to the same file 1. at start 2. after sleep f_csv = open(CSV_FILE, "a") # Opening file for GPS storage f_raw = open(TEMP_FILE, "w") # # Getting raw GPS data # # Array of characters to hold GPS bytes, two bytes at a time buff = [] buff.append('X') buff.append('X') logging.info("GPS-DUMP STARTED") print("GPS-DUMP STARTED") # Send Start Dump log command write(GPS_DUMP) # Breaks from loop after 3 minutes start = datetime.datetime.now() # Loop to receive GPS data using shift register # When OK signal received, loop exits while not ((buff[0] == 'O') and (buff[1] == 'K')): # Shifting buffer buff[0] = buff[1] # Receiving new character buff[1] = port.read() #if not (buff[1] == ""): #print("%c" % buff[1]) # Saving f_raw.write(buff[1]) # After 3 minutes, loop exits automatically if (datetime.datetime.now() - start).seconds > (3*60): logging.info("GPS-DUMP TIMEOUT OCCURED") print("GPS-DUMP TIMEOUT OCCURED") break # Closing temprary raw data file f_raw.close() # # Parsing # print("PARSING TO CSV") # Parse data as JSOM coords = locus.parseFile(MISSION_FILE) # filter out bad data coords = [c for c in coords if c.fix > 0 and c.fix < 5] # Printing into CSV file # Headers if not (HEADERS_PRINTED == True): f_csv.write("Timestamp,Satellite Fix,Latitude,Longitude\n") HEADERS_PRINTED = True # Lines for c in coords: line = str(c.datetime) +","+ str(c.fix) +","+ str(c.latitude) +","+ str(c.longitude) +"\n" f_csv.write(line) # print(line) # Closing CSV file f_csv.close() logging.info("GPS-DUMP FINISHED") print("GPS-DUMP FINISHED") # Starting new Log write(GPS_ERASE) write(GPS_LOG)
# Program to test locus data parsing into CSV file import locus # lib for GPS data parsing import json # JSON format library import datetime # lib for system datetime TEMP_PATH = "gps_raw.txt" f_csv = open(datetime.datetime.now().strftime('%Y-%m-%d') + '.csv', "a") print("PARSING STARTED") # Parse data as JSOM coords = locus.parseFile(TEMP_PATH) # filter out bad data coords = [c for c in coords if c.fix > 0 and c.fix < 5] # Printing into CSV file # Headers f_csv.write("Timestamp,Satellite Fix,Latitude,Longitude,Altitude\n") # Lines for c in coords: line = str(c.datetime) + "," + str(c.fix) + "," + str( c.latitude) + "," + str(c.longitude) + "," + str(c.height) f_csv.write(line + "\n") print(line)