MERGE : incorporate changes on cleaning branch to completelly decouple wifiview and...
[wifihood] / wifiscanner / wifimap / view.py
index f98f582..bcb4c51 100755 (executable)
@@ -1,4 +1,5 @@
 
+import gtk
 import gobject
 
 import urllib2
@@ -30,17 +31,6 @@ class AbstractmapWidget :
     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 )
@@ -81,6 +71,19 @@ class AbstractmapWidget :
       os.mkdir(rootsubdir)
     return "%s/%s.png" % ( rootsubdir , tiley )
 
+class interactiveMapWidget :
+
+  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 Shift( self , dx , dy ) :
     self.hide()
 
@@ -134,3 +137,127 @@ class AbstractmapWidget :
     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.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
+
+        # 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.draw_paths()
+        self.plot_APs()
+
+    def draw_paths( 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
+
+        pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
+        red = pixmap.new_gc()
+        red.foreground = pixmap.get_colormap().alloc_color("red")
+        green = pixmap.new_gc()
+        green.foreground = pixmap.get_colormap().alloc_color("green")
+        blue = pixmap.new_gc()
+        blue.foreground = pixmap.get_colormap().alloc_color("blue")
+
+        filename = "data/wiscan_gui.info.old"
+        fd = open( filename )
+        for line in fd.readlines() :
+            values = line.split()
+            if values[1] == "FIX" :
+                dest_x , dest_y = self.gps2pix( ( float(values[5]) , float(values[6]) ) , ( center_x , center_y ) )
+                pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
+        fd.close()
+
+        self.get_pixbuf().get_from_drawable( pixmap , pixmap.get_colormap() , 0, 0 , 0 , 0 , self.win_x, self.win_y )
+
+    def plot_APs( 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
+
+        pixmap,mask = self.get_pixbuf().render_pixmap_and_mask()
+        blue = pixmap.new_gc()
+        blue.foreground = pixmap.get_colormap().alloc_color("blue")
+
+        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 :
+                dest_x , dest_y = self.gps2pix( ( ap[4]/ap[3] , ap[5]/ap[3] ) , ( center_x , center_y ) )
+                pixmap.draw_rectangle(blue, True , dest_x , dest_y , 3 , 3 )
+        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
+