2023-10-31 07:41:21 +03:00
|
|
|
# MBTiles Validation
|
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
The original [MBTiles specification](https://github.com/mapbox/mbtiles-spec#readme) does not provide any guarantees for
|
|
|
|
the content of the tile data in MBTiles. `mbtiles validate` assumes a few additional conventions and uses them to ensure
|
|
|
|
that the content of the tile data is valid performing several validation steps. If the file is not valid, the command
|
|
|
|
will print an error message and exit with a non-zero exit code.
|
2023-10-31 07:41:21 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
```bash
|
2023-10-31 07:41:21 +03:00
|
|
|
mbtiles validate src_file.mbtiles
|
|
|
|
```
|
|
|
|
|
|
|
|
## SQLite Integrity check
|
2023-12-05 01:11:03 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
The `validate` command will run `PRAGMA integrity_check` on the file, and will fail if the result is not `ok`.
|
|
|
|
The `--integrity-check` flag can be used to disable this check, or to make it more thorough with `full` value. Default
|
|
|
|
is `quick`.
|
2023-10-31 07:41:21 +03:00
|
|
|
|
|
|
|
## Schema check
|
2023-12-05 01:11:03 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
The `validate` command will verify that the `tiles` table/view exists, and that it has the expected columns and indexes.
|
|
|
|
It will also verify that the `metadata` table/view exists, and that it has the expected columns and indexes.
|
2023-10-31 07:41:21 +03:00
|
|
|
|
|
|
|
## Per-tile validation
|
2023-12-05 01:11:03 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
If the `.mbtiles` file uses [flat_with_hash](mbtiles-schema.md#flat-with-hash)
|
|
|
|
or [normalized](mbtiles-schema.md#normalized) schema, the `validate` command will verify that the MD5 hash of
|
|
|
|
the `tile_data` column matches the `tile_hash` or `tile_id` columns (depending on the schema).
|
2023-10-31 07:41:21 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
A typical Normalized schema generated by tools like [tilelive-copy](https://github.com/mapbox/TileLive#bintilelive-copy)
|
|
|
|
use MD5 hash in the `tile_id` column. The Martin's `mbtiles` tool can use this hash to verify the content of each tile.
|
|
|
|
We also define a new [flat-with-hash](mbtiles-schema.md#flat-with-hash) schema that stores the hash and tile data in the
|
|
|
|
same table, allowing per-tile validation without the multiple table layout.
|
2023-10-31 07:41:21 +03:00
|
|
|
|
|
|
|
Per-tile validation is not available for the `flat` schema, and will be skipped.
|
|
|
|
|
|
|
|
## Aggregate Content Validation
|
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
Per-tile validation will catch individual tile corruption, but it will not detect overall datastore corruption such as
|
|
|
|
missing tiles, tiles that should not exist, or tiles with incorrect z/x/y values. For that, the `mbtiles` tool defines a
|
|
|
|
new metadata value called `agg_tiles_hash`.
|
2023-10-31 07:41:21 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
The value is computed by hashing the combined value for all rows in the `tiles` table/view, ordered by z,x,y. The value
|
|
|
|
is computed using the following SQL expression, which uses a custom `md5_concat_hex` function
|
|
|
|
from [sqlite-hashes crate](https://crates.io/crates/sqlite-hashes):
|
2023-10-31 07:41:21 +03:00
|
|
|
|
|
|
|
```sql, ignore
|
|
|
|
md5_concat_hex(
|
|
|
|
CAST(zoom_level AS TEXT),
|
|
|
|
CAST(tile_column AS TEXT),
|
|
|
|
CAST(tile_row AS TEXT),
|
|
|
|
tile_data)
|
|
|
|
```
|
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
In case there are no rows or all are NULL, the hash value of an empty string is used. Note that SQLite allows any value
|
|
|
|
type to be stored as in any column, so if `tile_data` accidentally contains non-blob/text/null value, validation will
|
|
|
|
fail.
|
2023-10-31 07:41:21 +03:00
|
|
|
|
2024-05-26 14:06:28 +03:00
|
|
|
The `mbtiles` tool will compute `agg_tiles_hash` value when copying or validating mbtiles files. Use `--agg-hash update`
|
|
|
|
to force the value to be updated, even if it is incorrect or does not exist.
|