12 class AbstractmapWidget :
14 def __init__ ( self , config , map_size=(800,480) ) :
18 # Maximum width should be 800, but actually gets reduced
19 self.win_x , self.win_y = map_size
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 )
25 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
28 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
30 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
33 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
34 return math.degrees( math.atan( math.sinh( tiley ) ) )
36 def SetZoom( self , zoom ) :
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 )
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] )
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] )
55 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
57 x_pos = self.lon2tilex( lon , self.conf.zoom )
58 y_pos = self.lat2tiley( lat , self.conf.zoom )
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]
63 return dest_x , dest_y
65 def tilename ( self , x , y , zoom ) :
66 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
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" :
77 fd = open( file , 'w' )
78 fd.write( response.read() )
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 ) :
90 rootsubdir = "%s/%s" % ( rootdir , tilex )
91 if not os.path.isdir( rootsubdir ) :
93 return "%s/%s.png" % ( rootsubdir , tiley )
95 class interactiveMapWidget :
97 def Shift( self , dx , dy ) :
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
104 self.refpix_x -= dx + self.tile_size * tile_x
105 self.refpix_y -= dy + self.tile_size * tile_y
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]) )
140 class simpleMapWidget ( AbstractmapWidget , gtk.DrawingArea ) :
142 def __init__ ( self , config , map_size=(800,480) ) :
143 AbstractmapWidget.__init__( self , config , map_size )
145 gtk.DrawingArea.__init__(self)
146 self.set_size_request(map_size[0] , map_size[1])
148 self.connect("expose-event", self.expose)
150 def expose(self, area, event):
152 # FIXME : The two below could slow the displaying
156 def composeMap( self ) :
157 center_x , center_y = self.center()
159 gc = self.style.fg_gc[gtk.STATE_NORMAL]
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 )
167 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
168 pixbuf.fill( 0x00000000 )
171 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
172 except gobject.GError , ex :
173 print "Corrupted file %s" % ( 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 )
178 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
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 )
183 dest_x = self.tile_size * i + center_x
184 dest_y = self.tile_size * j + center_y
187 size_x = self.tile_size
190 size_x = self.tile_size + dest_x
192 if dest_x + self.tile_size > self.win_x :
193 size_x = self.win_x - dest_x
196 size_y = self.tile_size
199 size_y = self.tile_size + dest_y
201 if dest_y + self.tile_size > self.win_y :
202 size_y = self.win_y - dest_y
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 )
210 center_x , center_y = self.win_x / 2 , self.win_y / 2
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
216 return center_x , center_y
218 def plot( self , coords , colorname , radius=3 ) :
220 center_x , center_y = self.center()
222 gc = self.window.new_gc()
223 gc.foreground = self.get_colormap().alloc_color( colorname )
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 )
228 def draw_paths( self ) :
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" )
238 def plot_APs( self ) :
240 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
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" )
248 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :