11 class background_map ( gtk.gdk.Pixmap ) :
13 def __init__ ( self , map_size , tileloader ) :
16 self.tileloader = tileloader
18 # Values for minimun fit without border
19 center = map( lambda x : int( math.ceil( x / float(tileloader.tilesize) ) / 2 ) , map_size )
20 size = map( lambda x : 2 * x + 1 , center )
22 self.center = map( lambda x : x + bordersize , center )
23 self.size = map( lambda x : x + 2 * bordersize , size )
24 pixsize = map( lambda x : x * tileloader.tilesize , self.size )
26 # FIXME : seems that reproducing the previous behaviour requires an extra subtraction of 128 to vpor[1]
27 # when moving to non-integer viewports, the shift turns and adition, and changes to vpor[0]
28 self.vport = [ bordersize * tileloader.tilesize + tileloader.refpix[0] , bordersize * tileloader.tilesize + tileloader.refpix[1] ]
30 gtk.gdk.Pixmap.__init__( self , None , pixsize[0] , pixsize[1] , 24 )
32 self.fill = map( lambda x : False , range( self.size[0] * self.size[1] ) )
36 def index ( self , x , y ) :
37 return x + y * self.size[0]
39 def loadtiles ( self ) :
41 for x in range(self.size[0]) :
42 for y in range(self.size[1]) :
44 if not self.fill[ self.index(x,y) ] :
45 pixbuf = self.tileloader.get_tile( (x-self.center[0],y-self.center[1]) )
47 self.fill[ self.index(x,y) ] = True
49 pixbuf = self.tileloader.emptytile()
51 dest_x = self.tileloader.tilesize * x
52 dest_y = self.tileloader.tilesize * y
53 self.draw_pixbuf( None , pixbuf , 0 , 0 , dest_x , dest_y )
56 def do_change_reftile( self , dx , dy ) :
57 self.tileloader.reftile[0] += dx
58 self.tileloader.reftile[1] += dy
60 pixsize = self.get_size()
61 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, pixsize[0] , pixsize[1] )
62 pixbuf.get_from_drawable( self , self.get_colormap() , 0 , 0 , 0 , 0 , pixsize[0] , pixsize[1] )
64 # width , source , destination
65 x_vals = [ pixsize[0] , 0 , 0 ]
66 y_vals = [ pixsize[1] , 0 , 0 ]
69 x_vals[0] -= self.tileloader.tilesize
70 x_vals[dx] = self.tileloader.tilesize
72 for y in range(self.size[1]) : self.fill[ self.index(self.size[0]-1,y) ] = False
74 for y in range(self.size[1]) : self.fill[ self.index(0,y) ] = False
76 y_vals[0] -= self.tileloader.tilesize
77 y_vals[dy] = self.tileloader.tilesize
79 for x in range(self.size[0]) : self.fill[ self.index(x,self.size[1]-1) ] = False
81 for x in range(self.size[0]) : self.fill[ self.index(x,0) ] = False
83 self.draw_pixbuf( None , pixbuf , x_vals[1] , y_vals[1] , x_vals[-1] , y_vals[-1] , x_vals[0] , y_vals[0] )
89 def __init__ ( self , conf ) :
91 self.rootdir = "%s/%s/%s" % ( conf.mapsdir , conf.mapclass , conf.zoom )
92 self.reftile , self.refpix = self.get_reference( conf )
94 def get_reference ( self , conf ) :
95 tilex = self.lon2tilex( conf.lon , conf.zoom )
96 tiley = self.lat2tiley( conf.lat , conf.zoom )
97 tile = tilex[1] , tiley[1]
98 pix = tilex[0] , tiley[0]
99 return map( int , tile ) , map( lambda x : int( self.tilesize * x ) , pix )
101 def lon2tilex ( self , lon , zoom ) :
102 return math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
104 def lat2tiley ( self , lat , zoom ) :
105 lat = lat * math.pi / 180
106 return math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
108 def get_tile ( self , tile ) :
109 file = self.tilepath( self.reftile[0] + tile[0] , self.reftile[1] + tile[1] )
112 return gtk.gdk.pixbuf_new_from_file( file )
115 # useful members : response.code, response.headers
116 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
117 if response.geturl() != "http://tile.openstreetmap.org/11/0/0.png" :
118 fd = open( file , 'w' )
119 fd.write( response.read() )
121 # FIXME : can this actually produce a gobject.GError exception ?
122 return gtk.gdk.pixbuf_new_from_file( file )
128 def tilepath( self , tilex , tiley ) :
129 return "%s/%s/%s.png" % ( self.rootdir , tilex , tiley )
131 def emptytile( self ) :
132 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tilesize, self.tilesize )
133 pixbuf.fill( 0x00000000 )
137 class AbstractmapWidget :
139 def __init__ ( self , config , map_size ) :
143 # Maximum width should be 800, but actually gets reduced
144 self.win_x , self.win_y = map_size
147 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
148 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
150 def recenter ( self , latlon ) :
152 center = self.gps2pix( latlon , self.center() )
153 pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
155 distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
157 # FIXME : instead of hardcoded, should depend on the actual display size
159 self.conf.set_latlon( latlon )
161 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
162 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
166 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
169 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
171 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
174 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
175 return math.degrees( math.atan( math.sinh( tiley ) ) )
177 def SetZoom( self , zoom ) :
179 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
180 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
181 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
182 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
183 self.conf.set_zoom( zoom )
187 def lon2tilex ( self , lon , zoom ) :
188 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
189 return int( number[1] ) , int( self.tile_size * number[0] )
191 def lat2tiley ( self , lat , zoom ) :
192 lat = lat * math.pi / 180
193 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
194 return int( number[1] ) , int( self.tile_size * number[0] )
196 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
198 x_pos = self.lon2tilex( lon , self.conf.zoom )
199 y_pos = self.lat2tiley( lat , self.conf.zoom )
201 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
202 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
204 return dest_x , dest_y
206 def tilename ( self , x , y , zoom ) :
207 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
214 # useful members : response.code, response.headers
215 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
216 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
218 fd = open( file , 'w' )
219 fd.write( response.read() )
227 def tile2file( self , tilex , tiley , zoom ) :
228 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
229 if not os.path.isdir( rootdir ) :
231 rootsubdir = "%s/%s" % ( rootdir , tilex )
232 if not os.path.isdir( rootsubdir ) :
234 return "%s/%s.png" % ( rootsubdir , tiley )
236 class interactiveMapWidget :
238 def Shift( self , dx , dy ) :
240 # tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
241 # self.reftile_x += tile_x
242 # self.reftile_y += tile_y
244 # self.refpix_x -= dx + self.tile_size * tile_x
245 # self.refpix_y -= dy + self.tile_size * tile_y
247 self.mapwidget._bg.vport[0] += dx
248 self.mapwidget._bg.vport[1] += dy
249 self.mapwidget.update_background()
252 self.mapwidget._bg.do_change_reftile( 0 , -1 )
253 self.mapwidget.update_background()
256 self.mapwidget._bg.do_change_reftile( 0 , +1 )
257 self.mapwidget.update_background()
260 self.mapwidget._bg.do_change_reftile( +1 , 0 )
261 self.mapwidget.update_background()
264 self.mapwidget._bg.do_change_reftile( -1 , 0 )
265 self.mapwidget.update_background()
268 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
270 def __init__ ( self , config , map_size=(800,480) ) :
271 AbstractmapWidget.__init__( self , config , map_size )
273 gtk.Image.__init__(self)
275 self._bg = background_map( map_size , tile_loader( config ) )
277 self.update_background()
279 def update_background( self ) :
280 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x , self.win_y )
281 p.get_from_drawable( self._bg , self._bg.get_colormap() , self._bg.vport[0] , self._bg.vport[1] , 0 , 0 , self.win_x , self.win_y )
282 self.set_from_pixbuf(p)
284 def composeMap( self ) :
285 center_x , center_y = self.center()
287 # Ranges should be long enough as to fill the screen
288 # Maybe they should be decided based on self.win_x, self.win_y
289 for i in range(-3,4) :
290 for j in range(-3,4) :
291 file = self.tilename( i , j , self.conf.zoom )
293 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
294 pixbuf.fill( 0x00000000 )
297 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
298 except gobject.GError , ex :
299 print "Corrupted file %s" % ( file )
301 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
302 file = self.tilename( i , j , self.conf.zoom )
304 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
306 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
307 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
309 dest_x = self.tile_size * i + center_x
310 dest_y = self.tile_size * j + center_y
313 size_x = self.tile_size
316 size_x = self.tile_size + dest_x
318 if dest_x + self.tile_size > self.win_x :
319 size_x = self.win_x - dest_x
322 size_y = self.tile_size
325 size_y = self.tile_size + dest_y
327 if dest_y + self.tile_size > self.win_y :
328 size_y = self.win_y - dest_y
330 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
331 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
339 center_x , center_y = self.win_x / 2 , self.win_y / 2
341 # To get the central pixel in the window center, we must shift to the tile origin
342 center_x -= self.refpix_x
343 center_y -= self.refpix_y
345 return center_x , center_y
347 def plot( self , pixmap , coords , colorname , radius=3 ) :
349 center_x , center_y = self.center()
352 gc.foreground = pixmap.get_colormap().alloc_color( colorname )
354 dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
355 pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
357 def draw_paths( self ) :
359 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
361 filename = "data/wiscan_gui.info.old"
362 fd = open( filename )
363 for line in fd.readlines() :
364 values = line.split()
365 if values[1] == "FIX" :
366 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
369 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
371 def plot_APs( self ) :
373 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
375 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
377 # NOTE : Intervals for query are just educated guesses to fit in window
378 lat , lon = self.conf.lat , self.conf.lon
379 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 ) ) :
381 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
384 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
386 class mapWidget ( gtk.EventBox , interactiveMapWidget ) :
388 def __init__ ( self , config ) :
389 gtk.EventBox.__init__( self )
390 self.mapwidget = simpleMapWidget( config )
391 self.add( self.mapwidget )
393 self.click_x , self.click_y = None , None
394 self.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
395 self.connect_object('button_press_event', self.press_event, self.mapwidget)
396 self.connect_object('button_release_event', self.release_event, self)
398 def press_event ( self , widget , event ) :
399 self.click_x , self.click_y = event.x , event.y
401 def release_event ( self , widget, event ) :
402 delta_x = int( self.click_x - event.x )
403 delta_y = int( self.click_y - event.y )
404 widget.Shift(delta_x, delta_y)
405 self.click_x , self.click_y = None , None
407 if __name__ == "__main__" :
409 class StaticConfiguration :
411 def __init__ ( self , type=None ) :
414 self.homedir , self.dbname = None , None
415 self.mapsdir , self.mapclass = "/boot/home/localuser/Maps" , "OpenStreetMap I"
417 self.store_log , self.use_mapper , self.store_gps = None , None , None
419 self.lat , self.lon = 40.416 , -3.683
422 def on_key_press ( widget, event , map ) :
423 if event.keyval == gtk.keysyms.Up :
425 elif event.keyval == gtk.keysyms.Down :
427 elif event.keyval == gtk.keysyms.Right :
429 elif event.keyval == gtk.keysyms.Left :
432 print "UNKNOWN",event.keyval
434 config = StaticConfiguration()
435 mapwidget = mapWidget( config )
436 window = gtk.Window()
437 window.connect("destroy", gtk.main_quit )
438 window.connect("key-press-event", on_key_press, mapwidget )
439 window.add( mapwidget )