Skip to content

Instantly share code, notes, and snippets.

@dlau
Last active December 22, 2015 05:59
Show Gist options
  • Save dlau/6428298 to your computer and use it in GitHub Desktop.
Save dlau/6428298 to your computer and use it in GitHub Desktop.
crop mbtiles to another see desc
import sqlite3
import sys
import shutil
if len(sys.argv) != 4:
print 'usage: crop_mbtile_to_mbtile.py <input_mbtiles file> <reference_mbtiles file> <output_mbtiles file>'
sys.exit(0)
shutil.copyfile(sys.argv[2], sys.argv[3])
conn_input_mbtiles = sqlite3.connect(sys.argv[1])
conn_reference_mbtiles = sqlite3.connect(sys.argv[2])
conn_output_mbtiles = sqlite3.connect(sys.argv[3])
cursor_input = conn_input_mbtiles.cursor()
cursor_reference = conn_reference_mbtiles.cursor()
cursor_output = conn_output_mbtiles.cursor()
cursor_reference.execute('SELECT tile_row, tile_column, zoom_level, tile_id FROM map WHERE tile_id IS NOT NULL')
reference_row = cursor_reference.fetchone()
while reference_row != None:
tile_row = reference_row[0]
tile_column = reference_row[1]
zoom_level = reference_row[2]
tile_id = reference_row[3]
cursor_input.execute(
'''SELECT tile_row, tile_column, zoom_level, tile_id FROM map WHERE
tile_row = ? AND tile_column = ? AND zoom_level = ?''',
(tile_row,tile_column,zoom_level)
)
input_row = cursor_input.fetchone()
if input_row == None:
#note, this may create a dangling pointer ?
cursor_output.execute(
'''DELETE FROM map WHERE tile_row = ? AND tile_column = ? AND zoom_level = ?''',
(tile_row,tile_column,zoom_level)
)
else:
input_tile_id = input_row[3]
input_image_data = conn_input_mbtiles.cursor().execute(
'SELECT tile_data FROM images WHERE tile_id=?',(input_tile_id,)
).fetchone()[0]
cursor_output.execute('UPDATE images SET tile_data = ? WHERE tile_id = ?',
(input_image_data,tile_id)
)
#HACK, incomplete implementation, leaves some grids behind
cursor_output.execute(
'UPDATE map SET grid_id = NULL WHERE tile_row = ? AND tile_column = ? AND zoom_level = ?',
(tile_row, tile_column, zoom_level)
)
reference_row = cursor_reference.fetchone()
#HACK, incomplete implementation, should transfer utfgrids
cursor_output.execute('UPDATE map SET grid_id = NULL WHERE 1=1')
cursor_output.execute('DELETE FROM grid_key WHERE 1=1')
cursor_output.execute('DELETE FROM keymap WHERE 1=1')
cursor_output.execute('DELETE FROM grid_utfgrid WHERE 1=1')
#should be immediate
conn_output_mbtiles.commit()
cursor_output.execute('VACUUM')
conn_input_mbtiles.close()
conn_reference_mbtiles.close()
conn_output_mbtiles.close()
crop mbtile to another
I needed to generate a large amount of terrain tiles which covered a very detailed zoom level, this creates a gigantic mbtiles file, most of which is for areas that I don't care about.
This script takes an input, in this case the terrain tiles, and a reference set of tiles and limits the input to a subset of the reference.
Should probably build in some tolerances, especially because this is used for background imagery
Also utfgrids get nuked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment