#!/usr/bin/python3 # coding=utf-8 """creates Google Earth kml file (/tmp/gps3_live.kml) for realtime (4 second GE default) updates of gps coordinates and history # Concept from Jaroslaw Zachwieja & TJ # from their work in gegpsd.py included in gpsd project (http://catb.org/gpsd) """ import time from gps3 import agps3 # Moe, remember to CHANGE to straight 'import agps3' if not installed, # or check which Python version it's installed in. You forget sometimes. __author__ = 'Moe' __copyright__ = 'Copyright 2016 Moe' __license__ = 'MIT' __version__ = '0.33.2' link_file = '/tmp/agps3_live.kml' # AFAIK, 'Links' call href on time events or entry/exit Multiple href may be possible. gps3data_file = '/tmp/agps3_static.kml' gps3data_history = [] link_data = ('\n' '\n' '\n' ' AGPS3 Live\n' ' \n' ' {0}\n' ' onInterval\n' ' \n' '\n' '').format(gps3data_file) # inserts 'the gps3data file' into a refresh mode default 4 second f = open(link_file, 'w') f.write(link_data) f.close() gps_socket = agps3.GPSDSocket() gps_socket.connect(host='localhost', port=2947) gps_socket.watch() data_stream = agps3.DataStream() try: for new_data in gps_socket: if new_data: data_stream.unpack(new_data) if data_stream.lat != 'n/a': speed = data_stream.speed latitude = data_stream.lat longitude = data_stream.lon altitude = data_stream.alt if data_stream.track == 'n/a': heading = data_stream.track # 'track' frequently is missing and returns as 'n/a' else: heading = round(data_stream.track) # and heading precision in hundreths is just clutter. gps3data_history.append(longitude) gps3data_history.append(latitude) gps3data_history.append(altitude) hist_string = str(gps3data_history).replace(' ', '') # GE > 7.1.xxxx spits up on spaces in static_file = ('\n' '\n' '\n' ' Frankie likes walking and stopping \n' ' \n' ' {0:.2f} m/s {4}°\n' ' Current gps location\nAltitude: {3} Metres\n' ' \n' ' {1}\n' ' {2}\n' ' 600\n' ' 0\n' ' 0\n' ' \n' ' \n' ' {1},{2},{3}\n' ' \n' ' \n' ' \n' ' Pin Scratches\n' ' GPS Trail of Tears\n' ' \n' ' 7f0000ff\n' ' 20\n' ' 1\n' ' {5}\n' ' \n' ' \n' '\n' '').format(speed, longitude, latitude, altitude, heading, hist_string.strip('[]')) f = open(gps3data_file, 'w') f.write(static_file) f.close() else: time.sleep(.1) time.sleep(.7) # default GE refresh rate is 4 seconds, therefore no refresh older than ~1 second from itself. except KeyboardInterrupt: gps_socket.close() print('\nTerminated by user\nGood Bye.\n') # End