Move scanning area as the position changes (fixes #6887)
[wifihood] / wifiscanner / wifimap / view.py
1
2 import gtk
3 import gobject
4
5 import urllib2
6 import math
7
8 import os
9
10 import wifimap
11
12 class AbstractmapWidget :
13
14   def __init__ ( self , config , map_size=(800,480) ) :
15
16     self.conf = config
17
18     # Maximum width should be 800, but actually gets reduced
19     self.win_x , self.win_y = map_size
20     self.tile_size = 256
21
22     self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
23     self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
24
25   def recenter ( self , latlon ) :
26
27         center = self.gps2pix( latlon , self.center() )
28         pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
29
30         distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
31
32         # FIXME : instead of hardcoded, should depend on the actual display size
33         if distance > 150 :
34             self.conf.lat , self.conf.lon = latlon
35
36             self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
37             self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
38
39             self.composeMap()
40
41   def lon2tilex ( self , lon , zoom ) :
42     number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
43     return int( number[1] ) , int( self.tile_size * number[0] )
44
45   def lat2tiley ( self , lat , zoom ) :
46     lat = lat * math.pi / 180
47     number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
48     return int( number[1] ) , int( self.tile_size * number[0] )
49
50   def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
51
52     x_pos = self.lon2tilex( lon , self.conf.zoom )
53     y_pos = self.lat2tiley( lat , self.conf.zoom )
54
55     dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
56     dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
57
58     return dest_x , dest_y
59
60   def tilename ( self , x , y , zoom ) :
61     file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
62     try :
63       os.stat(file)
64     except :
65     #  if mapDownload :
66       if False :
67         try :
68           # useful members : response.code, response.headers
69           response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
70           if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
71               return None
72           fd = open( file , 'w' )
73           fd.write( response.read() )
74           fd.close()
75         except :
76           return None
77       else :
78         return None
79     return file
80
81   def tile2file( self , tilex , tiley , zoom ) :
82     rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
83     if not os.path.isdir( rootdir ) :
84       os.mkdir(rootdir)
85     rootsubdir = "%s/%s" % ( rootdir , tilex )
86     if not os.path.isdir( rootsubdir ) :
87       os.mkdir(rootsubdir)
88     return "%s/%s.png" % ( rootsubdir , tiley )
89
90 class interactiveMapWidget :
91
92   def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
93     tilex = float(tilex)
94     pixx = float(pixx)
95     return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
96
97   def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
98     tiley = float(tiley)
99     pixy = float(pixy)
100     tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
101     return math.degrees( math.atan( math.sinh( tiley ) ) )
102
103   def Shift( self , dx , dy ) :
104     self.hide()
105
106     tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
107     self.reftile_x += tile_x
108     self.reftile_y += tile_y
109
110     self.refpix_x -= dx + self.tile_size * tile_x
111     self.refpix_y -= dy + self.tile_size * tile_y
112
113     self.composeMap()
114     self.show()
115
116   def ZoomChange ( self , selector ) :
117     model = selector.get_model(0)
118     active = selector.get_active(0)
119     value = model.get( model.get_iter(active) , 0 )
120     self.SetZoom( int(value[0]) )
121
122   def SetZoom( self , zoom ) :
123     self.hide()
124     lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
125     lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
126     self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
127     self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
128     self.conf.zoom = zoom
129     self.composeMap()
130     self.show()
131
132   def Up( self ) :
133     self.hide()
134     self.reftile_y -= 1
135     self.composeMap()
136     self.show()
137
138   def Down( self ) :
139     self.hide()
140     self.reftile_y += 1
141     self.composeMap()
142     self.show()
143
144   def Right( self ) :
145     self.hide()
146     self.reftile_x += 1
147     self.composeMap()
148     self.show()
149
150   def Left( self ) :
151     self.hide()
152     self.reftile_x -= 1
153     self.composeMap()
154     self.show()
155
156 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
157
158     def __init__ ( self , config , map_size=(800,480) ) :
159         AbstractmapWidget.__init__( self , config , map_size )
160
161         gtk.Image.__init__(self)
162
163         p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
164         self.set_from_pixbuf(p)
165     
166         self.composeMap()
167
168     def composeMap( self ) :
169         center_x , center_y = self.center()
170
171         # Ranges should be long enough as to fill the screen
172         # Maybe they should be decided based on self.win_x, self.win_y
173         for i in range(-3,4) :
174             for j in range(-3,4) :
175                 file = self.tilename( i , j , self.conf.zoom )
176                 if file is None :
177                     pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
178                     pixbuf.fill( 0x00000000 )
179                 else :
180                     try :
181                         pixbuf = gtk.gdk.pixbuf_new_from_file( file )
182                     except gobject.GError , ex :
183                         print "Corrupted file %s" % ( file )
184                         os.unlink( file )
185                         #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
186                         file = self.tilename( i , j , self.conf.zoom )
187                         try :
188                             pixbuf = gtk.gdk.pixbuf_new_from_file( file )
189                         except :
190                             print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
191                             pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
192
193                 dest_x = self.tile_size * i + center_x
194                 dest_y = self.tile_size * j + center_y
195
196                 init_x = 0
197                 size_x = self.tile_size
198                 if dest_x < 0 :
199                    init_x = abs(dest_x)
200                    size_x = self.tile_size + dest_x
201                    dest_x = 0
202                 if dest_x + self.tile_size > self.win_x :
203                    size_x = self.win_x - dest_x
204     
205                 init_y = 0
206                 size_y = self.tile_size
207                 if dest_y < 0 :
208                    init_y = abs(dest_y)
209                    size_y = self.tile_size + dest_y
210                    dest_y = 0
211                 if dest_y + self.tile_size > self.win_y :
212                    size_y = self.win_y - dest_y
213
214                 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
215                     pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
216                 del(pixbuf)
217
218 #        self.draw_paths()
219         self.plot_APs()
220
221     def center( self ) :
222
223         center_x , center_y = self.win_x / 2 , self.win_y / 2
224
225         # To get the central pixel in the window center, we must shift to the tile origin
226         center_x -= self.refpix_x
227         center_y -= self.refpix_y
228
229         return center_x , center_y
230
231     def plot( self , pixmap , coords , colorname , radius=3 ) :
232
233         center_x , center_y = self.center()
234
235         gc = pixmap.new_gc()
236         gc.foreground = pixmap.get_colormap().alloc_color( colorname )
237
238         dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
239         pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
240
241     def draw_paths( self ) :
242
243         pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
244
245         filename = "data/wiscan_gui.info.old"
246         fd = open( filename )
247         for line in fd.readlines() :
248             values = line.split()
249             if values[1] == "FIX" :
250                 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
251         fd.close()
252
253         self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
254
255     def plot_APs( self ) :
256
257         pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
258
259         db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
260         db.open()
261         # NOTE : Intervals for query are just educated guesses to fit in window
262         lat , lon = self.conf.lat , self.conf.lon
263         for ap in db.db.execute( "SELECT * FROM ap where lat/n>%f and lat/n<%f and lon/n>%f and lon/n<%f" % ( lat - 0.003 , lat + 0.003 , lon - 0.007 , lon + 0.007 ) ) :
264             if ap[3] > 1 :
265                 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
266         db.close()
267
268         self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
269
270 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :
271
272     pass
273