From: javiplx Date: Wed, 13 Oct 2010 11:55:51 +0000 (+0000) Subject: Move UI independent parts of mapWidget into main module X-Git-Tag: cleaned~25 X-Git-Url: http://vcs.maemo.org/git/?p=wifihood;a=commitdiff_plain;h=4f855a260b1d265981c9963f17a6f83d086aff96 Move UI independent parts of mapWidget into main module git-svn-id: file:///svnroot/wifihood/trunk@59 c51dfc6a-5949-4919-9c8e-f207a149c383 --- diff --git a/wifiscanner/MANIFEST b/wifiscanner/MANIFEST index 21ebfb2..7375846 100644 --- a/wifiscanner/MANIFEST +++ b/wifiscanner/MANIFEST @@ -11,3 +11,4 @@ wifimap/wifiscan.py wifimap/osso_wrapper.py wifimap/db.py wifimap/config.py +wifimap/view.py diff --git a/wifiscanner/wifimap/__init__.py b/wifiscanner/wifimap/__init__.py index 3f6a473..a1c5f09 100644 --- a/wifiscanner/wifimap/__init__.py +++ b/wifiscanner/wifimap/__init__.py @@ -1,5 +1,5 @@ -__all__ = [ 'config' , 'db' , 'gps' , 'wifiscan' ] +__all__ = [ 'config' , 'db' , 'gps' , 'wifiscan' , 'view' ] from config import * @@ -9,3 +9,5 @@ from gps import * from wifiscan import * +from view import * + diff --git a/wifiscanner/wifimap/view.py b/wifiscanner/wifimap/view.py new file mode 100755 index 0000000..4a7e591 --- /dev/null +++ b/wifiscanner/wifimap/view.py @@ -0,0 +1,136 @@ + +import gobject + +import urllib2 +import math + +import os + +import wifimap + +class AbstractmapWidget : + + def __init__ ( self , config ) : + + self.conf = config + + # Maximum width should be 800, but actually gets reduced + self.win_x , self.win_y = 800 , 480 + 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 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 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() + diff --git a/wifiscanner/wifiview.py b/wifiscanner/wifiview.py index 83f148f..dabe111 100755 --- a/wifiscanner/wifiview.py +++ b/wifiscanner/wifiview.py @@ -11,57 +11,22 @@ import math import os -import wifimap.config , wifimap.db +import wifimap.config -class mapWidget ( gtk.Image ) : +import wifimap.view + +class mapWidget ( wifimap.view.AbstractmapWidget , gtk.Image ) : def __init__ ( self , config ) : + wifimap.view.AbstractmapWidget.__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 @@ -144,89 +109,6 @@ class mapWidget ( gtk.Image ) : 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 ) :