File: MBTiles.py

package info (click to toggle)
tilecache 2.11%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 532 kB
  • ctags: 423
  • sloc: python: 3,086; makefile: 90
file content (30 lines) | stat: -rw-r--r-- 1,005 bytes parent folder | download | duplicates (3)
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
# A minimal implementation of an MBTiles-formatted SQLite database 
# cache access mechanism. (No writing, only reading.)
# See:
#  http://mapbox.com/tools/mbtiles 
# for more information on the mbtiles format; it is essentially a single 
# table in a sqlite database with 4 columns:
#  * tile_column
#  * tile_row
#  * zoom_level
#  * tile_data

from TileCache.Cache import Cache
import os
import sqlite3

class MBTiles (Cache):
    def __init__ (self, base = None, ext = None, umask = '002', **kwargs):
        Cache.__init__(self, **kwargs)
        self.basedir = base
        self.ext = ext
        
    def get (self, tile):
        db = sqlite3.connect("%s.%s" % (os.path.join(self.basedir, tile.layer.name), self.ext))
        c = db.cursor()
        c.execute("select tile_data from tiles where tile_column=? and tile_row=? and zoom_level=?", (tile.x, tile.y, tile.z))
        res = c.fetchone()
        if res:
            tile.data = str(res[0])
            return tile.data
        return None