11 class AbstractmapWidget :
13 def __init__ ( self , config ) :
17 # Maximum width should be 800, but actually gets reduced
18 self.win_x , self.win_y = 800 , 480
21 self.reftile_x , self.refpix_x = self.lon2tilex( self.conf.lon , self.conf.zoom )
22 self.reftile_y , self.refpix_y = self.lat2tiley( self.conf.lat , self.conf.zoom )
24 def lon2tilex ( self , lon , zoom ) :
25 number = math.modf( ( lon + 180 ) / 360 * 2 ** zoom )
26 return int( number[1] ) , int( self.tile_size * number[0] )
28 def lat2tiley ( self , lat , zoom ) :
29 lat = lat * math.pi / 180
30 number = math.modf( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
31 return int( number[1] ) , int( self.tile_size * number[0] )
33 def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
36 return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
38 def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
41 tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
42 return math.degrees( math.atan( math.sinh( tiley ) ) )
44 def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
46 x_pos = self.lon2tilex( lon , self.conf.zoom )
47 y_pos = self.lat2tiley( lat , self.conf.zoom )
49 dest_x = self.tile_size * ( x_pos[0] - self.reftile_x ) + center_x + x_pos[1]
50 dest_y = self.tile_size * ( y_pos[0] - self.reftile_y ) + center_y + y_pos[1]
52 return dest_x , dest_y
54 def tilename ( self , x , y , zoom ) :
55 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
62 # useful members : response.code, response.headers
63 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
64 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
66 fd = open( file , 'w' )
67 fd.write( response.read() )
75 def tile2file( self , tilex , tiley , zoom ) :
76 rootdir = "%s/%s/%s" % ( self.conf.mapsdir , self.conf.mapclass , zoom )
77 if not os.path.isdir( rootdir ) :
79 rootsubdir = "%s/%s" % ( rootdir , tilex )
80 if not os.path.isdir( rootsubdir ) :
82 return "%s/%s.png" % ( rootsubdir , tiley )
84 def Shift( self , dx , dy ) :
87 tile_x , tile_y = ( self.refpix_x - dx ) / self.tile_size , ( self.refpix_y - dy ) / self.tile_size
88 self.reftile_x += tile_x
89 self.reftile_y += tile_y
91 self.refpix_x -= dx + self.tile_size * tile_x
92 self.refpix_y -= dy + self.tile_size * tile_y
97 def ZoomChange ( self , selector ) :
98 model = selector.get_model(0)
99 active = selector.get_active(0)
100 value = model.get( model.get_iter(active) , 0 )
101 self.SetZoom( int(value[0]) )
103 def SetZoom( self , zoom ) :
105 lat = self.tiley2lat( ( self.reftile_y , self.refpix_y ) , self.conf.zoom )
106 lon = self.tilex2lon( ( self.reftile_x , self.refpix_x ) , self.conf.zoom )
107 self.reftile_x , self.refpix_x = self.lon2tilex( lon , zoom )
108 self.reftile_y , self.refpix_y = self.lat2tiley( lat , zoom )
109 self.conf.zoom = zoom