diff --git a/python/bin/pmtiles-convert b/python/bin/pmtiles-convert index 72a81c5..0efaa64 100755 --- a/python/bin/pmtiles-convert +++ b/python/bin/pmtiles-convert @@ -4,16 +4,25 @@ #pmtiles to mbtiles #mbtiles to pmtiles +import os import argparse import sqlite3 +from pmtiles.reader import read from pmtiles.writer import write parser = argparse.ArgumentParser(description='Convert between PMTiles and other archive formats.') parser.add_argument('input',help='Input .mbtiles or .pmtiles') parser.add_argument('output',help='Output .mbtiles, .pmtiles, or directory') parser.add_argument('--maxzoom', help='the maximum zoom level to include in the output.') +parser.add_argument('--overwrite', help='Overwrite the existing output.',action='store_true') args = parser.parse_args() +if os.path.exists(args.output) and not args.overwrite: + print("Output exists, use --overwrite to overwrite the output.") + exit(1) +if os.path.exists(args.output) and args.overwrite: + os.remove(args.output) + if args.input.endswith('.mbtiles') and args.output.endswith('.pmtiles'): conn = sqlite3.connect(args.input) cursor = conn.cursor() @@ -34,7 +43,20 @@ if args.input.endswith('.mbtiles') and args.output.endswith('.pmtiles'): conn.close() elif args.input.endswith('.pmtiles') and args.output.endswith('.mbtiles'): - print("PMTiles to MBTiles not yet implemented") + conn = sqlite3.connect(args.output) + cursor = conn.cursor() + cursor.execute('CREATE TABLE metadata (name text, value text);') + cursor.execute('CREATE TABLE tiles (zoom_level integer, tile_column integer, tile_row integer, tile_data blob);') + + with read(args.input) as reader: + for k,v in reader.metadata.items(): + cursor.execute('INSERT INTO metadata VALUES(?,?)',(k,v)) + for tile, data in reader.tiles(): + cursor.execute('INSERT INTO tiles VALUES(?,?,?,?)',(tile[0],tile[1],tile[2],data)) + + cursor.execute('CREATE UNIQUE INDEX tile_index on tiles (zoom_level, tile_column, tile_row);') + conn.commit() + conn.close() elif args.input.endswith(".pmtiles"): print("PMTiles to directory not yet implemented") else: diff --git a/python/pmtiles/reader.py b/python/pmtiles/reader.py index 2752924..97040fb 100644 --- a/python/pmtiles/reader.py +++ b/python/pmtiles/reader.py @@ -67,3 +67,11 @@ class Reader: if val: return self.mmap[val[0]:val[0]+val[1]] + def tiles(self): + for k,v in self.root_dir.items(): + yield (k,self.mmap[v[0]:v[0]+v[1]]) + for val in self.leaves.values(): + leaf_dir, _ = self.load_directory(val[0],val[1]//17) + for k,v in leaf_dir.items(): + yield (k,self.mmap[v[0]:v[0]+v[1]]) + diff --git a/python/setup.py b/python/setup.py index 5b2be34..18cf3d6 100644 --- a/python/setup.py +++ b/python/setup.py @@ -19,6 +19,6 @@ setuptools.setup( "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", ], - scripts=['bin/mbtiles2pmtiles','bin/pmtiles-serve','bin/pmtiles-show'], + scripts=['bin/pmtiles-convert','bin/pmtiles-serve','bin/pmtiles-show'], requires_python='>=3.0' ) \ No newline at end of file