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 lon2tilex ( self , lon , zoom ) :
26 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
27 return int( number[1] ) , int( self.tile_size * number[0] )
29 def lat2tiley ( self , lat , zoom ) :
30 lat = lat * math.pi / 180
31 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
32 return int( number[1] ) , int( self.tile_size * number[0] )
34 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
36 x_pos = self.lon2tilex( lon , self.conf.zoom )
37 y_pos = self.lat2tiley( lat , self.conf.zoom )
39 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
40 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
42 return dest_x , dest_y
44 def tilename ( self , x , y , zoom ) :
45 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
52 # useful members : response.code, response.headers
53 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
54 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
56 fd = open( file , 'w' )
57 fd.write( response.read() )
65 def tile2file( self , tilex , tiley , zoom ) :
66 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
67 if not os.path.isdir( rootdir ) :
69 rootsubdir = "%s/%s" % ( rootdir , tilex )
70 if not os.path.isdir( rootsubdir ) :
72 return "%s/%s.png" % ( rootsubdir , tiley )
74 class interactiveMapWidget :
76 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
79 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
81 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
84 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
85 return math.degrees( math.atan( math.sinh( tiley ) ) )
87 def Shift( self , dx , dy ) :
90 tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
91 self.reftile_x += tile_x
92 self.reftile_y += tile_y
94 self.refpix_x -= dx + self.tile_size * tile_x
95 self.refpix_y -= dy + self.tile_size * tile_y
100 def ZoomChange ( self , selector ) :
101 model = selector.get_model(0)
102 active = selector.get_active(0)
103 value = model.get( model.get_iter(active) , 0 )
104 self.SetZoom( int(value[0]) )
106 def SetZoom( self , zoom ) :
108 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
109 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
110 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
111 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
112 self.conf.zoom = zoom
140 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
142 def __init__ ( self , config , map_size=(800,480) ) :
143 AbstractmapWidget.__init__( self , config , map_size )
145 gtk.Image.__init__(self)
147 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
148 self.set_from_pixbuf(p)
152 def composeMap( self ) :
153 center_x , center_y = self.win_x / 2 , self.win_y / 2
155 # To get the central pixel in the window center, we must shift to the tile origin
156 center_x -= self.refpix_x
157 center_y -= self.refpix_y
159 # Ranges should be long enough as to fill the screen
160 # Maybe they should be decided based on self.win_x, self.win_y
161 for i in range(-3,4) :
162 for j in range(-3,4) :
163 file = self.tilename( i , j , self.conf.zoom )
165 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
166 pixbuf.fill( 0x00000000 )
169 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
170 except gobject.GError , ex :
171 print "Corrupted file %s" % ( file )
173 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
174 file = self.tilename( i , j , self.conf.zoom )
176 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
178 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
179 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
181 dest_x = self.tile_size * i + center_x
182 dest_y = self.tile_size * j + center_y
185 size_x = self.tile_size
188 size_x = self.tile_size + dest_x
190 if dest_x + self.tile_size > self.win_x :
191 size_x = self.win_x - dest_x
194 size_y = self.tile_size
197 size_y = self.tile_size + dest_y
199 if dest_y + self.tile_size > self.win_y :
200 size_y = self.win_y - dest_y
202 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
203 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
209 def draw_paths( self ) :
211 center_x , center_y = self.win_x / 2 , self.win_y / 2
213 # To get the central pixel in the window center, we must shift to the tile origin
214 center_x -= self.refpix_x
215 center_y -= self.refpix_y
217 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
218 red = pixmap.new_gc()
219 red.foreground = pixmap.get_colormap().alloc_color("red")
220 green = pixmap.new_gc()
221 green.foreground = pixmap.get_colormap().alloc_color("green")
222 blue = pixmap.new_gc()
223 blue.foreground = pixmap.get_colormap().alloc_color("blue")
225 filename = "data/wiscan_gui.info.old"
226 fd = open( filename )
227 for line in fd.readlines() :
228 values = line.split()
229 if values[1] == "FIX" :
230 dest_x , dest_y = self.gps2pix( ( float(values[5]) , float(values[6]) ) , ( center_x , center_y ) )
231 pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
234 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
236 def plot_APs( self ) :
238 center_x , center_y = self.win_x / 2 , self.win_y / 2
240 # To get the central pixel in the window center, we must shift to the tile origin
241 center_x -= self.refpix_x
242 center_y -= self.refpix_y
244 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
245 blue = pixmap.new_gc()
246 blue.foreground = pixmap.get_colormap().alloc_color("blue")
248 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
250 # NOTE : Intervals for query are just educated guesses to fit in window
251 lat , lon = self.conf.lat , self.conf.lon
252 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 ) ) :
254 dest_x , dest_y = self.gps2pix( ( ap[4]/ap[3] , ap[5]/ap[3] ) , ( center_x , center_y ) )
255 pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
258 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
260 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :