-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathATLASTileHandler.py
43 lines (36 loc) · 1.23 KB
/
ATLASTileHandler.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
from mbtiles import MbtileSet
import tornado.ioloop
import tornado.web
import threading
import os
import time
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('''
<h1> Server is running... </h1>
''')
class MbtilesHandler(tornado.web.RequestHandler):
def initialize(self, ext, mbtiles):
self.ext = ext
self.mbtiles = mbtiles
self.tileset = MbtileSet(mbtiles=mbtiles)
def get(self, z, x, y):
origin = self.get_arguments('origin')
# invert y axis to top origin
ymax = 1 << int(z);
y = ymax - int(y) - 1;
tile = self.tileset.get_tile(z, x, y)
if self.ext == 'png':
self.set_header('Content-Type', 'image/png')
self.write(tile.get_png())
elif self.ext == 'json':
callback = self.get_arguments('callback')
try:
callback = callback[0]
except IndexError:
callback = None
self.set_header('Content-Type', 'application/json')
if callback:
self.write("%s(%s)" % (callback, tile.get_json()))
else:
self.write(tile.get_json())