1beb6c8312490dd60343c4cf5dca8a0a710f22b8
[wifihood] / view.py
1 #!/usr/bin/python
2
3 import gtk
4 import gobject
5
6 import urllib2
7 import math
8
9 import os
10
11 class background_map ( gtk.gdk.Pixmap ) :
12
13     def __init__ ( self , map_size , tileloader ) :
14         bordersize = 1
15
16         self.tileloader = tileloader
17
18         # Values for minimun fit without border
19         center = map( lambda x :     int( math.ceil( x / float(tileloader.tilesize) ) / 2 )     , map_size )
20         size = map( lambda x : 2 * x + 1 , center )
21
22         self.center = map( lambda x : x + bordersize , center )
23         self.size = map( lambda x : x + 2 * bordersize , size )
24         pixsize = map( lambda x : x * tileloader.tilesize , self.size )
25
26         # FIXME : seems that reproducing the previous behaviour requires an extra subtraction of 128 to vpor[1]
27         #         when moving to non-integer viewports, the shift turns and adition, and changes to vpor[0]
28         self.vport = [ bordersize * tileloader.tilesize + tileloader.refpix[0] , bordersize * tileloader.tilesize + tileloader.refpix[1] ]
29
30         gtk.gdk.Pixmap.__init__( self , None , pixsize[0] , pixsize[1] , 24 )
31
32         self.fill = map( lambda x : False , range( self.size[0] * self.size[1] ) )
33
34         self.loadtiles()
35
36     def index ( self , x , y ) :
37         return x + y * self.size[0]
38
39     def loadtiles ( self ) :
40
41         for x in range(self.size[0]) :
42             for y in range(self.size[1]) :
43
44                 pixbuf = self.tileloader.get_tile( (x-self.center[0],y-self.center[1]) )
45                 if pixbuf :
46                     self.fill[ self.index(x,y) ] = True
47                 else :
48                     pixbuf = self.tileloader.emptytile()
49
50                 dest_x = self.tileloader.tilesize * x
51                 dest_y = self.tileloader.tilesize * y
52                 self.draw_pixbuf( None , pixbuf , 0 , 0 , dest_x , dest_y )
53
54
55 class tile_loader :
56
57     def __init__ ( self , conf ) :
58         self.tilesize = 256
59         self.rootdir = "%s/%s/%s" % ( conf.mapsdir , conf.mapclass , conf.zoom )
60         self.reftile , self.refpix = self.get_reference( conf )
61
62     def get_reference ( self , conf ) :
63         tilex = self.lon2tilex( conf.lon , conf.zoom )
64         tiley = self.lat2tiley( conf.lat , conf.zoom )
65         tile = tilex[1] , tiley[1] 
66         pix = tilex[0] , tiley[0] 
67         return map( int , tile ) , map( lambda x : int( self.tilesize * x ) , pix )
68
69     def lon2tilex ( self , lon , zoom ) :
70         return math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
71
72     def lat2tiley ( self , lat , zoom ) :
73         lat = lat * math.pi / 180
74         return math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
75
76     def get_tile ( self , tile ) :
77         file = self.tilepath( self.reftile[0] + tile[0] , self.reftile[1] + tile[1] )
78         try :
79             os.stat(file)
80             return gtk.gdk.pixbuf_new_from_file( file )
81         except :
82             try :
83                 # useful members : response.code, response.headers
84                 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
85                 if response.geturl() != "http://tile.openstreetmap.org/11/0/0.png" :
86                     fd = open( file , 'w' )
87                     fd.write( response.read() )
88                     fd.close()
89                     # FIXME : can this actually produce a gobject.GError exception ?
90                     return gtk.gdk.pixbuf_new_from_file( file )
91             except :
92                 pass
93
94         return None
95
96     def tilepath( self , tilex , tiley ) :
97       return "%s/%s/%s.png" % ( self.rootdir , tilex , tiley )
98
99     def emptytile( self ) :
100         pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tilesize, self.tilesize )
101         pixbuf.fill( 0x00000000 )
102         return pixbuf
103
104
105 class AbstractmapWidget :
106
107   def __init__ ( self , config , map_size ) :
108
109     self.conf = config
110
111     # Maximum width should be 800, but actually gets reduced
112     self.win_x , self.win_y = map_size
113     self.tile_size = 256
114
115     self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
116     self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
117
118   def recenter ( self , latlon ) :
119
120         center = self.gps2pix( latlon , self.center() )
121         pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
122
123         distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
124
125         # FIXME : instead of hardcoded, should depend on the actual display size
126         if distance > 150 :
127             self.conf.set_latlon( latlon )
128
129             self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
130             self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
131
132             self.composeMap()
133
134   def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
135         tilex = float(tilex)
136         pixx = float(pixx)
137         return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
138
139   def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
140         tiley = float(tiley)
141         pixy = float(pixy)
142         tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
143         return math.degrees( math.atan( math.sinh( tiley ) ) )
144
145   def SetZoom( self , zoom ) :
146         self.hide()
147         lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
148         lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
149         self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
150         self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
151         self.conf.set_zoom( zoom )
152         self.composeMap()
153         self.show()
154
155   def lon2tilex ( self , lon , zoom ) :
156     number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
157     return int( number[1] ) , int( self.tile_size * number[0] )
158
159   def lat2tiley ( self , lat , zoom ) :
160     lat = lat * math.pi / 180
161     number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
162     return int( number[1] ) , int( self.tile_size * number[0] )
163
164   def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
165
166     x_pos = self.lon2tilex( lon , self.conf.zoom )
167     y_pos = self.lat2tiley( lat , self.conf.zoom )
168
169     dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
170     dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
171
172     return dest_x , dest_y
173
174   def tilename ( self , x , y , zoom ) :
175     file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
176     try :
177       os.stat(file)
178     except :
179     #  if mapDownload :
180       if False :
181         try :
182           # useful members : response.code, response.headers
183           response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
184           if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
185               return None
186           fd = open( file , 'w' )
187           fd.write( response.read() )
188           fd.close()
189         except :
190           return None
191       else :
192         return None
193     return file
194
195   def tile2file( self , tilex , tiley , zoom ) :
196     rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
197     if not os.path.isdir( rootdir ) :
198       os.mkdir(rootdir)
199     rootsubdir = "%s/%s" % ( rootdir , tilex )
200     if not os.path.isdir( rootsubdir ) :
201       os.mkdir(rootsubdir)
202     return "%s/%s.png" % ( rootsubdir , tiley )
203
204 class interactiveMapWidget :
205
206   def Shift( self , dx , dy ) :
207
208 #    tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
209 #    self.reftile_x += tile_x
210 #    self.reftile_y += tile_y
211
212 #    self.refpix_x -= dx + self.tile_size * tile_x
213 #    self.refpix_y -= dy + self.tile_size * tile_y
214
215         self.mapwidget._bg.vport[0] += dx
216         self.mapwidget._bg.vport[1] += dy
217         self.mapwidget.update_background()
218
219   def Up( self ) :
220         self.mapwidget.reftile_y -= 1
221         self.mapwidget._bg.vport[1] += 256
222         self.mapwidget.update_background()
223
224   def Down( self ) :
225         self.mapwidget.reftile_y += 1
226         self.mapwidget._bg.vport[1] -= 256
227         self.mapwidget.update_background()
228
229   def Right( self ) :
230         self.mapwidget.reftile_x += 1
231         self.mapwidget._bg.vport[0] += 256
232         self.mapwidget.update_background()
233
234   def Left( self ) :
235         self.mapwidget.reftile_x -= 1
236         self.mapwidget._bg.vport[0] -= 256
237         self.mapwidget.update_background()
238
239
240 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
241
242     def __init__ ( self , config , map_size=(800,480) ) :
243         AbstractmapWidget.__init__( self , config , map_size )
244
245         gtk.Image.__init__(self)
246
247         self._bg = background_map( map_size , tile_loader( config ) )
248
249         self.update_background()
250     
251     def update_background( self ) :
252         p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x , self.win_y )
253         p.get_from_drawable( self._bg , self._bg.get_colormap() , self._bg.vport[0] , self._bg.vport[1] , 0 , 0 , self.win_x , self.win_y )
254         self.set_from_pixbuf(p)
255     
256     def composeMap( self ) :
257         center_x , center_y = self.center()
258
259         # Ranges should be long enough as to fill the screen
260         # Maybe they should be decided based on self.win_x, self.win_y
261         for i in range(-3,4) :
262             for j in range(-3,4) :
263                 file = self.tilename( i , j , self.conf.zoom )
264                 if file is None :
265                     pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
266                     pixbuf.fill( 0x00000000 )
267                 else :
268                     try :
269                         pixbuf = gtk.gdk.pixbuf_new_from_file( file )
270                     except gobject.GError , ex :
271                         print "Corrupted file %s" % ( file )
272                         os.unlink( file )
273                         #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
274                         file = self.tilename( i , j , self.conf.zoom )
275                         try :
276                             pixbuf = gtk.gdk.pixbuf_new_from_file( file )
277                         except :
278                             print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
279                             pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
280
281                 dest_x = self.tile_size * i + center_x
282                 dest_y = self.tile_size * j + center_y
283
284                 init_x = 0
285                 size_x = self.tile_size
286                 if dest_x < 0 :
287                    init_x = abs(dest_x)
288                    size_x = self.tile_size + dest_x
289                    dest_x = 0
290                 if dest_x + self.tile_size > self.win_x :
291                    size_x = self.win_x - dest_x
292     
293                 init_y = 0
294                 size_y = self.tile_size
295                 if dest_y < 0 :
296                    init_y = abs(dest_y)
297                    size_y = self.tile_size + dest_y
298                    dest_y = 0
299                 if dest_y + self.tile_size > self.win_y :
300                    size_y = self.win_y - dest_y
301
302                 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
303                     pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
304                 del(pixbuf)
305
306 #        self.draw_paths()
307 #        self.plot_APs()
308
309     def center( self ) :
310
311         center_x , center_y = self.win_x / 2 , self.win_y / 2
312
313         # To get the central pixel in the window center, we must shift to the tile origin
314         center_x -= self.refpix_x
315         center_y -= self.refpix_y
316
317         return center_x , center_y
318
319     def plot( self , pixmap , coords , colorname , radius=3 ) :
320
321         center_x , center_y = self.center()
322
323         gc = pixmap.new_gc()
324         gc.foreground = pixmap.get_colormap().alloc_color( colorname )
325
326         dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
327         pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
328
329     def draw_paths( self ) :
330
331         pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
332
333         filename = "data/wiscan_gui.info.old"
334         fd = open( filename )
335         for line in fd.readlines() :
336             values = line.split()
337             if values[1] == "FIX" :
338                 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
339         fd.close()
340
341         self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
342
343     def plot_APs( self ) :
344
345         pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
346
347         db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
348         db.open()
349         # NOTE : Intervals for query are just educated guesses to fit in window
350         lat , lon = self.conf.lat , self.conf.lon
351         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 ) ) :
352             if ap[3] > 1 :
353                 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
354         db.close()
355
356         self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
357
358 class mapWidget ( gtk.EventBox , interactiveMapWidget ) :
359
360     def __init__ ( self , config ) :
361         gtk.EventBox.__init__( self )
362         self.mapwidget = simpleMapWidget( config )
363         self.add( self.mapwidget )
364
365         self.click_x , self.click_y = None , None
366         self.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
367         self.connect_object('button_press_event', self.press_event, self.mapwidget)
368         self.connect_object('button_release_event', self.release_event, self)
369
370     def press_event ( self , widget , event ) :
371         self.click_x , self.click_y = event.x , event.y
372
373     def release_event ( self , widget, event ) :
374         delta_x = int( self.click_x - event.x )
375         delta_y = int( self.click_y - event.y )
376         widget.Shift(delta_x, delta_y)
377         self.click_x , self.click_y = None , None
378
379 if __name__ == "__main__" :
380
381     class StaticConfiguration :
382
383         def __init__ ( self , type=None ) :
384             self._type = type
385
386             self.homedir , self.dbname = None , None
387             self.mapsdir , self.mapclass = "/boot/home/localuser/Maps" , "OpenStreetMap I"
388
389             self.store_log , self.use_mapper , self.store_gps = None , None , None
390
391             self.lat , self.lon = 40.416 , -3.683
392             self.zoom = 15
393
394     def on_key_press ( widget, event , map ) :
395         if event.keyval == gtk.keysyms.Up :
396             map.Up()
397         elif event.keyval == gtk.keysyms.Down :
398             map.Down()
399         elif event.keyval == gtk.keysyms.Right :
400             map.Right()
401         elif event.keyval == gtk.keysyms.Left :
402             map.Left()
403         else :
404             print "UNKNOWN",event.keyval
405
406     config = StaticConfiguration()
407     map = mapWidget( config )
408     window = gtk.Window()
409     window.connect("destroy", gtk.main_quit )
410     window.connect("key-press-event", on_key_press, map )
411     window.add( map )
412     window.show_all()
413     gtk.main()
414