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 self.vport = bordersize * tileloader.tilesize , bordersize * tileloader.tilesize
27 gtk.gdk.Pixmap.__init__( self , None , pixsize[0] , pixsize[1] , 24 )
29 self.fill = map( lambda x : False , range( self.size[0] * self.size[1] ) )
33 def index ( self , x , y ) :
34 return x + y * self.size[0]
36 def loadtiles ( self ) :
38 for x in range(self.size[0]) :
39 for y in range(self.size[1]) :
41 pixbuf = self.tileloader.get_tile( (x-self.center[0],y-self.center[1]) )
43 self.fill[ self.index(x,y) ] = True
45 pixbuf = self.tileloader.emptytile()
47 dest_x = self.tileloader.tilesize * x
48 dest_y = self.tileloader.tilesize * y
49 self.draw_pixbuf( None , pixbuf , 0 , 0 , dest_x , dest_y )
54 def __init__ ( self , conf ) :
56 self.rootdir = "%s/%s/%s" % ( conf.mapsdir , conf.mapclass , conf.zoom )
57 self.reftile , self.refpix = self.get_reference( conf )
59 def get_reference ( self , conf ) :
60 tilex = self.lon2tilex( conf.lon , conf.zoom )
61 tiley = self.lat2tiley( conf.lat , conf.zoom )
62 tile = tilex[1] , tiley[1]
63 pix = tilex[0] , tiley[0]
64 return map( int , tile ) , map( lambda x : int( self.tilesize * x ) , pix )
66 def lon2tilex ( self , lon , zoom ) :
67 return math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
69 def lat2tiley ( self , lat , zoom ) :
70 lat = lat * math.pi / 180
71 return math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
73 def get_tile ( self , tile ) :
74 file = self.tilepath( self.reftile[0] + tile[0] , self.reftile[1] + tile[1] )
77 return gtk.gdk.pixbuf_new_from_file( file )
80 # useful members : response.code, response.headers
81 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
82 if response.geturl() != "http://tile.openstreetmap.org/11/0/0.png" :
83 fd = open( file , 'w' )
84 fd.write( response.read() )
86 # FIXME : can this actually produce a gobject.GError exception ?
87 return gtk.gdk.pixbuf_new_from_file( file )
93 def tilepath( self , tilex , tiley ) :
94 return "%s/%s/%s.png" % ( self.rootdir , tilex , tiley )
96 def emptytile( self ) :
97 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tilesize, self.tilesize )
98 pixbuf.fill( 0x00000000 )
102 class AbstractmapWidget :
104 def __init__ ( self , config , map_size ) :
108 # Maximum width should be 800, but actually gets reduced
109 self.win_x , self.win_y = map_size
112 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
113 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
115 def recenter ( self , latlon ) :
117 center = self.gps2pix( latlon , self.center() )
118 pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
120 distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
122 # FIXME : instead of hardcoded, should depend on the actual display size
124 self.conf.set_latlon( latlon )
126 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
127 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
131 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
134 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
136 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
139 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
140 return math.degrees( math.atan( math.sinh( tiley ) ) )
142 def SetZoom( self , zoom ) :
144 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
145 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
146 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
147 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
148 self.conf.set_zoom( zoom )
152 def lon2tilex ( self , lon , zoom ) :
153 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
154 return int( number[1] ) , int( self.tile_size * number[0] )
156 def lat2tiley ( self , lat , zoom ) :
157 lat = lat * math.pi / 180
158 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
159 return int( number[1] ) , int( self.tile_size * number[0] )
161 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
163 x_pos = self.lon2tilex( lon , self.conf.zoom )
164 y_pos = self.lat2tiley( lat , self.conf.zoom )
166 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
167 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
169 return dest_x , dest_y
171 def tilename ( self , x , y , zoom ) :
172 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
179 # useful members : response.code, response.headers
180 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
181 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
183 fd = open( file , 'w' )
184 fd.write( response.read() )
192 def tile2file( self , tilex , tiley , zoom ) :
193 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
194 if not os.path.isdir( rootdir ) :
196 rootsubdir = "%s/%s" % ( rootdir , tilex )
197 if not os.path.isdir( rootsubdir ) :
199 return "%s/%s.png" % ( rootsubdir , tiley )
201 class interactiveMapWidget :
203 def Shift( self , dx , dy ) :
206 tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
207 self.reftile_x += tile_x
208 self.reftile_y += tile_y
210 self.refpix_x -= dx + self.tile_size * tile_x
211 self.refpix_y -= dy + self.tile_size * tile_y
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 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, map_size[0] , map_size[1] )
250 p.get_from_drawable( self._bg , self._bg.get_colormap() , self._bg.vport[0], self._bg.vport[1] , 0, 0 , map_size[0] , map_size[1] )
251 self.set_from_pixbuf(p)
253 def composeMap( self ) :
254 center_x , center_y = self.center()
256 # Ranges should be long enough as to fill the screen
257 # Maybe they should be decided based on self.win_x, self.win_y
258 for i in range(-3,4) :
259 for j in range(-3,4) :
260 file = self.tilename( i , j , self.conf.zoom )
262 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
263 pixbuf.fill( 0x00000000 )
266 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
267 except gobject.GError , ex :
268 print "Corrupted file %s" % ( file )
270 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
271 file = self.tilename( i , j , self.conf.zoom )
273 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
275 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
276 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
278 dest_x = self.tile_size * i + center_x
279 dest_y = self.tile_size * j + center_y
282 size_x = self.tile_size
285 size_x = self.tile_size + dest_x
287 if dest_x + self.tile_size > self.win_x :
288 size_x = self.win_x - dest_x
291 size_y = self.tile_size
294 size_y = self.tile_size + dest_y
296 if dest_y + self.tile_size > self.win_y :
297 size_y = self.win_y - dest_y
299 if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
300 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
308 center_x , center_y = self.win_x / 2 , self.win_y / 2
310 # To get the central pixel in the window center, we must shift to the tile origin
311 center_x -= self.refpix_x
312 center_y -= self.refpix_y
314 return center_x , center_y
316 def plot( self , pixmap , coords , colorname , radius=3 ) :
318 center_x , center_y = self.center()
321 gc.foreground = pixmap.get_colormap().alloc_color( colorname )
323 dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
324 pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
326 def draw_paths( self ) :
328 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
330 filename = "data/wiscan_gui.info.old"
331 fd = open( filename )
332 for line in fd.readlines() :
333 values = line.split()
334 if values[1] == "FIX" :
335 self.plot( pixmap , ( float(values[5]) , float(values[6]) ) , "red" )
338 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
340 def plot_APs( self ) :
342 pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
344 db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
346 # NOTE : Intervals for query are just educated guesses to fit in window
347 lat , lon = self.conf.lat , self.conf.lon
348 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 ) ) :
350 self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
353 self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
355 class mapWidget ( simpleMapWidget , interactiveMapWidget ) :
359 if __name__ == "__main__" :
361 class StaticConfiguration :
363 def __init__ ( self , type=None ) :
366 self.homedir , self.dbname = None , None
367 self.mapsdir , self.mapclass = "/boot/home/localuser/Maps" , "OpenStreetMap I"
369 self.store_log , self.use_mapper , self.store_gps = None , None , None
371 self.lat , self.lon = 40.416 , -3.683
374 def on_key_press ( widget, event , map ) :
375 if event.keyval == gtk.keysyms.Up :
377 elif event.keyval == gtk.keysyms.Down :
379 elif event.keyval == gtk.keysyms.Right :
381 elif event.keyval == gtk.keysyms.Left :
384 print "UNKNOWN",event.keyval
386 config = StaticConfiguration()
387 map = mapWidget( config )
388 window = gtk.Window()
389 window.connect("destroy", gtk.main_quit )
390 #window.connect_object("destroy", gtk.Window.destroy , window )
391 window.connect("key-press-event", on_key_press, map )