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