11 class WifihoodConfig :
13 def __init__ ( self ) :
14 self.lat , self.lon = 40.40491 , -3.6774
16 self.mapsdir = "/home/user/MyDocs/.maps"
18 self.client = gconf.client_get_default()
20 def read ( self , parentdir="/apps/wifihood" ) :
21 lat = self.client.get_float( "%s/lattitude" % parentdir )
22 if lat : self.lat = lat
23 lon = self.client.get_float( "%s/longitude" % parentdir )
24 if lon : self.lon = lon
25 zoom = self.client.get_int( "%s/zoom" % parentdir )
26 if zoom : self.zoom = zoom
27 mapsdir = self.client.get_string( "%s/mapsdir" % parentdir )
28 if mapsdir : self.mapsdir = mapsdir
30 def save ( self , parentdir="/apps/wifihood" ) :
31 self.client.set_float( "%s/lattitude" % parentdir , self.lat )
32 self.client.set_float( "%s/longitude" % parentdir , self.lon )
33 self.client.set_int( "%s/zoom" % parentdir , self.zoom )
34 self.client.set_string( "%s/mapsdir" % parentdir , self.mapsdir )
36 class mapWidget ( gtk.Image , WifihoodConfig ) :
40 WifihoodConfig.__init__( self )
41 gtk.Image.__init__(self)
43 # Maximum width should be 800, but actually gets reduced
44 self.win_x , self.win_y = 800 , 480
46 p = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, self.win_x, self.win_y)
47 self.set_from_pixbuf(p)
50 self.reftile_x , self.reftile_y = self.lon2tilex( self.lon , self.zoom ) , self.lat2tiley( self.lat , self.zoom )
51 # Half the size of a tile
52 self.refpix_x , self.refpix_y = 128 , 128
56 def lon2tilex ( self , lon , zoom ) :
57 return int( ( lon + 180 ) / 360 * 2 ** zoom )
59 def lat2tiley ( self , lat , zoom ) :
60 lat = lat * math.pi / 180
61 return int( ( 1 - math.log( math.tan( lat ) + 1 / math.cos( lat ) ) / math.pi ) / 2 * 2 ** zoom )
63 def composeMap( self ) :
64 center_x , center_y = self.win_x / 2 , self.win_y / 2
66 # To get the central pixel in the window center, we must shift to the tile origin
67 center_x -= self.refpix_x
68 center_y -= self.refpix_y
70 # Ranges should be long enough as to fill the screen
71 # Maybe they should be decided based on self.win_x, self.win_y
72 for i in range(-3,4) :
73 for j in range(-3,4) :
74 file = self.tilename( i , j , self.zoom )
76 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 256, 256 )
77 pixbuf.fill( 0x00000000 )
80 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
81 except gobject.GError , ex :
82 print "Corrupted file %s" % ( file )
84 #file = self.tilename( self.reftile_x + i , self.reftile_y + j , self.zoom )
85 file = self.tilename( i , j , self.zoom )
87 pixbuf = gtk.gdk.pixbuf_new_from_file( file )
89 print "Total failure for tile for %s,%s" % ( self.reftile_x + i , self.reftile_y + j )
90 pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 256, 256 )
92 dest_x = 256 * i + center_x
93 dest_y = 256 * j + center_y
101 if dest_x + 256 > self.win_x :
102 size_x = self.win_x - dest_x
108 size_y = 256 + dest_y
110 if dest_y + 256 > self.win_y :
111 size_y = self.win_y - dest_y
113 if ( size_x > 0 and size_y > 0 ) and ( init_x < 256 and init_y < 256 ) :
114 pixbuf.copy_area( init_x, init_y, size_x, size_y, self.get_pixbuf(), dest_x , dest_y )
117 def tilename ( self , x , y , zoom ) :
118 file = self.tile2file( self.reftile_x + x , self.reftile_y + y , zoom )
125 # useful members : response.code, response.headers
126 response = urllib2.urlopen( "http://tile.openstreetmap.org/%s/%s/%s.png" % ( zoom , x , y ) )
127 if response.geturl() == "http://tile.openstreetmap.org/11/0/0.png" :
129 fd = open( file , 'w' )
130 fd.write( response.read() )
138 def tile2file( self , tilex , tiley , zoom ) :
139 rootdir = "%s/OpenStreetMap I/%s" % ( self.mapsdir , zoom )
140 if not os.path.isdir( rootdir ) :
142 rootsubdir = "%s/%s" % ( rootdir , tilex )
143 if not os.path.isdir( rootsubdir ) :
145 return "%s/%s.png" % ( rootsubdir , tiley )
147 def Shift( self , dx , dy ) :
150 tile_x , tile_y = ( self.refpix_x - dx ) / 256 , ( self.refpix_y - dy ) / 256
151 self.reftile_x += tile_x
152 self.reftile_y += tile_y
154 self.refpix_x -= dx + 256 * tile_x
155 self.refpix_y -= dy + 256 * tile_y
160 def SetZoom( self , zoom ) :
162 # FIXME : Use current lat,lon instead of hardcoded ones
163 lat , lon = 40.40491 , -3.6774
164 self.reftile_x , self.reftile_y = self.lon2tilex( lon , zoom ) , self.lat2tiley( lat , zoom )
166 self.refpix_x , self.refpix_y = 128 , 128
194 class ZoomDialog ( gtk.Dialog ) :
196 def __init__ ( self , widget ) :
197 gtk.Dialog.__init__( self , "Select zoom level",
200 ( gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
201 gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT
205 zooms = gtk.ListStore(int)
206 combo = gtk.ComboBox( zooms )
208 for zoom in range(8,19) :
209 iter = zooms.append()
210 zooms.set( iter , 0 , zoom )
211 if zoom == widget.zoom :
212 combo.set_active_iter( iter )
214 cell = gtk.CellRendererText()
215 combo.pack_start(cell, True)
216 combo.add_attribute(cell, 'text', 0)
218 self.vbox.pack_start(combo , True, True, 0)
220 self.connect_object( "response", self.response , combo , widget )
222 def response ( self , combo , response , widget ) :
223 if response == gtk.RESPONSE_ACCEPT :
224 item = combo.get_active_iter()
225 model = combo.get_model()
226 widget.SetZoom( model.get(item,0)[0] )
232 def delete_event(self, widget, event, data=None):
233 # If you return FALSE in the "delete_event" signal handler,
234 # GTK will emit the "destroy" signal. Returning TRUE means
235 # you don't want the window to be destroyed.
236 # This is useful for popping up 'are you sure you want to quit?'
238 print "delete event occurred"
240 # Change FALSE to TRUE and the main window will not be destroyed
241 # with a "delete_event".
244 def destroy(self, widget, data=None):
245 print "destroy signal occurred"
248 def press_event ( self, widget, event, *args ) :
249 # FIXME : Set only if far enough from borders
252 print "press ",event.get_coords(),event.get_root_coords()
253 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 ) :
254 self.click_x = event.x
255 self.click_y = event.y
257 def release_event ( self, widget, event, *args ) :
259 print "unpress",event.get_coords(),event.get_root_coords()
260 if self.click_x is not None and self.click_y is not None :
261 delta_x = int( event.x - self.click_x )
262 delta_y = int( event.y - self.click_y )
263 shift = math.sqrt( delta_x * delta_x + delta_y * delta_y )
264 if shift > min_shift :
265 self.map.Shift(delta_x, delta_y)
268 # elif delta_x < -100 :
270 # elif delta_y > 100 :
272 # elif delta_y < -100 :
274 self.click_x , self.click_y = None , None
276 def screen_event ( self, widget, event, *args ) :
282 def on_button_press ( self, widget, event, *args ) :
285 def on_key_press ( self, widget, event, *args ) :
286 if event.keyval == gtk.keysyms.Up :
288 elif event.keyval == gtk.keysyms.Down :
290 elif event.keyval == gtk.keysyms.Right :
292 elif event.keyval == gtk.keysyms.Left :
295 print "UNKNOWN",event.keyval
299 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
301 self.window.connect("delete_event", self.delete_event)
302 self.window.connect("destroy", self.destroy)
304 self.window.set_border_width(10)
306 self.window.connect("key-press-event", self.on_key_press)
308 vbox = gtk.VBox(False, 0)
309 self.window.add( vbox )
311 # To get explicit GDK_BUTTON_PRESS instead of paired GDK_LEAVE_NOTIFY & GDK_ENTER_NOTIFY
312 # self.window.add_events(gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK)
313 self.window.set_events( gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK )
315 # self.window.connect('motion_notify_event', self.screen_event)
316 self.window.connect('button_press_event', self.press_event)
317 self.window.connect('button_release_event', self.release_event)
319 self.map = mapWidget()
320 vbox.pack_end( self.map , True , True , 5)
322 self.create_menu( vbox )
325 self.window.show_all()
327 self.size_x , self.size_y = 800 , 480
328 self.click_x , self.click_y = None , None
330 def zoomdialog ( self , widget ) :
331 dialog = ZoomDialog( widget )
334 def create_menu ( self , vbox ) :
335 menubar = gtk.MenuBar()
337 zoomlevel = gtk.MenuItem( label="Zoom level" )
338 zoomlevel.connect_object( "activate", self.zoomdialog, self.map )
339 menubar.append( zoomlevel )
341 vbox.pack_start(menubar,True,True,5)
346 if __name__ == "__main__":