The following python 2.x script shows your current (GPS-enabled) location on a Google Map. A useful learning experience:
#!/usr/bin/env python
"""This is a python 2.5 script that plot's a GPS receiver's location on the
Google Maps website.
In order to work, you need a network connection, an attached GPS receiver, and
the GPS daemon (gpsd).
"""
import os
#import subprocess and SU
import gps
import dbus
import sys
def test( ):
""" Step 1: Test for the existence of a running gpsd, test for the existence of an open network connection, and test for a firefox process.
If any fail, give an error message, don't try to recover. FUTURE: Could also use DBus to test for firefox and gpsd."""
process_list = os.popen('ps -e') # os.popen is deprecated in favort of subprocess.Popen
#process_list = SU.Popen(['ps','e'], stdout=SU.PIPE).stdout.read()
gpsd_existence_flag = 0
firefox_existence_flag = 0
for line in process_list.readlines():
if line.count('gpsd') > 0: gpsd_existence_flag = 1
if line.count('firefox') > 0: firefox_existence_flag = 1
if not gpsd_existence_flag:
print ("gpsd is not running. Use 'gpsd -b /dev/ttyUSB0' to start it, and then try again.")
sys.exit()
else: print ('Checking...found gpsd')
if not firefox_existence_flag:
print ("firefox is not running. Please start it and try again.")
sys.exit()
else: print ('Checking...found firefox')
bus = dbus.SystemBus()
nm_item = ('org.freedesktop.NetworkManager') # This string gets used a lot
nm_path = ('/org/freedesktop/NetworkManager')
nm_device = ('org.freedesktop.NetworkManager.Device')
list_of_interface_paths = dbus.Interface(bus.get_object(nm_item, nm_path), nm_device).getDevices()
found_network_flag = 0
for interface_path in list_of_interface_paths:
one_interface = dbus.Interface(bus.get_object(nm_item, interface_path), nm_device)
if one_interface.getLinkActive(): # True if there is an active network on this interface
if one_interface.getType() == 2: # 0 unknown, 1 wired, 2 wireless
print('Checking...found the wireless network')
found_network_flag = 1
elif one_interface.getType() == 1:
print('Checking...found the wired network')
found_network_flag = 1
if found_network_flag: return
else:
print ("cannot find a network connection. Please connect and try again.")
sys.exit()
def get_position_fix( ):
"""Step 2: Get a position fix from gpsd."""
session = gps.gps('localhost','2947') # Open a connection to gpsd
session.query('p') # Get the location fix
lat = session.fix.latitude
lon = session.fix.longitude
print ('Location is ' + str(lat) + ' latitude and ' + str(lon) + ' longitude.')
return (lat, lon)
def show_map(lat_lon_tuple):
"""Step 3: Submit the position fix to Google Maps. Note that the parentheses '()' in the URL must be escaped '\' to work.
Sample URL format: http://maps.google.com/maps?q=37.771008,+-122.41175+(You+can+insert+your+text+here)&iwloc=A&hl=en"""
url_string = ('http://maps.google.com/maps?q=' + str(lat_lon_tuple[0]) + ',+' + str(lat_lon_tuple[1]) + '+\(You+Are+Here\)&iwloc=A&hl=en')
os.popen('firefox ' + url_string)
return
# Run this script as a standalone program
if __name__ == "__main__" :
test()
location = get_position_fix()
show_map(location)
No comments:
Post a Comment