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.lat , self.conf.lon = 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 lon2tilex ( self , lon , zoom ) :
42 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
43 return int( number[1] ) , int( self.tile_size * number[0] )
45 def lat2tiley ( self , lat , zoom ) :
46 lat = lat * math.pi / 180
47 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
48 return int( number[1] ) , int( self.tile_size * number[0] )
50 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
52 x_pos = self.lon2tilex( lon , self.conf.zoom )
53 y_pos = self.lat2tiley( lat , self.conf.zoom )
55 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
56 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
58 return dest_x , dest_y
60 def tilename ( self , x , y , zoom ) :
61 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
68 # useful members : response.code, response.headers
69 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
70 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
72 fd = open( file , 'w' )
73 fd.write( response.read() )
81 def tile2file( self , tilex , tiley , zoom ) :
82 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
83 if not os.path.isdir( rootdir ) :
85 rootsubdir = "%s/%s" % ( rootdir , tilex )
86 if not os.path.isdir( rootsubdir ) :
88 return "%s/%s.png" % ( rootsubdir , tiley )
90 class interactiveMapWidget :
92 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
95 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
97 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
100 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
101 return math.degrees( math.atan( math.sinh( tiley ) ) )
103 def Shift( self , dx , dy ) :
106 tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
107 self.reftile_x += tile_x
108 self.reftile_y += tile_y
110 self.refpix_x -= dx + self.tile_size * tile_x
111 self.refpix_y -= dy + self.tile_size * tile_y
116 def ZoomChange ( self , selector ) :
117 model = selector.get_model(0)
118 active = selector.get_active(0)
119 value = model.get( model.get_iter(active) , 0 )
120 self.SetZoom( int(value[0]) )
122 def SetZoom( self , zoom ) :
124 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
125 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
126 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
127 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
128 self.conf.zoom = zoom
156 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
158 def __init__ ( self , config , map_size=(800,480) ) :
159 AbstractmapWidget.__init__( self , config , map_size )
161 gtk.Image.__init__(self)
163 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
164 self.set_from_pixbuf(p)
168 def composeMap( self ) :
169 center_x , center_y = self.center()
171 # Ranges should be long enough as to fill the screen
172 # Maybe they should be decided based on self.win_x, self.win_y
173 for i in range(-3,4) :
174 for j in range(-3,4) :
175 file = self.tilename( i , j , self.conf.zoom )
177 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
178 pixbuf.fill( 0x00000000 )
181 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
182 except gobject.GError , ex :
183 print "Corrupted file %s" % ( file )
185 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
186 file = self.tilename( i , j , self.conf.zoom )
188 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
190 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
191 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
193 dest_x = self.tile_size * i + center_x
194 dest_y = self.tile_size * j + center_y
197 size_x = self.tile_size
200 size_x = self.tile_size + dest_x
202 if dest_x + self.tile_size > self.win_x :
203 size_x = self.win_x - dest_x
206 size_y = self.tile_size
209 size_y = self.tile_size + dest_y
211 if dest_y + self.tile_size > self.win_y :
212 size_y = self.win_y - dest_y
214 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
215 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
223 center_x , center_y = self.win_x / 2 , self.win_y / 2
225 # To get the central pixel in the window center, we must shift to the tile origin
226 center_x -= self.refpix_x
227 center_y -= self.refpix_y
229 return center_x , center_y
231 def plot( self , pixmap , coords , colorname , radius=3 ) :
233 center_x , center_y = self.center()
236 gc.foreground = pixmap.get_colormap().alloc_color( colorname )
238 dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
239 pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
241 def draw_paths( self ) :
243 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
245 filename = "data/wiscan_gui.info.old"
246 fd = open( filename )
247 for line in fd.readlines() :
248 values = line.split()
249 if values[1] == "FIX" :
250 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
253 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
255 def plot_APs( self ) :
257 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
259 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
261 # NOTE : Intervals for query are just educated guesses to fit in window
262 lat , lon = self.conf.lat , self.conf.lon
263 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 ) ) :
265 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
268 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
270 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :