Twisted Positioning

twisted.positioning: geolocation in Twisted

Introduction

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.

Examples

nmealogger.py

#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Connects to an NMEA device, logs beacon information and position.
"""
from __future__ import print_function

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"
        log.msg(template.format(beaconInformation))



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



def run():
    log.startLogging(sys.stdout)

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

    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)

    reactor.run()



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