Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Dec 15, 2023
1 parent 003fe21 commit 1fafe18
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 70 deletions.
4 changes: 3 additions & 1 deletion martin-tile-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tile_grid::{tms, Tms, Xyz};
pub const EARTH_CIRCUMFERENCE: f64 = 40_075_016.685_578_5;
pub const EARTH_RADIUS: f64 = EARTH_CIRCUMFERENCE / 2.0 / PI;

pub const MAX_ZOOM: u8 = 30;
pub const MAX_ZOOM: u8 = 24;
use std::sync::OnceLock;

fn web_merc() -> &'static Tms {
Expand Down Expand Up @@ -209,6 +209,8 @@ pub fn tile_index(lon: f64, lat: f64, zoom: u8) -> (u32, u32) {
#[must_use]
pub fn xyz_to_bbox(zoom: u8, min_x: u32, min_y: u32, max_x: u32, max_y: u32) -> [f64; 4] {
assert!(zoom <= MAX_ZOOM, "zoom {zoom} must be <= {MAX_ZOOM}");
assert!(min_x <= max_x, "min_x {min_x} must be <= max_x {max_x}");
assert!(min_y <= max_y, "min_y {min_y} must be <= max_y {max_y}");
let left_top_bounds = web_merc().xy_bounds(&Xyz::new(u64::from(min_x), u64::from(min_y), zoom));
let right_bottom_bounds =
web_merc().xy_bounds(&Xyz::new(u64::from(max_x), u64::from(max_y), zoom));
Expand Down
7 changes: 3 additions & 4 deletions mbtiles/src/copier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;
use clap::{Args, ValueEnum};
use enum_display::EnumDisplay;
use log::{debug, info, trace};
use martin_tile_utils::bbox_to_xyz;
use martin_tile_utils::{bbox_to_xyz, MAX_ZOOM};
use serde::{Deserialize, Serialize};
use sqlite_hashes::rusqlite::Connection;
use sqlx::{query, Executor as _, Row, SqliteConnection};
Expand Down Expand Up @@ -605,9 +605,8 @@ impl MbtileCopierInt {
if !self.options.bbox.is_empty() {
sql.push_str(" AND (\n");
for (idx, bbox) in self.options.bbox.iter().enumerate() {
// Use maximum zoom value for easy filtering, converting it on the fly to the actual zoom level
const MAX_ZOOM: u8 = 30;

// Use maximum zoom value for easy filtering,
// converting it on the fly to the actual zoom level
let (min_x, min_y, max_x, max_y) =
bbox_to_xyz(bbox.left, bbox.bottom, bbox.right, bbox.top, MAX_ZOOM);
trace!("Bounding box {bbox} converted to {min_x},{min_y},{max_x},{max_y} at zoom {MAX_ZOOM}");
Expand Down
68 changes: 27 additions & 41 deletions mbtiles/tests/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,55 +247,41 @@ async fn convert(
let mem = Mbtiles::new(":memory:")?;
let (frm_mbt, _frm_cn) = new_file!(convert, frm_type, METADATA_V1, TILES_V1, "{frm}-{to}");

// let mut opt = copier(&frm_mbt, &mem);
// opt.dst_type_cli = Some(dst_type);
// let dmp = dump(&mut opt.run().await?).await?;
// pretty_assert_eq!(databases.dump("v1", dst_type), &dmp);
//
// let mut opt = copier(&frm_mbt, &mem);
// opt.dst_type_cli = Some(dst_type);
// opt.zoom_levels.push(6);
// let z6only = dump(&mut opt.run().await?).await?;
// assert_snapshot!(z6only, "v1__z6__{frm}-{to}");

// Bounding box -180,-85.0511287798066,-146.25,-81.09321385260837
//
// converted to
//
// 0,973078527,100663296,1073741823 at zoom 30
let mut opt = copier(&frm_mbt, &mem);
opt.dst_type_cli = Some(dst_type);
let dmp = dump(&mut opt.run().await?).await?;
pretty_assert_eq!(databases.dump("v1", dst_type), &dmp);

// (
//
// (
// tile_column * (1 << (30 - zoom_level))
// ) BETWEEN 0 AND 100663296
//
// AND
//
// (
// tile_row * (1 << (30 - zoom_level))
// ) BETWEEN 100663296 AND 0
//
// )
let mut opt = copier(&frm_mbt, &mem);
opt.dst_type_cli = Some(dst_type);
opt.zoom_levels.push(6);
let z6only = dump(&mut opt.run().await?).await?;
assert_snapshot!(z6only, "v1__z6__{frm}-{to}");

let mut opt = copier(&frm_mbt, &mem);
opt.dst_type_cli = Some(dst_type);
let bounds = xyz_to_bbox(5, 0, invert_y_value(5, 2), 2, invert_y_value(5, 0)).into();

opt.bbox.push(bounds);
// Filter (0, 0, 2, 2) in mbtiles coordinates, which is (0, 2^5-1-2, 2, 2^5-1-0) = (0, 29, 2, 31) in XYZ coordinates, and slightly decrease it
let mut bbox = xyz_to_bbox(5, 0, invert_y_value(5, 2), 2, invert_y_value(5, 0));
bbox[0] += 180.0 / f64::from(1 << 5) * 0.1;
bbox[1] += 90.0 / f64::from(1 << 5) * 0.1;
bbox[2] -= 180.0 / f64::from(1 << 5) * 0.1;
bbox[3] -= 90.0 / f64::from(1 << 5) * 0.1;
opt.bbox.push(bbox.into());

let dmp = dump(&mut opt.run().await?).await?;
assert_snapshot!(dmp, "v1__bbox__{frm}-{to}");

// let mut opt = copier(&frm_mbt, &mem);
// opt.dst_type_cli = Some(dst_type);
// opt.min_zoom = Some(6);
// pretty_assert_eq!(&z6only, &dump(&mut opt.run().await?).await?);
//
// let mut opt = copier(&frm_mbt, &mem);
// opt.dst_type_cli = Some(dst_type);
// opt.min_zoom = Some(6);
// opt.max_zoom = Some(6);
// pretty_assert_eq!(&z6only, &dump(&mut opt.run().await?).await?);
let mut opt = copier(&frm_mbt, &mem);
opt.dst_type_cli = Some(dst_type);
opt.min_zoom = Some(6);
pretty_assert_eq!(&z6only, &dump(&mut opt.run().await?).await?);

let mut opt = copier(&frm_mbt, &mem);
opt.dst_type_cli = Some(dst_type);
opt.min_zoom = Some(6);
opt.max_zoom = Some(6);
pretty_assert_eq!(&z6only, &dump(&mut opt.run().await?).await?);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand All @@ -26,7 +26,12 @@ CREATE TABLE tiles (
tile_row integer NOT NULL,
tile_data blob,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, blob(edit-v1) )',
'( 5, 1, 2, blob() )',
'( 5, 2, 2, blob(remove) )',
'( 6, 1, 4, blob(edit-v1) )',
]

[[]]
type = 'index'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand All @@ -27,7 +27,12 @@ CREATE TABLE tiles_with_hash (
tile_data blob,
tile_hash text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, blob(edit-v1), "EFE0AE5FD114DE99855BC2838BE97E1D" )',
'( 5, 1, 2, blob(), "D41D8CD98F00B204E9800998ECF8427E" )',
'( 5, 2, 2, blob(remove), "0F6969D7052DA9261E31DDB6E88C136E" )',
'( 6, 1, 4, blob(edit-v1), "EFE0AE5FD114DE99855BC2838BE97E1D" )',
]

[[]]
type = 'index'
Expand Down
15 changes: 12 additions & 3 deletions mbtiles/tests/snapshots/copy__convert@v1__bbox__flat-norm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ sql = '''
CREATE TABLE images (
tile_id text NOT NULL PRIMARY KEY,
tile_data blob)'''
values = []
values = [
'( "0F6969D7052DA9261E31DDB6E88C136E", blob(remove) )',
'( "D41D8CD98F00B204E9800998ECF8427E", blob() )',
'( "EFE0AE5FD114DE99855BC2838BE97E1D", blob(edit-v1) )',
]

[[]]
type = 'table'
Expand All @@ -21,7 +25,12 @@ CREATE TABLE map (
tile_row integer NOT NULL,
tile_id text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, "EFE0AE5FD114DE99855BC2838BE97E1D" )',
'( 5, 1, 2, "D41D8CD98F00B204E9800998ECF8427E" )',
'( 5, 2, 2, "0F6969D7052DA9261E31DDB6E88C136E" )',
'( 6, 1, 4, "EFE0AE5FD114DE99855BC2838BE97E1D" )',
]

[[]]
type = 'table'
Expand All @@ -31,7 +40,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand All @@ -26,7 +26,12 @@ CREATE TABLE tiles (
tile_row integer NOT NULL,
tile_data blob,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, blob(edit-v1) )',
'( 5, 1, 2, blob() )',
'( 5, 2, 2, blob(remove) )',
'( 6, 1, 4, blob(edit-v1) )',
]

[[]]
type = 'index'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand All @@ -27,7 +27,12 @@ CREATE TABLE tiles_with_hash (
tile_data blob,
tile_hash text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, blob(edit-v1), "EFE0AE5FD114DE99855BC2838BE97E1D" )',
'( 5, 1, 2, blob(), "D41D8CD98F00B204E9800998ECF8427E" )',
'( 5, 2, 2, blob(remove), "0F6969D7052DA9261E31DDB6E88C136E" )',
'( 6, 1, 4, blob(edit-v1), "EFE0AE5FD114DE99855BC2838BE97E1D" )',
]

[[]]
type = 'index'
Expand Down
15 changes: 12 additions & 3 deletions mbtiles/tests/snapshots/copy__convert@v1__bbox__hash-norm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ sql = '''
CREATE TABLE images (
tile_id text NOT NULL PRIMARY KEY,
tile_data blob)'''
values = []
values = [
'( "0F6969D7052DA9261E31DDB6E88C136E", blob(remove) )',
'( "D41D8CD98F00B204E9800998ECF8427E", blob() )',
'( "EFE0AE5FD114DE99855BC2838BE97E1D", blob(edit-v1) )',
]

[[]]
type = 'table'
Expand All @@ -21,7 +25,12 @@ CREATE TABLE map (
tile_row integer NOT NULL,
tile_id text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, "EFE0AE5FD114DE99855BC2838BE97E1D" )',
'( 5, 1, 2, "D41D8CD98F00B204E9800998ECF8427E" )',
'( 5, 2, 2, "0F6969D7052DA9261E31DDB6E88C136E" )',
'( 6, 1, 4, "EFE0AE5FD114DE99855BC2838BE97E1D" )',
]

