Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic copying functionality #712

Merged
merged 7 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/src/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ Retrieve raw metadata value by its name. The value is printed to stdout without
```shell
mbtiles meta-get <file.mbtiles> <key>
```

### copy
Copy existing `.mbtiles` file to a new, non-existent file.

```shell
mbtiles copy <src_file.mbtiles> <dst_file.mbtiles>
```
46 changes: 38 additions & 8 deletions martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};

use anyhow::Result;
use clap::{Parser, Subcommand};
use martin_mbtiles::Mbtiles;
use martin_mbtiles::{Mbtiles, TileCopier, TileCopierOptions};
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{Connection, SqliteConnection};

Expand All @@ -13,6 +13,9 @@ use sqlx::{Connection, SqliteConnection};
about = "A utility to work with .mbtiles file content"
)]
pub struct Args {
/// Display detailed information
#[arg(short, long, hide = true)]
verbose: bool,
#[command(subcommand)]
command: Commands,
}
Expand All @@ -39,13 +42,23 @@ enum Commands {
// /// MBTiles file to modify
// file: PathBuf,
// },
// /// Copy tiles from one mbtiles file to another.
// Copy {
// /// MBTiles file to read from
// src_file: PathBuf,
// /// MBTiles file to write to
// dst_file: PathBuf,
// },
/// Copy tiles from one mbtiles file to another.
#[command(name = "copy")]
Copy {
/// MBTiles file to read from
src_file: PathBuf,
/// MBTiles file to write to
dst_file: PathBuf,
/// Minimum zoom level to copy
#[arg(long)]
min_zoom: Option<u8>,
/// Maximum zoom level to copy
#[arg(long)]
max_zoom: Option<u8>,
/// List of zoom levels to copy; if provided, min-zoom and max-zoom will be ignored
#[arg(long, value_delimiter(','))]
zoom_levels: Vec<u8>,
},
}

#[tokio::main]
Expand All @@ -56,6 +69,23 @@ async fn main() -> Result<()> {
Commands::MetaGetValue { file, key } => {
meta_get_value(file.as_path(), &key).await?;
}
Commands::Copy {
src_file,
dst_file,
min_zoom,
max_zoom,
zoom_levels,
} => {
let copy_opts = TileCopierOptions::new()
.verbose(args.verbose)
.min_zoom(min_zoom)
.max_zoom(max_zoom)
.zooms(zoom_levels);

let tile_copier = TileCopier::new(src_file, dst_file, copy_opts)?;

tile_copier.run().await?;
}
}

Ok(())
Expand Down
10 changes: 8 additions & 2 deletions martin-mbtiles/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ pub enum MbtError {
#[error("Inconsistent tile formats detected: {0} vs {1}")]
InconsistentMetadata(TileInfo, TileInfo),

#[error("Invalid data storage format for MBTile file {0}")]
InvalidDataStorageFormat(String),
#[error("Invalid data format for MBTile file {0}")]
InvalidDataFormat(String),

#[error(r#"Filename "{0}" passed to SQLite must be valid UTF-8"#)]
InvalidFilenameType(PathBuf),

#[error("No tiles found")]
NoTilesFound,

#[error("The destination file {0} is non-empty")]
NonEmptyTargetFile(PathBuf),
}

pub type MbtResult<T> = Result<T, MbtError>;
2 changes: 2 additions & 0 deletions martin-mbtiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ mod errors;
mod mbtiles;
mod mbtiles_pool;
mod mbtiles_queries;
mod tile_copier;

pub use errors::MbtError;
pub use mbtiles::{Mbtiles, Metadata};
pub use mbtiles_pool::MbtilesPool;
pub use tile_copier::{TileCopier, TileCopierOptions};
16 changes: 8 additions & 8 deletions martin-mbtiles/src/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Metadata {
}

#[derive(Debug, PartialEq)]
pub enum Type {
pub enum MbtType {
TileTables,
DeDuplicated,
}
Expand Down Expand Up @@ -276,16 +276,16 @@ impl Mbtiles {
Ok(None)
}

pub async fn detect_type<T>(&self, conn: &mut T) -> MbtResult<Type>
pub async fn detect_type<T>(&self, conn: &mut T) -> MbtResult<MbtType>
where
for<'e> &'e mut T: SqliteExecutor<'e>,
{
if is_deduplicated_type(&mut *conn).await? {
Ok(Type::DeDuplicated)
Ok(MbtType::DeDuplicated)
} else if is_tile_tables_type(&mut *conn).await? {
Ok(Type::TileTables)
Ok(MbtType::TileTables)
} else {
Err(MbtError::InvalidDataStorageFormat(self.filepath.clone()))
Err(MbtError::InvalidDataFormat(self.filepath.clone()))
}
}
}
Expand Down Expand Up @@ -384,14 +384,14 @@ mod tests {
async fn detect_type() {
let (mut conn, mbt) = open("../tests/fixtures/files/world_cities.mbtiles").await;
let res = mbt.detect_type(&mut conn).await.unwrap();
assert_eq!(res, Type::TileTables);
assert_eq!(res, MbtType::TileTables);

let (mut conn, mbt) = open("../tests/fixtures/files/geography-class-jpg.mbtiles").await;
let res = mbt.detect_type(&mut conn).await.unwrap();
assert_eq!(res, Type::DeDuplicated);
assert_eq!(res, MbtType::DeDuplicated);

let (mut conn, mbt) = open(":memory:").await;
let res = mbt.detect_type(&mut conn).await;
assert!(matches!(res, Err(MbtError::InvalidDataStorageFormat(_))));
assert!(matches!(res, Err(MbtError::InvalidDataFormat(_))));
}
}
Loading