4a7e591fcf786248857a99db417a7882c926e851
[wifihood] / wifiscanner / wifimap / view.py
1
2 import gobject
3
4 import urllib2
5 import math
6
7 import os
8
9 import wifimap
10
11 class AbstractmapWidget :
12
13   def __init__ ( self , config ) :
14
15     self.conf = config
16
17     # Maximum width should be 800, but actually gets reduced
18     self.win_x , self.win_y = 800 , 480
19     self.tile_size = 256
20
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 )
23
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] )
27
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] )
32
33   def tilex2lon ( self , ( tilex , pixx ) , zoom ) :
34     tilex = float(tilex)
35     pixx = float(pixx)
36     return ( tilex + pixx/self.tile_size ) / 2.0 ** zoom * 360.0 - 180.0
37
38   def tiley2lat ( self , ( tiley , pixy ) , zoom ) :
39     tiley = float(tiley)
40     pixy = float(pixy)
41     tiley = math.pi * ( 1 - 2 * ( tiley + pixy/self.tile_size ) / 2.0 ** zoom )
42     return math.degrees( math.atan( math.sinh( tiley ) ) )
43
44   def gps2pix ( self , ( lat , lon ) , ( center_x , center_y ) ) :
45
46     x_pos = self.lon2tilex( lon , self.conf.zoom )
47     y_pos = self.lat2tiley( lat , self.conf.zoom )
48
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]
51
52     return dest_x , dest_y
53
54   def tilename ( self , x , y , zoom ) :
55     file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
56     try :
57       os.stat(file)
58     except :
59     #  if mapDownload :
60       if False :
61         try :
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" :
65               return None
66           fd = open( file , 'w' )
67           fd.write( response.read() )
68           fd.close()
69         except :
70           return None
71       else :
72         return None
73     return file
74
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 ) :
78       os.mkdir(rootdir)
79     rootsubdir = "%s/%s" % ( rootdir , tilex )
80     if not os.path.isdir( rootsubdir ) :
81       os.mkdir(rootsubdir)
82     return "%s/%s.png" % ( rootsubdir , tiley )
83
84   def Shift( self , dx , dy ) :
85     self.hide()
86
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
90
91     self.refpix_x -= dx + self.tile_size * tile_x
92     self.refpix_y -= dy + self.tile_size * tile_y
93
94     self.composeMap()
95     self.show()
96
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]) )
102
103   def SetZoom( self , zoom ) :
104     self.hide()
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
110     self.composeMap()
111     self.show()
112
113   def Up( self ) :
114     self.hide()
115     self.reftile_y -= 1
116     self.composeMap()
117     self.show()
118
119   def Down( self ) :
120     self.hide()
121     self.reftile_y += 1
122     self.composeMap()
123     self.show()
124
125   def Right( self ) :
126     self.hide()
127     self.reftile_x += 1
128     self.composeMap()
129     self.show()
130
131   def Left( self ) :
132     self.hide()
133     self.reftile_x -= 1
134     self.composeMap()
135     self.show()
136