+
+import gtk
+import gobject
+try :
+ import hildon
+except :
+ hildon = False
+
+import urllib2
+import math
+
+import os
+
+import wifimap.config , wifimap.db
+
+class mapWidget ( gtk.Image ) :
+
+ def __init__ ( self , config ) :
+
+ self.conf = config
+ gtk.Image.__init__(self)
+
+ # Maximum width should be 800, but actually gets reduced
+ self.win_x , self.win_y = 800 , 480
+ self.tile_size = 256
+
+ p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
+ self.set_from_pixbuf(p)
+
+ 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 lon2tilex ( self , lon , zoom ) :
+ number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
+ return int( number[1] ) , int( self.tile_size * number[0] )
+
+ def lat2tiley ( self , lat , zoom ) :
+ lat = lat * math.pi / 180
+ 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 )
+ y_pos = self.lat2tiley( lat , self.conf.zoom )
+
+ dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
+ dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
+
+ return dest_x , dest_y
+
+ 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)
+
+ 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()
+
+ db = wifimap.db.database( os.path.join( self.conf.homedir , self.conf.dbname ) )
+ db.open()
+ for ap in db.db.execute( "SELECT * FROM ap" ) :
+ 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(red, 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 )
+
+
+ def tilename ( self , x , y , zoom ) :
+ file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
+ try :
+ os.stat(file)
+ except :
+ # if mapDownload :
+ if False :
+ try :
+ # useful members : response.code, response.headers
+ response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
+ if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
+ return None
+ fd = open( file , 'w' )
+ fd.write( response.read() )
+ fd.close()
+ except :
+ return None
+ else :
+ return None
+ return file
+
+ def tile2file( self , tilex , tiley , zoom ) :
+ rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
+ if not os.path.isdir( rootdir ) :
+ os.mkdir(rootdir)
+ rootsubdir = "%s/%s" % ( rootdir , tilex )
+ if not os.path.isdir( rootsubdir ) :
+ os.mkdir(rootsubdir)
+ return "%s/%s.png" % ( rootsubdir , tiley )
+
+ def Shift( self , dx , dy ) :
+ self.hide()
+
+ tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
+ self.reftile_x += tile_x
+ self.reftile_y += tile_y
+
+ self.refpix_x -= dx + self.tile_size * tile_x
+ self.refpix_y -= dy + self.tile_size * tile_y
+
+ 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()
+
+ def Down( self ) :
+ self.hide()
+ self.reftile_y += 1
+ self.composeMap()
+ self.show()
+
+ def Right( self ) :
+ self.hide()
+ self.reftile_x += 1
+ self.composeMap()
+ self.show()
+
+ def Left( self ) :
+ self.hide()
+ self.reftile_x -= 1
+ self.composeMap()
+ self.show()
+
+if hildon :
+
+ class ZoomDialog ( hildon.TouchSelector ) :
+
+ def __init__ ( self , widget ) :
+ hildon.TouchSelector.__init__( self )
+
+ zooms = gtk.ListStore(str)
+
+ active = index = 0
+ for zoom in range(8,19) :
+ iter = zooms.append()
+ zooms.set( iter , 0 , "%2d" % zoom )
+ if zoom == widget.conf.zoom :
+ active = index
+ index += 1
+
+ column = self.append_text_column( zooms , True )
+ #renderer = gtk.CellRendererText()
+ #column = self.append_column( zooms , renderer )
+ #column.set_property('text-column', 0)
+
+ # NOTE : with text=True, we must use 1 instead of 0
+ self.set_active( 0 , active )
+
+else :
+
+ class ZoomDialog ( gtk.Dialog ) :
+
+ def __init__ ( self , widget ) :
+ gtk.Dialog.__init__( self , "Select zoom level",
+ None,
+ gtk.DIALOG_MODAL,
+ ( gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
+ gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT
+ )
+ )
+
+ zooms = gtk.ListStore(int)
+ combo = gtk.ComboBox( zooms )
+
+ for zoom in range(8,19) :
+ iter = zooms.append()
+ zooms.set( iter , 0 , zoom )
+ if zoom == widget.conf.zoom :
+ combo.set_active_iter( iter )
+
+ cell = gtk.CellRendererText()
+ combo.pack_start(cell, True)
+ combo.add_attribute(cell, 'text', 0)
+
+ self.vbox.pack_start(combo , True, True, 0)
+
+ self.connect_object( "response", self.response , combo , widget )
+
+ def response ( self , combo , response , widget ) :
+ if response == gtk.RESPONSE_ACCEPT :
+ item = combo.get_active_iter()
+ model = combo.get_model()
+ widget.SetZoom( model.get(item,0)[0] )
+ self.destroy()
+
+
+class AbstractMapWindow:
+
+ def destroy(self, widget, data=None):
+ gtk.main_quit()
+
+ def press_event ( self, widget, event, *args ) :
+ # FIXME : Set only if far enough from borders
+ border_x = 40
+ border_y = 30
+ print "press ",event.get_coords(),event.get_root_coords()
+ if event.x > border_x and event.y > border_y and event.x < ( self.size_x - border_x ) and event.y < ( self.size_y - border_y ) :
+ self.click_x = event.x
+ self.click_y = event.y
+
+ def release_event ( self, widget, event, *args ) :
+ min_shift = 50
+ print "unpress",event.get_coords(),event.get_root_coords()
+ if self.click_x is not None and self.click_y is not None :
+ delta_x = int( event.x - self.click_x )
+ delta_y = int( event.y - self.click_y )
+ shift = math.sqrt( delta_x * delta_x + delta_y * delta_y )
+ if shift > min_shift :
+ self.map.Shift(delta_x, delta_y)
+ # if delta_x > 100 :
+ # self.map.Left()
+ # elif delta_x < -100 :
+ # self.map.Right()
+ # elif delta_y > 100 :
+ # self.map.Up()
+ # elif delta_y < -100 :
+ # self.map.Down()
+ self.click_x , self.click_y = None , None
+
+ def screen_event ( self, widget, event, *args ) :
+ print "REDIOS",event
+ print " ",widget
+ print " ",args
+
+
+ def on_button_press ( self, widget, event, *args ) :
+ print "HOLA",event
+
+ def on_key_press ( self, widget, event, *args ) :
+ if event.keyval == gtk.keysyms.Up :
+ self.map.Up()
+ elif event.keyval == gtk.keysyms.Down :
+ self.map.Down()
+ elif event.keyval == gtk.keysyms.Right :
+ self.map.Right()
+ elif event.keyval == gtk.keysyms.Left :
+ self.map.Left()
+ else :
+ print "UNKNOWN",event.keyval
+
+ def __init__(self):
+
+ self.connect("destroy", self.destroy)
+
+ self.set_border_width(10)
+
+ self.connect("key-press-event", self.on_key_press)
+
+ vbox = gtk.VBox(False, 0)
+ self.add( vbox )
+
+ # To get explicit GDK_BUTTON_PRESS instead of paired GDK_LEAVE_NOTIFY & GDK_ENTER_NOTIFY
+# self.add_events(gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK)
+ self.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
+ #
+# self.connect('motion_notify_event', self.screen_event)
+ self.connect('button_press_event', self.press_event)
+ self.connect('button_release_event', self.release_event)
+ #
+ self.config = wifimap.config.Configuration()
+ self.map = mapWidget( self.config )
+ vbox.pack_end( self.map , True , True , 5)
+
+ self.create_menu( vbox )
+
+ # and the window
+ self.show_all()
+
+ self.size_x , self.size_y = 800 , 480
+ self.click_x , self.click_y = None , None
+
+ def zoomdialog ( self , widget ) :
+ dialog = ZoomDialog( widget )
+ dialog.show_all()
+
+ def main(self):
+ gtk.main()
+
+if hildon :
+
+ class MapWindow ( AbstractMapWindow , hildon.Window ) :
+
+ def __init__(self):
+ hildon.Window.__init__( self )
+ AbstractMapWindow.__init__(self)
+
+ def create_menu ( self , vbox ) :
+
+ self.menubar = menubar = hildon.AppMenu()
+
+ #zoomlevel = hildon.Button(gtk.HILDON_SIZE_AUTO,
+ # hildon.BUTTON_ARRANGEMENT_VERTICAL,
+ # "Zoom level", None)
+ #zoomlevel.connect_object( "clicked", self.zoomstack, self.map )
+ selector = ZoomDialog( self.map )
+ zoomlevel = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,
+ hildon.BUTTON_ARRANGEMENT_VERTICAL)
+ zoomlevel.set_title( "Zoom" )
+ zoomlevel.set_selector( selector )
+ zoomlevel.connect_object( "value-changed", self.map.ZoomChange , selector )
+ menubar.append( zoomlevel )
+
+ menubar.show_all()
+ self.set_app_menu( menubar )
+
+else :
+
+ class MapWindow ( AbstractMapWindow , gtk.Window ) :
+
+ def __init__(self):
+ gtk.Window.__init__( self , gtk.WINDOW_TOPLEVEL )
+ AbstractMapWindow.__init__(self)
+ self.resize( self.size_x , self.size_y)
+
+ def create_menu ( self , vbox ) :
+
+ menubar = gtk.MenuBar()
+
+ zoomlevel = gtk.MenuItem( label="Zoom level" )
+ zoomlevel.connect_object( "activate", self.zoomdialog, self.map )
+ menubar.append( zoomlevel )
+
+ vbox.pack_start(menubar,True,True,5)
+