#N900 - My New GPS - Using Liblocation in a Python Script

Last December, I wrote a blog post about My New GPS, the #N900. In it, I mentioned writing a simple Python Script to use liblocation to gather and print out location information via Python. It was a very quick and dirty script, but I used it for some fun mapping.

Recently, I was asked for a copy of the script, so I am posting it below, including various lines that I had commented out during my testing. Some of them are useful in understanding different parameters that you might want to use.

Enjoy. Let me know any comments.

#!/usr/bin/python
import location
import gobject
import time

def on_error(control, error, data):
    print "location error: %d... quitting" % error
    data.quit()

def on_changed(device, data):
    if not device:
        return
    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
            print "%s " % time.asctime() ,
            print "lat = %f, long = %f" % device.fix[4:6],
            print "alt = %f" % device.fix[7],
            print "hor acc: %f meters" % (device.fix[6] / 100)
#            data.stop()

def on_stop(control, data):
    print "quitting"
    data.quit()

def start_location(data):
    data.start()
    return False

loop = gobject.MainLoop()
control = location.GPSDControl.get_default()
device = location.GPSDevice()

# control.set_properties(preferred_method=location.METHOD_USER_SELECTED, 
# control.set_properties(preferred_method=location.METHOD_CWP,
control.set_properties(preferred_method=location.METHOD_GNSS,
                       preferred_interval=location.INTERVAL_60S)
#                       preferred_interval=location.INTERVAL_DEFAULT)

control.connect("error-verbose", on_error, loop)
device.connect("changed", on_changed, control)
control.connect("gpsd-stopped", on_stop, loop)

gobject.idle_add(start_location, control)

loop.run()

(Categories: )