+import gtk
import gobject
import urllib2
class AbstractmapWidget :
- def __init__ ( self , config ) :
+ def __init__ ( self , config , map_size=(800,480) ) :
self.conf = config
# Maximum width should be 800, but actually gets reduced
- self.win_x , self.win_y = 800 , 480
+ self.win_x , self.win_y = map_size
self.tile_size = 256
self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
+ def recenter ( self , latlon ) :
+
+ center = self.gps2pix( latlon , self.center() )
+ pixel = self.gps2pix( (self.conf.lat,self.conf.lon) , self.center() )
+
+ distance = math.sqrt( (pixel[0]-center[0])**2 + (pixel[1]-center[1])**2 )
+
+ # FIXME : instead of hardcoded, should depend on the actual display size
+ if distance > 150 :
+ self.conf.set_latlon( latlon )
+
+ self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
+ self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
+
+ self.composeMap()
+
+ def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
+ tilex = float(tilex)
+ pixx = float(pixx)
+ return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
+
+ def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
+ tiley = float(tiley)
+ pixy = float(pixy)
+ tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
+ return math.degrees( math.atan( math.sinh( tiley ) ) )
+
+ def SetZoom( self , zoom ) :
+ self.hide()
+ lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
+ lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
+ self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
+ self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
+ self.conf.set_zoom( zoom )
+ self.composeMap()
+ self.show()
+
def lon2tilex ( self , lon , zoom ) :
number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
return int( number[1] ) , int( self.tile_size * number[0] )
number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
return int( number[1] ) , int( self.tile_size * number[0] )
- def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
- tilex = float(tilex)
- pixx = float(pixx)
- return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
-
- def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
- tiley = float(tiley)
- pixy = float(pixy)
- tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
- return math.degrees( math.atan( math.sinh( tiley ) ) )
-
def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
x_pos = self.lon2tilex( lon , self.conf.zoom )
os.mkdir(rootsubdir)
return "%s/%s.png" % ( rootsubdir , tiley )
+class interactiveMapWidget :
+
def Shift( self , dx , dy ) :
self.hide()
self.composeMap()
self.show()
- def ZoomChange ( self , selector ) :
- model = selector.get_model(0)
- active = selector.get_active(0)
- value = model.get( model.get_iter(active) , 0 )
- self.SetZoom( int(value[0]) )
-
- def SetZoom( self , zoom ) :
- self.hide()
- lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
- lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
- self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
- self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
- self.conf.zoom = zoom
- self.composeMap()
- self.show()
-
def Up( self ) :
self.hide()
self.reftile_y -= 1
self.composeMap()
self.show()
+class simpleMapWidget ( AbstractmapWidget , gtk.Image ) :
+
+ def __init__ ( self , config , map_size=(800,480) ) :
+ AbstractmapWidget.__init__( self , config , map_size )
+
+ gtk.Image.__init__(self)
+
+ p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
+ self.set_from_pixbuf(p)
+
+ self.composeMap()
+
+ def composeMap( self ) :
+ center_x , center_y = self.center()
+
+ # Ranges should be long enough as to fill the screen
+ # Maybe they should be decided based on self.win_x, self.win_y
+ for i in range(-3,4) :
+ for j in range(-3,4) :
+ file = self.tilename( i , j , self.conf.zoom )
+ if file is None :
+ pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
+ pixbuf.fill( 0x00000000 )
+ else :
+ try :
+ pixbuf = gtk.gdk.pixbuf_new_from_file( file )
+ except gobject.GError , ex :
+ print "Corrupted file %s" % ( file )
+ os.unlink( file )
+ #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.conf.zoom )
+ file = self.tilename( i , j , self.conf.zoom )
+ try :
+ pixbuf = gtk.gdk.pixbuf_new_from_file( file )
+ except :
+ print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
+ pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.tile_size, self.tile_size )
+
+ dest_x = self.tile_size * i + center_x
+ dest_y = self.tile_size * j + center_y
+
+ init_x = 0
+ size_x = self.tile_size
+ if dest_x < 0 :
+ init_x = abs(dest_x)
+ size_x = self.tile_size + dest_x
+ dest_x = 0
+ if dest_x + self.tile_size > self.win_x :
+ size_x = self.win_x - dest_x
+
+ init_y = 0
+ size_y = self.tile_size
+ if dest_y < 0 :
+ init_y = abs(dest_y)
+ size_y = self.tile_size + dest_y
+ dest_y = 0
+ if dest_y + self.tile_size > self.win_y :
+ size_y = self.win_y - dest_y
+
+ if ( size_x > 0 and size_y > 0 ) and ( init_x < self.tile_size and init_y < self.tile_size ) :
+ pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
+ del(pixbuf)
+
+ self.plot_APs()
+
+ def center( self ) :
+
+ center_x , center_y = self.win_x / 2 , self.win_y / 2
+
+ # To get the central pixel in the window center, we must shift to the tile origin
+ center_x -= self.refpix_x
+ center_y -= self.refpix_y
+
+ return center_x , center_y
+
+ def plot( self , pixmap , coords , colorname , radius=3 ) :
+
+ center_x , center_y = self.center()
+
+ gc = pixmap.new_gc()
+ gc.foreground = pixmap.get_colormap().alloc_color( colorname )
+
+ dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
+ pixmap.draw_rectangle(gc, True , dest_x , dest_y , radius , radius )
+
+ def line( self , pixmap , start , coords , colorname ) :
+
+ center_x , center_y = self.center()
+
+ gc = pixmap.new_gc()
+ gc.foreground = pixmap.get_colormap().alloc_color( colorname )
+
+ src_x , src_y = self.gps2pix( start , ( center_x , center_y ) )
+ dest_x , dest_y = self.gps2pix( coords , ( center_x , center_y ) )
+ dist = math.sqrt( (dest_x-src_x)**2 + (dest_y-src_y)**2 )
+ pixmap.draw_line(gc , src_x , src_y , dest_x , dest_y )
+
+ def plot_APs( self ) :
+
+ pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
+
+ db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
+ db.open()
+ # NOTE : Intervals for query are just educated guesses to fit in window
+ lat , lon = self.conf.lat , self.conf.lon
+ 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 ) ) :
+ if ap[3] > 1 :
+ self.plot( pixmap , ( ap[4]/ap[3] , ap[5]/ap[3] ) , "blue" )
+ db.close()
+
+ self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
+
+class mapWidget ( simpleMapWidget , interactiveMapWidget ) :
+
+ pass
+