forked from vvoovv/tile-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbtiles.py
38 lines (31 loc) · 1.12 KB
/
mbtiles.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
#!/usr/bin/env python
import os, sqlite3, math, io
from PIL import Image
from base import Base
class Mbtiles(Base):
maxTileNumber = 1e400
path = ""
cursor = None
def __init__(self, path):
self.path = path
def getMaxZoom(self, bbox):
self.createCursor()
self.cursor.execute("SELECT MAX(zoom_level) FROM tiles")
maxZoom = self.cursor.fetchone()[0]
self.closeCursor()
return maxZoom
def doStitching(self, left, bottom, right, top, zoom, resultImage, numTiles):
self.createCursor()
tilesetSize = long(math.pow(2, zoom))
for tile in self.cursor.execute("SELECT tile_column, tile_row, tile_data FROM tiles WHERE zoom_level=? AND tile_column>=? AND tile_column<=? AND tile_row>=? AND tile_row<=?", (zoom, left, right, tilesetSize-bottom-1, tilesetSize-top-1)):
image = Image.open(io.BytesIO(bytes(tile[2])))
x = tile[0]
y = tilesetSize - tile[1] - 1
resultImage.paste(image, ((x-left)*self.tileWidth, (y-top)*self.tileHeight))
self.closeCursor()
def createCursor(self):
if not self.cursor:
self.cursor = sqlite3.connect(self.path).cursor()
def closeCursor(self):
self.cursor.close()
self.cursor = None