Skip to content

Instantly share code, notes, and snippets.

@matbor
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save matbor/9734362 to your computer and use it in GitHub Desktop.

Select an option

Save matbor/9734362 to your computer and use it in GitHub Desktop.
HowFar2Home -- This is for mqttwarn https://github.com/jpmens/mqttwarn and Owntracks https://github.com/owntracks.

How Far 2 Home

This function was designed as a proof of concept and will return the estimated driving distance the device (using owntracks here) is from the GPS coordinates specified in the functions file

For example;

2014-03-24 15:45:00,484 INFO [log] matthew iphone is 38mins away from home

Requiements

NOTE's

  • Unfortuantely it doesn't return the travel time including the current traffic! You need the 'google maps api for business' i believe to do this (need to confirm this!)
  • As it has been pointed out.... (Use at own risk!)... Use of the Distance Matrix API must relate to the display of information on a Google Map; for example, to determine origin-destination pairs that fall within a specific driving time from one another, before requesting and displaying those destinations on a map. Use of the service in an application that doesn't display a Google map is prohibited. https://developers.google.com/maps/documentation/distancematrix/

I suggest looking at other map services api's and see if they have similar restrictions.

Future, or you could easily do it yourself

  • Intergrate my other fucntion, reverse geo, https://gist.github.com/matbor/9381893
  • change to another api that doesn't have the restriction that google maps does! Maybe checkout openstreetmap to see if it is ok!
############################################################
### OWNTRACKS -- Let me know how far from home I am ##
### by @bordignon on Twitter ##
############################################################
#needs urllib, json & time libraries
import time
import urllib
try:
import json
except ImportError:
import simplejson as json
def howfar2home(data):
#beginging of settings
#google maps api key, you need to enable distancematrix api
googlemapapi = "xxxxxxxxxxxxxx"
#destination coordinates for home location
dest_lat = -37.813611
dest_lng = 144.963056
#end of settings
#origin gps coordinates
orig_lat = data.get('lat')
orig_lng = data.get('lon')
#using the gps cordinates here
orig_coord = float(orig_lat), float(orig_lng)
dest_coord = dest_lat, dest_lng
epoch_time = int(time.time())
urlforgps = "https://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&sensor=false&key={2}&departure_time={3}&mode=driving".format(str(orig_coord),str(dest_coord),str(googlemapapi),str(epoch_time))
result= json.load(urllib.urlopen(urlforgps))
driving_time = result['rows'][0]['elements'][0]['duration']['value']
minutes2home = driving_time / 60
data['time2home'] = minutes2home
#return ('%s mins from home' % minutes)
return "{username} {device} is {time2home}mins away from home".format(**data)
######
#owntracks maps the topic name
# THIS IS IN THE MQTTWARN sample functions file. You might not need this part.
def OwnTracksTopic2Data(topic):
if type(topic) == str:
try:
# owntracks/username/device
parts = topic.split('/')
username = parts[1]
deviceid = parts[2]
except:
deviceid = 'unknown'
username = 'unknown'
return dict(username=username, device=deviceid)
return None
[owntracks-howfar2home]
topic = owntracks/+/+
targets = log:info
datamap = OwnTracksTopic2Data()
format = howfar2home()
title = How far 2 home
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment