Skip to content

Commit

Permalink
Add force-simple parameter when copying (#723)
Browse files Browse the repository at this point in the history
Add `--force-simple` flag to `mbtiles copy` tool.
If supplied, ensures the destination file has a `tiles` _table_ (as
opposed to a `tiles` _view_ made up of `images` and `map` tables).
  • Loading branch information
upsicleclown authored Jun 23, 2023
1 parent 8b3a589 commit 177b72e
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 124 deletions.
181 changes: 147 additions & 34 deletions martin-mbtiles/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::path::{Path, PathBuf};

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

#[derive(Parser, Debug)]
#[derive(Parser, PartialEq, Eq, Debug)]
#[command(
version,
name = "mbtiles",
Expand All @@ -20,7 +20,7 @@ pub struct Args {
command: Commands,
}

#[derive(Subcommand, Debug)]
#[derive(Subcommand, PartialEq, Eq, Debug)]
enum Commands {
// /// Prints all values in the metadata table.
// #[command(name = "meta-all")]
Expand All @@ -44,21 +44,7 @@ enum Commands {
// },
/// 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>,
},
Copy(TileCopierOptions),
}

#[tokio::main]
Expand All @@ -69,22 +55,8 @@ 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?;
Commands::Copy(opts) => {
copy_mbtiles_file(opts).await?;
}
}

Expand All @@ -100,3 +72,144 @@ async fn meta_get_value(file: &Path, key: &str) -> Result<()> {
}
Ok(())
}

#[cfg(test)]
mod tests {
use crate::Args;
use crate::Commands::{Copy, MetaGetValue};
use clap::error::ErrorKind;
use clap::Parser;
use martin_mbtiles::TileCopierOptions;
use std::path::PathBuf;

#[test]
fn test_copy_no_arguments() {
assert_eq!(
Args::try_parse_from(["mbtiles", "copy"])
.unwrap_err()
.kind(),
ErrorKind::MissingRequiredArgument
);
}

#[test]
fn test_copy_minimal_arguments() {
assert_eq!(
Args::parse_from(["mbtiles", "copy", "src_file", "dst_file"]),
Args {
verbose: false,
command: Copy(TileCopierOptions::new(
PathBuf::from("src_file"),
PathBuf::from("dst_file")
))
}
);
}

#[test]
fn test_copy_min_max_zoom_arguments() {
assert_eq!(
Args::parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--max-zoom",
"100",
"--min-zoom",
"1"
]),
Args {
verbose: false,
command: Copy(
TileCopierOptions::new(PathBuf::from("src_file"), PathBuf::from("dst_file"))
.min_zoom(Some(1))
.max_zoom(Some(100))
)
}
);
}

#[test]
fn test_copy_min_max_zoom_no_arguments() {
assert_eq!(
Args::try_parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--max-zoom",
"--min-zoom",
])
.unwrap_err()
.kind(),
ErrorKind::InvalidValue
);
}

#[test]
fn test_copy_min_max_zoom_with_zoom_levels_arguments() {
assert_eq!(
Args::try_parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--max-zoom",
"100",
"--min-zoom",
"1",
"--zoom-levels",
"3,7,1"
])
.unwrap_err()
.kind(),
ErrorKind::ArgumentConflict
);
}

#[test]
fn test_copy_zoom_levels_arguments() {
assert_eq!(
Args::parse_from([
"mbtiles",
"copy",
"src_file",
"dst_file",
"--zoom-levels",
"3,7,1"
]),
Args {
verbose: false,
command: Copy(
TileCopierOptions::new(PathBuf::from("src_file"), PathBuf::from("dst_file"))
.zoom_levels(vec![1, 3, 7])
)
}
);
}

#[test]
fn test_meta_get_no_arguments() {
assert_eq!(
Args::try_parse_from(["mbtiles", "meta-get"])
.unwrap_err()
.kind(),
ErrorKind::MissingRequiredArgument
);
}

#[test]
fn test_meta_get_with_arguments() {
assert_eq!(
Args::parse_from(["mbtiles", "meta-get", "src_file", "key"]),
Args {
verbose: false,
command: MetaGetValue {
file: PathBuf::from("src_file"),
key: "key".to_string(),
}
}
);
}
}
2 changes: 1 addition & 1 deletion martin-mbtiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ mod tile_copier;
pub use errors::MbtError;
pub use mbtiles::{Mbtiles, Metadata};
pub use mbtiles_pool::MbtilesPool;
pub use tile_copier::{TileCopier, TileCopierOptions};
pub use tile_copier::{copy_mbtiles_file, TileCopierOptions};
Loading

0 comments on commit 177b72e

Please sign in to comment.