Skip to content

Instantly share code, notes, and snippets.

@creg-ny-baa
Created November 15, 2013 15:04
Show Gist options
  • Save creg-ny-baa/7485708 to your computer and use it in GitHub Desktop.
Save creg-ny-baa/7485708 to your computer and use it in GitHub Desktop.
Serve TMS tiles from an SQLite3, MBTiles spec database. Also provide a timestamp suitable for HTTP headers based on file timestamp.
import sqlite3
import os.path
from email import utils
class MBTilesTMS:
def __init__(self, filename):
if os.path.isfile(filename):
self.filename = filename
self.dbconn = sqlite3.connect(filename)
else:
raise ValueError("Value must be a valid file name")
def get_format(self):
#TODO: Tilemill is not using the current version of mbtiles spec
# which means the following does not work
c = self.dbconn.cursor()
c.execute('SELECT format FROM metadata')
row = c.fetchone()
return row[0]
def get_image_bytes(self, z, x, y):
c = self.dbconn.cursor()
c.execute('''SELECT tile_data FROM tiles
WHERE zoom_level = {}
AND tile_column = {}
AND tile_row = {}'''.format(z, x, y)
)
row = c.fetchone()
if not row:
return None
return bytes(row[0])
def get_date_modified(self):
filetimestamp = os.path.getmtime(self.filename)
return utils.formatdate(filetimestamp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment