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 recenter ( self , latlon ) :
27 center = self.gps2pix( latlon , self.center() )
28 pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
30 distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
32 # FIXME : instead of hardcoded, should depend on the actual display size
34 self.conf.set_latlon( latlon )
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 )
41 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
44 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
46 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
49 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
50 return math.degrees( math.atan( math.sinh( tiley ) ) )
52 def SetZoom( self , zoom ) :
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 )
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] )
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] )
71 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
73 x_pos = self.lon2tilex( lon , self.conf.zoom )
74 y_pos = self.lat2tiley( lat , self.conf.zoom )
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]
79 return dest_x , dest_y
81 def tilename ( self , x , y , zoom ) :
82 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
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" :
93 fd = open( file , 'w' )
94 fd.write( response.read() )
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 ) :
106 rootsubdir = "%s/%s" % ( rootdir , tilex )
107 if not os.path.isdir( rootsubdir ) :
109 return "%s/%s.png" % ( rootsubdir , tiley )
111 class interactiveMapWidget :
113 def Shift( self , dx , dy ) :
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
120 self.refpix_x -= dx + self.tile_size * tile_x
121 self.refpix_y -= dy + self.tile_size * tile_y
150 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
152 def __init__ ( self , config , map_size=(800,480) ) :
153 AbstractmapWidget.__init__( self , config , map_size )
155 gtk.Image.__init__(self)
157 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
158 self.set_from_pixbuf(p)
162 def composeMap( self ) :
163 center_x , center_y = self.center()
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 )
171 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
172 pixbuf.fill( 0x00000000 )
175 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
176 except gobject.GError , ex :
177 print "Corrupted file %s" % ( 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 )
182 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
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 )
187 dest_x = self.tile_size * i + center_x
188 dest_y = self.tile_size * j + center_y
191 size_x = self.tile_size
194 size_x = self.tile_size + dest_x
196 if dest_x + self.tile_size > self.win_x :
197 size_x = self.win_x - dest_x
200 size_y = self.tile_size
203 size_y = self.tile_size + dest_y
205 if dest_y + self.tile_size > self.win_y :
206 size_y = self.win_y - dest_y
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 )
217 center_x , center_y = self.win_x / 2 , self.win_y / 2
219 # To get the central pixel in the window center, we must shift to the tile origin
220 center_x -= self.refpix_x
221 center_y -= self.refpix_y
223 return center_x , center_y
225 def plot( self , pixmap , coords , colorname , radius=3 ) :
227 center_x , center_y = self.center()
230 gc.foreground = pixmap.get_colormap().alloc_color( colorname )
232 dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
233 pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
235 def draw_paths( self ) :
237 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
239 filename = "data/wiscan_gui.info.old"
240 fd = open( filename )
241 for line in fd.readlines() :
242 values = line.split()
243 if values[1] == "FIX" :
244 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
247 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
249 def plot_APs( self ) :
251 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
253 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
255 # NOTE : Intervals for query are just educated guesses to fit in window
256 lat , lon = self.conf.lat , self.conf.lon
257 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 ) ) :
259 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
262 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
264 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :