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