10 class background_map ( gtk.gdk.Pixmap ) :
12 def __init__ ( self , map_size , tileloader ) :
15 self.tileloader = tileloader
17 # Values for minimun fit without border
18 center = map( lambda x : int( math.ceil( x / float(tileloader.tilesize) ) / 2 ) , map_size )
19 size = map( lambda x : 2 * x + 1 , center )
21 self.center = map( lambda x : x + bordersize , center )
22 self.size = map( lambda x : x + 2 * bordersize , size )
23 pixsize = map( lambda x : x * tileloader.tilesize , self.size )
25 gtk.gdk.Pixmap.__init__( self , None , pixsize[0] , pixsize[1] , 24 )
27 self.fill = map( lambda x : False , range( self.size[0] * self.size[1] ) )
31 def index ( self , x , y ) :
32 return x + y * self.size[0]
34 def loadtiles ( self ) :
36 for x in range(self.size[0]) :
37 for y in range(self.size[1]) :
39 pixbuf = self.tileloader.get_tile( (x,y) )
41 self.fill[ self.index(x,y) ] = True
43 pixbuf = self.tileloader.emptytile()
45 dest_x = self.tileloader.tilesize * x
46 dest_y = self.tileloader.tilesize * y
47 self.draw_pixbuf( None , pixbuf , 0 , 0 , dest_x , dest_y )
52 def __init__ ( self , conf ) :
54 self.rootdir = "%s/%s/%s" % ( conf.mapsdir , conf.mapclass , conf.zoom )
55 self.reftile , self.refpix = self.get_reference( conf )
57 def get_reference ( self , conf ) :
58 tilex = self.lon2tilex( conf.lon , conf.zoom )
59 tiley = self.lat2tiley( conf.lat , conf.zoom )
60 tile = tilex[1] , tiley[1]
61 pix = tilex[0] , tiley[0]
62 return map( int , tile ) , map( lambda x : int( self.tilesize * x ) , pix )
64 def lon2tilex ( self , lon , zoom ) :
65 return math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
67 def lat2tiley ( self , lat , zoom ) :
68 lat = lat * math.pi / 180
69 return math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
71 def get_tile ( self , tile ) :
72 file = self.tilepath( self.reftile[0] + tile[0] , self.reftile[1] + tile[1] )
75 return gtk.gdk.pixbuf_new_from_file( file )
78 # useful members : response.code, response.headers
79 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
80 if response.geturl() != "http://tile.openstreetmap.org/11/0/0.png" :
81 fd = open( file , 'w' )
82 fd.write( response.read() )
84 # FIXME : can this actually produce a gobject.GError exception ?
85 return gtk.gdk.pixbuf_new_from_file( file )
91 def tilepath( self , tilex , tiley ) :
92 return "%s/%s/%s.png" % ( self.rootdir , tilex , tiley )
94 def emptytile( self ) :
95 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tilesize, self.tilesize )
96 pixbuf.fill( 0x00000000 )
100 class AbstractmapWidget :
102 def __init__ ( self , config , map_size ) :
106 # Maximum width should be 800, but actually gets reduced
107 self.win_x , self.win_y = map_size
110 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
111 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
113 def recenter ( self , latlon ) :
115 center = self.gps2pix( latlon , self.center() )
116 pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
118 distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
120 # FIXME : instead of hardcoded, should depend on the actual display size
122 self.conf.set_latlon( latlon )
124 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
125 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
129 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
132 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
134 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
137 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
138 return math.degrees( math.atan( math.sinh( tiley ) ) )
140 def SetZoom( self , zoom ) :
142 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
143 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
144 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
145 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
146 self.conf.set_zoom( zoom )
150 def lon2tilex ( self , lon , zoom ) :
151 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
152 return int( number[1] ) , int( self.tile_size * number[0] )
154 def lat2tiley ( self , lat , zoom ) :
155 lat = lat * math.pi / 180
156 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
157 return int( number[1] ) , int( self.tile_size * number[0] )
159 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
161 x_pos = self.lon2tilex( lon , self.conf.zoom )
162 y_pos = self.lat2tiley( lat , self.conf.zoom )
164 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
165 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
167 return dest_x , dest_y
169 def tilename ( self , x , y , zoom ) :
170 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
177 # useful members : response.code, response.headers
178 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
179 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
181 fd = open( file , 'w' )
182 fd.write( response.read() )
190 def tile2file( self , tilex , tiley , zoom ) :
191 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
192 if not os.path.isdir( rootdir ) :
194 rootsubdir = "%s/%s" % ( rootdir , tilex )
195 if not os.path.isdir( rootsubdir ) :
197 return "%s/%s.png" % ( rootsubdir , tiley )
199 class interactiveMapWidget :
201 def Shift( self , dx , dy ) :
204 tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
205 self.reftile_x += tile_x
206 self.reftile_y += tile_y
208 self.refpix_x -= dx + self.tile_size * tile_x
209 self.refpix_y -= dy + self.tile_size * tile_y
238 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
240 def __init__ ( self , config , map_size=(800,480) ) :
241 AbstractmapWidget.__init__( self , config , map_size )
243 gtk.Image.__init__(self)
245 self._bg = background_map( map_size , tile_loader( config ) )
247 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, map_size[0] , map_size[1] )
248 p.get_from_drawable( self._bg , self._bg.get_colormap() , 0, 0 , 0, 0 , map_size[0] , map_size[1] )
249 self.set_from_pixbuf(p)
251 def composeMap( self ) :
252 center_x , center_y = self.center()
254 # Ranges should be long enough as to fill the screen
255 # Maybe they should be decided based on self.win_x, self.win_y
256 for i in range(-3,4) :
257 for j in range(-3,4) :
258 file = self.tilename( i , j , self.conf.zoom )
260 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
261 pixbuf.fill( 0x00000000 )
264 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
265 except gobject.GError , ex :
266 print "Corrupted file %s" % ( file )
268 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
269 file = self.tilename( i , j , self.conf.zoom )
271 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
273 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
274 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
276 dest_x = self.tile_size * i + center_x
277 dest_y = self.tile_size * j + center_y
280 size_x = self.tile_size
283 size_x = self.tile_size + dest_x
285 if dest_x + self.tile_size > self.win_x :
286 size_x = self.win_x - dest_x
289 size_y = self.tile_size
292 size_y = self.tile_size + dest_y
294 if dest_y + self.tile_size > self.win_y :
295 size_y = self.win_y - dest_y
297 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
298 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
306 center_x , center_y = self.win_x / 2 , self.win_y / 2
308 # To get the central pixel in the window center, we must shift to the tile origin
309 center_x -= self.refpix_x
310 center_y -= self.refpix_y
312 return center_x , center_y
314 def plot( self , pixmap , coords , colorname , radius=3 ) :
316 center_x , center_y = self.center()
319 gc.foreground = pixmap.get_colormap().alloc_color( colorname )
321 dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
322 pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
324 def draw_paths( self ) :
326 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
328 filename = "data/wiscan_gui.info.old"
329 fd = open( filename )
330 for line in fd.readlines() :
331 values = line.split()
332 if values[1] == "FIX" :
333 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
336 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
338 def plot_APs( self ) :
340 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
342 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
344 # NOTE : Intervals for query are just educated guesses to fit in window
345 lat , lon = self.conf.lat , self.conf.lon
346 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 ) ) :
348 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
351 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
353 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :
357 if __name__ == "__main__" :
359 class StaticConfiguration :
361 def __init__ ( self , type=None ) :
364 self.homedir , self.dbname = None , None
365 self.mapsdir , self.mapclass = "/boot/home/localuser/Maps" , "OpenStreetMap I"
367 self.store_log , self.use_mapper , self.store_gps = None , None , None
369 self.lat , self.lon = 40.416 , -3.683
372 def on_key_press ( widget, event , map ) :
373 if event.keyval == gtk.keysyms.Up :
375 elif event.keyval == gtk.keysyms.Down :
377 elif event.keyval == gtk.keysyms.Right :
379 elif event.keyval == gtk.keysyms.Left :
382 print "UNKNOWN",event.keyval
384 config = StaticConfiguration()
385 map = mapWidget( config )
386 window = gtk.Window()
387 window.connect("destroy", gtk.main_quit )
388 #window.connect_object("destroy", gtk.Window.destroy , window )
389 window.connect("key-press-event", on_key_press, map )