[[]]
type = 'table'
Expand All @@ -31,7 +40,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand All @@ -26,7 +26,12 @@ CREATE TABLE tiles (
tile_row integer NOT NULL,
tile_data blob,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, blob(edit-v1) )',
'( 5, 1, 2, blob() )',
'( 5, 2, 2, blob(remove) )',
'( 6, 1, 4, blob(edit-v1) )',
]

[[]]
type = 'index'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand All @@ -27,7 +27,12 @@ CREATE TABLE tiles_with_hash (
tile_data blob,
tile_hash text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, blob(edit-v1), "EFE0AE5FD114DE99855BC2838BE97E1D" )',
'( 5, 1, 2, blob(), "D41D8CD98F00B204E9800998ECF8427E" )',
'( 5, 2, 2, blob(remove), "0F6969D7052DA9261E31DDB6E88C136E" )',
'( 6, 1, 4, blob(edit-v1), "EFE0AE5FD114DE99855BC2838BE97E1D" )',
]

[[]]
type = 'index'
Expand Down
15 changes: 12 additions & 3 deletions mbtiles/tests/snapshots/copy__convert@v1__bbox__norm-norm.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ sql = '''
CREATE TABLE images (
tile_id text NOT NULL PRIMARY KEY,
tile_data blob)'''
values = []
values = [
'( "0F6969D7052DA9261E31DDB6E88C136E", blob(remove) )',
'( "D41D8CD98F00B204E9800998ECF8427E", blob() )',
'( "EFE0AE5FD114DE99855BC2838BE97E1D", blob(edit-v1) )',
]

[[]]
type = 'table'
Expand All @@ -21,7 +25,12 @@ CREATE TABLE map (
tile_row integer NOT NULL,
tile_id text,
PRIMARY KEY(zoom_level, tile_column, tile_row))'''
values = []
values = [
'( 5, 1, 1, "EFE0AE5FD114DE99855BC2838BE97E1D" )',
'( 5, 1, 2, "D41D8CD98F00B204E9800998ECF8427E" )',
'( 5, 2, 2, "0F6969D7052DA9261E31DDB6E88C136E" )',
'( 6, 1, 4, "EFE0AE5FD114DE99855BC2838BE97E1D" )',
]

[[]]
type = 'table'
Expand All @@ -31,7 +40,7 @@ CREATE TABLE metadata (
name text NOT NULL PRIMARY KEY,
value text)'''
values = [
'( "agg_tiles_hash", "D41D8CD98F00B204E9800998ECF8427E" )',
'( "agg_tiles_hash", "012434681F0EBF296906D6608C54D632" )',
'( "md-edit", "value - v1" )',
'( "md-remove", "value - remove" )',
'( "md-same", "value - same" )',
Expand Down
Loading

0 comments on commit 1fafe18

Please sign in to comment.