Package viewer :: Package lib :: Module tileloader
[hide private]
[frames] | no frames]

Source Code for Module viewer.lib.tileloader

 1  """Loads OSM tiles from the Web""" 
 2   
 3  import os 
 4  import asyncore 
 5  import threading 
 6   
 7  import asynchttp 
 8   
 9  import pyglet.image as pygletimage 
10   
11  __author__ = "P. Tute" 
12  __maintainer__ = "B. Henne" 
13  __contact__ = "henne@dcsec.uni-hannover.de" 
14  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
15  __license__ = "GPLv3" 
16   
17   
18  PORT = 80 
19  METHOD = 'GET' 
20   
21  LAYERS = {"tah": ["cassini.toolserver.org:8080", "/http://a.tile.openstreetmap.org/+http://toolserver.org/~cmarqu/hill/"], 
22            "oam": ["oam1.hypercube.telascience.org", "/tiles/1.0.0/openaerialmap-900913/"], 
23            "mapnik": ["a.tile.openstreetmap.org", "/"] 
24  } 
25   
26 -class TileLoader(asynchttp.AsyncHTTPConnection):
27 """This class uses asynchttp to download OSM-tiles in an asynchronous manner. 28 @author: P. Tute"""
29 - def __init__(self, player, x, y, z, layer='mapnik'):
30 """player is an instance of the player that uses the tiles. 31 layer is one of 'tah', 'oam', and 'mapnik' (default).""" 32 host = LAYERS[layer][0] 33 self.layer = layer 34 asynchttp.AsyncHTTPConnection.__init__(self, host, PORT) 35 36 self.base_url_extension = LAYERS[layer][1] 37 #self.file_extension = '.' + self.tileLayerExt(layer) 38 self.file_extension = '.jpg' if layer == 'oam' else '.png' 39 40 self.player = player 41 self.x, self.y, self.z = x, y, z 42 self.loading_image = pygletimage.load(os.path.join(self.player.data_dir, 'loading.png')) 43 self.player.tiles[(self.x, self.y, self.z)] = self.loading_image 44 45 self.connect()
46
47 - def get_tile(self):
48 """Builds a response and tries to download the right tile from x, y, andd zoom-values.""" 49 url = self.base_url_extension + str(self.z) + '/' + str(self.x) + '/' + str(self.y) + self.file_extension 50 self.putrequest('GET', url) 51 self.endheaders() 52 self.getresponse()
53
54 - def handle_connect(self):
57
58 - def handle_response(self):
59 if '<html>' in self.response.body: 60 # something went wrong...mostlikely a 404 error 61 return 62 elements = [self.player.cache_dir, self.layer, str(self.z), str(self.x)] 63 path = '' 64 for element in elements: 65 path = os.path.join(path, element) 66 try: 67 os.mkdir(path) 68 except OSError: 69 #folder exists, no need to create 70 pass 71 path = os.path.join(path, str(self.y) + self.file_extension) 72 image_file = open(path, 'wb') 73 image_file.write(self.response.body) 74 image_file.flush() 75 image_file.close() 76 image = pygletimage.load(path) 77 image.anchor_x = image.width / 2 78 image.anchor_y = image.height / 2 79 self.player.tiles[(self.x, self.y, self.z)] = image
80 81 82 if __name__ == '__main__': 83 player = Player(52.382463, 9.717836, width=800, height=600, resizable=True) 84 tl = TileLoader(player, 34, 43, 16) 85 tl.start() 86