-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.py
91 lines (73 loc) · 2.2 KB
/
tiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import urllib
import random
from load_balance import update_server_list
from hashlib import md5
from patterns import Singleton
from logger import Logger
from constants.http.status import OK, NOT_FOUND
from constants.error import REQUEST_FAILED
class TilesManager(Singleton):
def configure(self, cfg):
Logger().info("Configuring Tiles Manager")
self.filters = []
self.tilesPath = cfg.tilesPath
self.wms = cfg.wms
Logger().info("Parsing filters")
for filter in cfg.filters:
self.filters.insert(filter.order - 1, filter)
Logger().info("Filters parsed")
Logger().info("Tiles Manager configured")
def getTile(self, params):
"""
Returns the tile from the cache if it exists, if not
"""
result = ""
tile_dir = self.tileDir(params)
tile_path = os.path.join(tile_dir, params.hash())
response_code = NOT_FOUND
try:
tile_file = open(tile_path, "r")
result = tile_file.read()
response_code = OK
except:
try:
# Redirect request to WMS
wms_request = random.sample(update_server_list(self.wms.split(",")),1)[0] + str(params)
opener = urllib.FancyURLopener()
response = opener.open(wms_request)
response_code = OK
result = response.fp.read();
response_header = result[2:5];
if(response_header != "xml"):
# Create the path
dir = os.path.dirname(tile_path)
if not os.path.exists(dir):
os.makedirs(dir)
# Store tile in cache
tile_file = open(tile_path, "w")
tile_file.write(result)
tile_file.close()
else:
Logger().error(REQUEST_FAILED + wms_request + result)
response_code = NOT_FOUND
result = ""
except:
response_code = NOT_FOUND
Logger().warning(REQUEST_FAILED + wms_request)
finally:
return result, response_code
def tileDir(self, params):
"""
Use the current configuration to convert the given parameters into a valid path
where the tile that should be stored
"""
path = self.tilesPath
for filter in self.filters:
dir = ""
if params.has_key(filter.name):
dir = filter.name + "=" + params[filter.name]
if filter.hash and dir != "":
dir = md5(dir).hexdigest()
path = os.path.join(path, dir)
return path