Skip to content

Diffing MBTiles

mbtiles diff

Copy command can also be used to compare two mbtiles files and generate a delta (diff) file. The diff file can be applied to the src_file.mbtiles elsewhere, to avoid copying/transmitting the entire modified dataset. The delta file will contain all tiles that are different between the two files (modifications, insertions, and deletions as NULL values), for both the tile and metadata tables.

mbtiles diff compares file1 and file2 into a diff holding changed, added and deleted tiles plus both aggregate hashes; apply-patch turns file1 and the diff into a copy of file2file1.mbtilesabcagg_tiles_hash = H1file2.mbtilesab′dagg_tiles_hash = H2mbtiles diffdiff.mbtilesb′changedcdeleted (NULL)dadded…before_apply = H1…after_apply = H2file1.mbtilesapply-patchrequires file1 = H1file2a.mbtilesab′dagg_tiles_hash = H2= file2

# This command will compare `file1.mbtiles` and `file2.mbtiles`,
# and generate a new diff file `diff.mbtiles`.
mbtiles diff file1.mbtiles file2.mbtiles diff.mbtiles

# If diff.mbtiles is applied to file1.mbtiles, it will produce file2.mbtiles
mbtiles apply-patch file1.mbtiles diff.mbtiles file2a.mbtiles

# file2.mbtiles and file2a.mbtiles should now be the same
# Validate both files and see that their hash values are identical
mbtiles validate file2.mbtiles
[INFO ] The agg_tiles_hashes=E95C1081447FB25674DCC1EB97F60C26 has been verified for file2.mbtiles

mbtiles validate file2a.mbtiles
[INFO ] The agg_tiles_hashes=E95C1081447FB25674DCC1EB97F60C26 has been verified for file2a.mbtiles

Delta file metadata

All metadata from file2.mbtiles will be copied to the diff file.

There are two exceptions to this. The first is that the agg_tiles_hash value will be renamed to agg_tiles_hash_after_apply. A new agg_tiles_hash will be generated for the diff file itself. This is done to avoid confusion when applying the diff file to the original file, as the agg_tiles_hash value will be different after the diff is applied. The apply-patch command will automatically rename the agg_tiles_hash_after_apply value back to agg_tiles_hash when applying the diff.

The second exception is that a new metadata value agg_tiles_hash_before_apply will be added to the diff file, which contains the agg_tiles_hash value from file1.mbtiles. This will be used to verify that the diff file is being applied to the correct source file.

If either input file has no agg_tiles_hash, it is computed on the fly (but not saved to the input file), so the diff file always gets both values. Diffing a file that is itself a diff is refused unless --force is used.

mbtiles apply-patch

Apply the diff file generated with the mbtiles diff command above to an MBTiles file. The diff file can be applied to the src_file.mbtiles that has been previously downloaded to avoid copying/transmitting the entire modified dataset again. The src_file.mbtiles will modified in-place. It is also possible to apply the diff file while copying the source file to a new destination file, by using the mbtiles copy --apply-patch command.

Both apply-patch and copy --apply-patch check the hashes:

  • before patching, the source file's agg_tiles_hash must equal the diff's agg_tiles_hash_before_apply. A missing agg_tiles_hash is computed. Use --validate to recompute it even when it is stored, in case the tiles were edited without updating the hash.
  • the diff file must have its own agg_tiles_hash, agg_tiles_hash_before_apply and agg_tiles_hash_after_apply.
  • after patching, the result's agg_tiles_hash is recomputed and must equal agg_tiles_hash_after_apply. apply-patch runs in a single transaction, so on a mismatch the source file is left untouched.

--force turns these errors into warnings.

mbtiles apply-patch src_file.mbtiles diff_file.mbtiles

Applying diff with SQLite

Another way to apply the diff is to use the sqlite3 command line tool directly. This SQL will delete all tiles from src_file.mbtiles that are set to NULL in diff_file.mbtiles, and then insert or update all new tiles from diff_file.mbtiles into src_file.mbtiles, where both files are of flat type. The name of the diff file is passed as a query parameter to the sqlite3 command line tool, and then used in the SQL statements. Note that this does not update the agg_tiles_hash metadata value, so it will be incorrect after the diff is applied.

sqlite3 src_file.mbtiles \
  -bail \
  -cmd ".parameter set @diffDbFilename diff_file.mbtiles "\
  "ATTACH DATABASE @diffDbFilename AS diffDb; "\
  "DELETE FROM tiles "\
  "  WHERE (zoom_level, tile_column, tile_row) IN ( "\
  "    SELECT zoom_level, tile_column, tile_row "\
  "    FROM diffDb.tiles "\
  "    WHERE tile_data ISNULL); "\
  "INSERT OR REPLACE INTO tiles (zoom_level, tile_column, tile_row, tile_data) "\
  "  SELECT * FROM diffDb.tiles WHERE tile_data NOTNULL;"

Binary Diff Support

The mbtiles diff command supports binary patching via the --patch-type flag, which can significantly reduce the size of difference files.

The flag combines two choices:

  • Input type - whether the tiles are raw or gzip-compressed
  • Output type - whether to store whole tiles (whole) or a delta against the old tile (bin-diff, zstd)
--patch-type Input Output
whole any Full tile stored as-is
bin-diff-raw uncompressed Brotli-compressed bsdiff patch
bin-diff-gz gzip Brotli-compressed bsdiff patch of decompressed data
zstd-raw uncompressed New tile compressed by zstd against the old tile
zstd-gz gzip Same as zstd-raw, on decompressed data

zstd-* patches are about as small as bin-diff-* patches and take a few hundred times less CPU to create.

Creating a diff

mbtiles diff original.mbtiles updated.mbtiles diff.mbtiles --patch-type zstd-raw

Applying a patch

The mbtiles CLI automatically detects and applies binary patches if a bsdiffraw, bsdiffrawgz, zstdpatchraw or zstdpatchgz table is present:

mbtiles copy diff.mbtiles original.mbtiles target.mbtiles --apply-patch

Note

bin-diff-gz and zstd-gz skip agg_tiles_hash_after_apply validation after patching, because re-compressing gzip tiles may produce different bytes. Use bin-diff-raw or zstd-raw if you need aggregate hash validation.

Note

mbtiles apply-patch does not currently support binary patching. Use mbtiles copy --apply-patch instead.