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 pixbuf = self.tileloader.get_tile( (x-self.center[0],y-self.center[1]) )
46 self.fill[ self.index(x,y) ] = True
48 pixbuf = self.tileloader.emptytile()
50 dest_x = self.tileloader.tilesize * x
51 dest_y = self.tileloader.tilesize * y
52 self.draw_pixbuf( None , pixbuf , 0 , 0 , dest_x , dest_y )
57 def __init__ ( self , conf ) :
59 self.rootdir = "%s/%s/%s" % ( conf.mapsdir , conf.mapclass , conf.zoom )
60 self.reftile , self.refpix = self.get_reference( conf )
62 def get_reference ( self , conf ) :
63 tilex = self.lon2tilex( conf.lon , conf.zoom )
64 tiley = self.lat2tiley( conf.lat , conf.zoom )
65 tile = tilex[1] , tiley[1]
66 pix = tilex[0] , tiley[0]
67 return map( int , tile ) , map( lambda x : int( self.tilesize * x ) , pix )
69 def lon2tilex ( self , lon , zoom ) :
70 return math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
72 def lat2tiley ( self , lat , zoom ) :
73 lat = lat * math.pi / 180
74 return math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
76 def get_tile ( self , tile ) :
77 file = self.tilepath( self.reftile[0] + tile[0] , self.reftile[1] + tile[1] )
80 return gtk.gdk.pixbuf_new_from_file( file )
83 # useful members : response.code, response.headers
84 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
85 if response.geturl() != "http://tile.openstreetmap.org/11/0/0.png" :
86 fd = open( file , 'w' )
87 fd.write( response.read() )
89 # FIXME : can this actually produce a gobject.GError exception ?
90 return gtk.gdk.pixbuf_new_from_file( file )
96 def tilepath( self , tilex , tiley ) :
97 return "%s/%s/%s.png" % ( self.rootdir , tilex , tiley )
99 def emptytile( self ) :
100 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tilesize, self.tilesize )
101 pixbuf.fill( 0x00000000 )
105 class AbstractmapWidget :
107 def __init__ ( self , config , map_size ) :
111 # Maximum width should be 800, but actually gets reduced
112 self.win_x , self.win_y = map_size
115 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
116 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
118 def recenter ( self , latlon ) :
120 center = self.gps2pix( latlon , self.center() )
121 pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
123 distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
125 # FIXME : instead of hardcoded, should depend on the actual display size
127 self.conf.set_latlon( latlon )
129 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
130 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
134 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
137 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
139 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
142 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
143 return math.degrees( math.atan( math.sinh( tiley ) ) )
145 def SetZoom( self , zoom ) :
147 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
148 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
149 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
150 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
151 self.conf.set_zoom( zoom )
155 def lon2tilex ( self , lon , zoom ) :
156 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
157 return int( number[1] ) , int( self.tile_size * number[0] )
159 def lat2tiley ( self , lat , zoom ) :
160 lat = lat * math.pi / 180
161 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
162 return int( number[1] ) , int( self.tile_size * number[0] )
164 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
166 x_pos = self.lon2tilex( lon , self.conf.zoom )
167 y_pos = self.lat2tiley( lat , self.conf.zoom )
169 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
170 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
172 return dest_x , dest_y
174 def tilename ( self , x , y , zoom ) :
175 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
182 # useful members : response.code, response.headers
183 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
184 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
186 fd = open( file , 'w' )
187 fd.write( response.read() )
195 def tile2file( self , tilex , tiley , zoom ) :
196 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
197 if not os.path.isdir( rootdir ) :
199 rootsubdir = "%s/%s" % ( rootdir , tilex )
200 if not os.path.isdir( rootsubdir ) :
202 return "%s/%s.png" % ( rootsubdir , tiley )
204 class interactiveMapWidget :
206 def Shift( self , dx , dy ) :
208 # tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
209 # self.reftile_x += tile_x
210 # self.reftile_y += tile_y
212 # self.refpix_x -= dx + self.tile_size * tile_x
213 # self.refpix_y -= dy + self.tile_size * tile_y
215 self.mapwidget._bg.vport[0] += dx
216 self.mapwidget._bg.vport[1] += dy
217 self.mapwidget.update_background()
220 self.mapwidget.reftile_y -= 1
221 self.mapwidget._bg.vport[1] += 256
222 self.mapwidget.update_background()
225 self.mapwidget.reftile_y += 1
226 self.mapwidget._bg.vport[1] -= 256
227 self.mapwidget.update_background()
230 self.mapwidget.reftile_x += 1
231 self.mapwidget._bg.vport[0] += 256
232 self.mapwidget.update_background()
235 self.mapwidget.reftile_x -= 1
236 self.mapwidget._bg.vport[0] -= 256
237 self.mapwidget.update_background()
240 class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
242 def __init__ ( self , config , map_size=(800,480) ) :
243 AbstractmapWidget.__init__( self , config , map_size )
245 gtk.Image.__init__(self)
247 self._bg = background_map( map_size , tile_loader( config ) )
249 self.update_background()
251 def update_background( self ) :
252 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x , self.win_y )
253 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 )
254 self.set_from_pixbuf(p)
256 def composeMap( self ) :
257 center_x , center_y = self.center()
259 # Ranges should be long enough as to fill the screen
260 # Maybe they should be decided based on self.win_x, self.win_y
261 for i in range(-3,4) :
262 for j in range(-3,4) :
263 file = self.tilename( i , j , self.conf.zoom )
265 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
266 pixbuf.fill( 0x00000000 )
269 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
270 except gobject.GError , ex :
271 print "Corrupted file %s" % ( file )
273 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
274 file = self.tilename( i , j , self.conf.zoom )
276 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
278 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
279 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
281 dest_x = self.tile_size * i + center_x
282 dest_y = self.tile_size * j + center_y
285 size_x = self.tile_size
288 size_x = self.tile_size + dest_x
290 if dest_x + self.tile_size > self.win_x :
291 size_x = self.win_x - dest_x
294 size_y = self.tile_size
297 size_y = self.tile_size + dest_y
299 if dest_y + self.tile_size > self.win_y :
300 size_y = self.win_y - dest_y
302 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
303 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
311 center_x , center_y = self.win_x / 2 , self.win_y / 2
313 # To get the central pixel in the window center, we must shift to the tile origin
314 center_x -= self.refpix_x
315 center_y -= self.refpix_y
317 return center_x , center_y
319 def plot( self , pixmap , coords , colorname , radius=3 ) :
321 center_x , center_y = self.center()
324 gc.foreground = pixmap.get_colormap().alloc_color( colorname )
326 dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
327 pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
329 def draw_paths( self ) :
331 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
333 filename = "data/wiscan_gui.info.old"
334 fd = open( filename )
335 for line in fd.readlines() :
336 values = line.split()
337 if values[1] == "FIX" :
338 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
341 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
343 def plot_APs( self ) :
345 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
347 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
349 # NOTE : Intervals for query are just educated guesses to fit in window
350 lat , lon = self.conf.lat , self.conf.lon
351 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 ) ) :
353 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
356 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
358 class mapWidget ( gtk.EventBox , interactiveMapWidget ) :
360 def __init__ ( self , config ) :
361 gtk.EventBox.__init__( self )
362 self.mapwidget = simpleMapWidget( config )
363 self.add( self.mapwidget )
365 self.click_x , self.click_y = None , None
366 self.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
367 self.connect_object('button_press_event', self.press_event, self.mapwidget)
368 self.connect_object('button_release_event', self.release_event, self)
370 def press_event ( self , widget , event ) :
371 self.click_x , self.click_y = event.x , event.y
373 def release_event ( self , widget, event ) :
374 delta_x = int( self.click_x - event.x )
375 delta_y = int( self.click_y - event.y )
376 widget.Shift(delta_x, delta_y)
377 self.click_x , self.click_y = None , None
379 if __name__ == "__main__" :
381 class StaticConfiguration :
383 def __init__ ( self , type=None ) :
386 self.homedir , self.dbname = None , None
387 self.mapsdir , self.mapclass = "/boot/home/localuser/Maps" , "OpenStreetMap I"
389 self.store_log , self.use_mapper , self.store_gps = None , None , None
391 self.lat , self.lon = 40.416 , -3.683
394 def on_key_press ( widget, event , map ) :
395 if event.keyval == gtk.keysyms.Up :
397 elif event.keyval == gtk.keysyms.Down :
399 elif event.keyval == gtk.keysyms.Right :
401 elif event.keyval == gtk.keysyms.Left :
404 print "UNKNOWN",event.keyval
406 config = StaticConfiguration()
407 map = mapWidget( config )
408 window = gtk.Window()
409 window.connect("destroy", gtk.main_quit )
410 window.connect("key-press-event", on_key_press, map )