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

Last change on this file since 20924 was 12252, checked in by jeansch, 11 years ago

Fixed 'tah' URL
Fixed starting from another directory
Implemented 'scroll' events to allow to change zoom from mouse-wheel
Implemented initial position

File size: 3.7 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 os
27import socket
28from time import sleep
29import re
30
31class geoPosition:
32  def __init__(self):
33    self.mode = "none"
34    try:
35      self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
36      self.s.connect(("127.0.0.1", 2947))
37      self.mode = "socket"
38    except socket.error:
39      print "No GPSD detected, position information will not be available"
40      pass # leave mode at "none"
41   
42    self.filename = os.path.join(os.path.dirname(__file__), "pos.txt")
43    if(os.path.exists(self.filename)):
44      self.mode = "file"
45      print "Using %s as position reference" % self.filename
46
47    self.filename = os.path.join(os.path.dirname(__file__), "init_pos.txt")
48    if(os.path.exists(self.filename)):
49      self.mode = "initfile"
50      print "Using %s as initial position reference" % self.filename
51 
52  def get(self):
53    try:
54        function = getattr(self, 'get_%s' % self.mode)
55    except AttributeError:
56        return({'valid':False,'message':"function for %s not defined" % self.mode})
57    return(function())
58   
59  def get_none(self):
60    return({'valid':False,'message':"no gps"})
61 
62  def socket_cmd(self, cmd):
63    self.s.send("%s\r\n" % cmd)
64    return self.s.recv(8192)
65   
66  def test_socket(self):
67    for i in ('i','p','p','p','p'):
68      print "%s = %s" % (i, self.socket_cmd(i))
69      sleep(1)
70   
71  def get_socket(self):
72    pos = self.socket_cmd("p")
73    p = re.compile('P=(.*?)\s*$')
74    match = p.search(pos)
75    if(match == None):
76      return({'valid':False,'message':"GPS returned %s" % pos})
77    text = match.group(1)
78    if(text == '?'):
79      return({'valid':False,'message':"GPS returned P=?"})
80    lat,lon = [float(ll) for ll in text.split(' ')]
81    #print "matched '%s', '%s'"%(lat,lon)
82    return({'valid':True, 'lat':lat, 'lon':lon, 'source':'GPS'})
83 
84  def get_file(self):
85    file = open(self.filename, 'r')
86    text = file.readline(50)
87    file.close()
88    try:
89      lat,lon = [float(i) for i in text.rstrip().split(",")]
90      return({'valid':True, 'lat':lat, 'lon':lon, 'source':'textfile'})
91    except ValueError:
92      return({'valid':False, 'message':'Invalid textfile'})
93
94  def get_initfile(self):
95    file = open(self.filename, 'r')
96    text = file.readline(50)
97    file.close()
98    self.mode = "none"
99    try:
100      lat,lon = [float(i) for i in text.rstrip().split(",")]
101
102      return({'valid':True, 'lat':lat, 'lon':lon, 'source':'textfile'})
103    except ValueError:
104      return({'valid':False, 'message':'Invalid textfile'})
105   
106if __name__ == "__main__":
107  #print dir(socket)
108  pos = geoPosition()
109  #print pos.test_socket()
110  for i in range(5):
111    print pos.get()
112    sleep(1)
Note: See TracBrowser for help on using the repository browser.