How to save GPS data to a file using Python

Below is an example python script which will save GPS data (time, Lon, Lat, speed and sats in view) to a file.

The gpsd client libraries  will be used to get the data from GPSD. We will be using the TPV class to get time, latitude, longitude and speed.

We can get the number of satellites in view by getting the length of the satellites object.

This page shows how to get gpsd up an running on a Raspberry Pi

Every time the script is run, it will create a new file beginning with the current date and time.

In this example, I am writing in a csv format, where each GPS attribute is separated by a comma.

#! /usr/bin/python
from gps import *
import time, inspect

f = open(time.strftime("%Y%m%d-%H%M%S")+'_GSPData.csv','w')
gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)
print 'GPStime utc\t\t\tlatitude\tlongitude\tspeed\tsats in view' # '\t' = TAB to try and output the data in columns.
f.write("GPStime utc,latitude,longitude,speed,sats in view\n")
try:
    while True:
        report = gpsd.next() #
        if report['class'] == 'TPV':
            GPStime =  str(getattr(report,'time',''))
            lat = str(getattr(report,'lat',0.0))
            lon = str(getattr(report,'lon',0.0))
            speed =  str(getattr(report,'speed','nan'))
            sats = str(len(gpsd.satellites))
            print  GPStime,"\t",
            print  lat,"\t",
            print  lon,"\t",
            print  speed,"\t",
            print  sats,"\t"
            f.write(GPStime + ',' + lat +',' + lon + ',' + speed + ',' + sats + '\n')
            time.sleep(1)
except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    print "Done.\nExiting."
    f.close()

10 thoughts on “How to save GPS data to a file using Python”

  1. Hello Mark, Thanks for the script. What is the 1+ for in the first of these two lines copied from the script :
    lat = str(1+getattr(report,’lat’,0.0))
    lon = str(getattr(report,’lon’,0.0))

    Regards,
    Neil

  2. Mark love you’re GPS tutorials, how about taking it to the next level push a button to create a new track, push another button to create a waypoint, all the data (track logs, waypoints) saved in the GPX or KML format so the data can be imported directly into Google earth or other programs.

    Thanks in advance.

  3. Hallo,
    sowas habe ich 2 Monate lang gesucht.
    Super!
    Ist die Geschwindigkeit in Knoten, Meilen oder km?
    Is the speed in knots , miles or km?
    Thank you

  4. I have 2 quetions regarding this Python Script, 1.) What version of Python is this compatible with and 2.) exactly what do i need to add to convert th speed to MPH? Thank you

    1. The above code supports Python 2.7

      You can use the conversation factor (0.621371)to get miles;
      Update these two line sin the code;
      print speed/0.621371,"\t",
      f.write(GPStime + ',' + lat +',' + lon + ',' + speed/0.621371 + ',' + sats + '\n')

  5. This has given me somewhere to start from, thank you!
    Am looking to make a simple Waypoint logger to use on a motorcycle, whereby on a long ride when riding past something that catches my eye I can push a button to log a waypoint. Then when viewing these on a map later I can zoom in to find out more, either to satisfy curiousity or plan a return visit. My friend had such a feature on her old Garmin but the device no longer works.
    We have started using GPS Logger on Android phones to track where we have been but whilst it has functionality to ‘drop a pin’ it isn’t as simple as pressing one button whilst concentrating on the road.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.