Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import sys
  5. import shutil
  6. import math
  7. import sqlite3
  8. import bottle
  9.  
  10. class EnableCors(object):
  11.     name = 'enable_cors'
  12.     api = 2
  13.     def apply(self, fn, context):
  14.         def _enable_cors(*args, **kwargs):
  15.             bottle.response.headers['Access-Control-Allow-Origin'] = '*'
  16.             bottle.response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS'
  17.             bottle.response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
  18.             if bottle.request.method != 'OPTIONS':
  19.                 return fn(*args, **kwargs)
  20.         return _enable_cors
  21.  
  22.  
  23. app = bottle.app()
  24. app.install(EnableCors())
  25. db = sqlite3.connect( sys.argv[1] )
  26.  
  27. def get_tile(z,x,y):
  28.     z = int(z)
  29.     x = int(x)
  30.     y = int(y)
  31.     print (z,x,y)
  32.  
  33.     q = "SELECT tile_data FROM tiles WHERE zoom_level=%d AND tile_column=%d AND tile_row=%d" % (z, x, y)
  34.     cur = db.cursor()
  35.     cur.execute( q )
  36.     data = cur.fetchone()
  37.     return bytes(data)
  38.  
  39. @app.get('/<z>/<x>/<y>', methods=['GET'])
  40. def tiles(z=0, x=0, y=0):
  41.     bottle.response.content_type = 'application/x-protobuf'
  42.     # bottle.response.content_encoding = 'gzip'
  43.     tile = get_tile(z, x, y)
  44.     return tile
  45.  
  46. # http://localhost:8080/12/2198/2752
  47. bottle.run( host='localhost', port=8080, debug=True, reloader=True )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement