forked from tarwirdur/mbtiles2osmand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbtiles2osmand.py
59 lines (44 loc) · 1.85 KB
/
mbtiles2osmand.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python3
import io
import sqlite3
from PIL import Image
import argparse
import os
parser = argparse.ArgumentParser(description='Converts mbtiles format to sqlitedb format suitable for OsmAnd')
parser.add_argument('input', help='input file')
parser.add_argument('output', help='output file')
parser.add_argument('-f', '-force', action='store_true', help='override output file if exists')
parser.add_argument('--jpg', dest='jpeg_quality', action='store', help='convert tiles to JPEG with specified quality')
args = parser.parse_args()
if os.path.isfile(args.output):
if args.f:
os.remove(args.output)
else:
print("Output file already exists. Add -f option for overwrite")
exit(1)
# See:
# * https://github.com/osmandapp/Osmand/blob/master/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java
def to_jpg(raw_bytes, quality):
im = Image.open(io.BytesIO(raw_bytes))
im = im.convert('RGB')
stream = io.BytesIO()
im.save(stream, format = "JPEG", subsampling=0, quality=quality)
return stream.getvalue()
source = sqlite3.connect(args.input)
dest = sqlite3.connect(args.output)
scur = source.cursor()
dcur = dest.cursor()
dcur.execute('''CREATE TABLE tiles (x int, y int, z int, s int, image blob, PRIMARY KEY (x,y,z,s));''')
dcur.execute('''CREATE TABLE info (maxzoom Int, minzoom Int);''')
for row in scur.execute("SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles"):
image = row[3]
if(args.jpeg_quality != None):
image = to_jpg(image, int(args.jpeg_quality))
z, x, y, s = int(row[0]), int(row[1]), int(row[2]), 0
y = 2 ** z - 1 - y
z = 17 - z
dcur.execute("INSERT INTO tiles (x, y, z, s, image) VALUES (?, ?, ?, ?, ?)", [x, y, z, s, sqlite3.Binary(image)])
dcur.execute("INSERT INTO info (maxzoom, minzoom) SELECT max(z),min(z) from tiles")
dest.commit()
source.close()
dest.close()