Skip to content

Instantly share code, notes, and snippets.

@matbor
Last active August 29, 2015 13:57
Show Gist options
  • Save matbor/9381893 to your computer and use it in GitHub Desktop.
Save matbor/9381893 to your computer and use it in GitHub Desktop.
This will reverse Geo the gps data sent by owntracks and then send it thru mqttwarn to whatever target you specify, https://github.com/jpmens/mqttwarn
#add this first part to the top of the functions file
#for reversegeo
import time
#import logging
from urllib import urlencode
from urllib2 import urlopen
#this filter will reverse geo the address for you
def OwnTracksConvertWithAdd(data):
if type(data) == dict:
tst = data.get('tst', int(time.time()))
data['tst'] = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(int(tst)))
# Remove these elements to eliminate warnings
for k in ['_type', 'desc']:
data.pop(k, None)
whereisthis = getaddress(data)
data['whereisthis'] = whereisthis
return "{username} {device} {tst} was last seen at {whereisthis}".format(**data)
class ReverseGeo(object):
"""
Reverse geocoder using the MapQuest Open Platform Web Service.
"""
def __init__(self):
"""Initialize reverse geocoder; no API key is required by the
Nomatim-based platform"""
self.url = "http://open.mapquestapi.com/nominatim/v1/reverse?format=json&addressdetails=1&%s"
def parse_json(self, data):
try:
data = json.loads(data)
except:
data = {}
return data
def reverse(self, lat, lon):
params = { 'lat': lat, 'lon' : lon }
url = self.url % urlencode(params)
data = urlopen(url)
response = data.read()
return self.parse_json(response)
def getaddress(data):
if type(data) == dict and 'lat' in data:
nominatim = ReverseGeo()
lookitup = nominatim.reverse(data['lat'], data['lon'])
# logging.debug(lookitup)
fulladdress = lookitup['display_name']
return str(fulladdress)
return None
;add this to the bottom of the mqttwarn.ini file, don't forget to add some more targets.
[owntracks-location]
topic = owntracks/+/+
targets = log:info
datamap = OwnTracksTopic2Data()
format = OwnTracksConvertWithAdd()
@sumnerboy12
Copy link

Seriously cool mate - works like a charm!! One little buglette in your notes, you are missing the 'import time' at the top of the functions file. Apart from that, worked first time. Cheers.

@matbor
Copy link
Author

matbor commented Mar 19, 2014

@summerboy12 will re-add, was just assuming people where adding it to sample function file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment