Twisted Positioning

twisted.positioning: geolocation in Twisted


twisted.positioning is a package for doing geospatial positioning (trying to find where you are on Earth) using Twisted.

High-level overview

In twisted.positioning, you write an IPositioningReceiver implementation that will get called whenever some information about your position is known (such as position, altitude, heading...). The package provides a base class, BasePositioningReceiver you might want to use that implements all of the receiver methods as stubs.

Secondly, you will want a positioning source, which will call your IPositioningReceiver. Currently, twisted.positioning provides an NMEA implementation, which is a standard protocol spoken by many positioning devices, usually over a serial port.


#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
Connects to an NMEA device, logs beacon information and position.
import sys
from twisted.internet import reactor, serialport
from twisted.positioning import base, nmea
from twisted.python import log, usage

class PositioningReceiver(base.BasePositioningReceiver):
    def positionReceived(self, latitude, longitude):
        log.msg("I'm at {} lat, {} lon".format(latitude, longitude))

    def beaconInformationReceived(self, beaconInformation):
        template = "{0.seen} beacons seen, {0.used} beacons used"

class Options(usage.Options):
    optParameters = [
        ['baud-rate', 'b', 4800, "Baud rate (default: 4800)"],
        ['serial-port', 'p', '/dev/ttyS0', 'Serial Port device'],

def run():

    opts = Options()
    except usage.UsageError, message:
        print "{}: {}".format(sys.argv[0], message)

    positioningReceiver = PositioningReceiver()
    nmeaReceiver = nmea.NMEAAdapter(positioningReceiver)
    proto = nmea.NMEAProtocol(nmeaReceiver)

    port, baudrate = opts["serial-port"], opts["baud-rate"]
    serialport.SerialPort(proto, port, reactor, baudrate=baudrate)

if __name__ == "__main__":
  • Connects to an NMEA device on a serial port, and reports whenever it receives a position.

Table Of Contents

Previous topic

Porting to Python 3

Next topic

Twisted Glossary

This Page