source: subversion/applications/routing/pyroute/geoPosition.py @ 5513

Last change on this file since 5513 was 5513, checked in by ojw, 12 years ago

looking at gpsd, still not easy to test on desktop pc

File size: 2.1 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3# report "current position" (currently accepts replayed GPX
4# files from follow.py)
5#------------------------------------------------------
6# Usage:
7#   pos = geoPosition(filename_of_latlong_file)
8#   lat,lon = pos.get()
9#------------------------------------------------------
10# Copyright 2007, Oliver White
11#
12# This program is free software: you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation, either version 3 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24#------------------------------------------------------
25import sys
26import gps
27import os
28import socket
29from time import sleep
30
31class geoPosition:
32  def __init__(self):
33    self.mode = "none"
34    try:
35      self.gpsd = gps.gps()
36      self.mode = "gpsd"
37      return
38    except socket.error:
39      pass
40   
41    self.filename = "pos.txt"
42    if(os.path.exists(self.filename)):
43      self.mode = "file"
44 
45  def get(self):
46    try:
47        function = getattr(self, 'get_%s' % self.mode)
48    except AttributeError:
49        print "Error: position mode %s not defined" % self.mode
50        self.mode = 'none'
51        return(0,0)
52    return(function())
53   
54  def get_none(self):
55    return((False,0,0))
56 
57  def get_gpsd(self):
58    return((True, self.gpsd.fix.latitude, self.gpsd.fix.longitude))
59 
60  def get_file(self):
61    file = open("pos.txt", 'r')
62    text = file.readline(50)
63    file.close()
64    try:
65      lat,lon = [float(i) for i in text.rstrip().split(",")]
66      return((True, lat,lon))
67    except ValueError:
68      return((False, 0,0))
69   
70if __name__ == "__main__":
71  pos = geoPosition()
72  print pos.get()
Note: See TracBrowser for help on using the repository browser.