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