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