Skip to content

Commit

Permalink
added examples to utiles of basic mbtiles usage (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessekrubin authored Jul 25, 2024
1 parent 7117683 commit d2b341e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/utiles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ md-5 = "0.10.6"
noncrypto-digests = "0.3.2"
hex = "0.4.3"
pmtiles = { version = "0.10.0", features = ["mmap-async-tokio", "tilejson"], optional = true }
anyhow.workspace = true

[dev-dependencies]
criterion = "0.5.1"
Expand Down
44 changes: 44 additions & 0 deletions crates/utiles/examples/mbt_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use anyhow::Result;
use std::path::PathBuf;
use utiles::mbt::{MbtilesAsync, MbtilesClientAsync};
use utiles_core::utile;
use utiles_core::Tile;
fn get_utiles_test_osm_mbtiles_path() -> PathBuf {
let pwd = std::env::current_dir().unwrap();
let repo_root = pwd.parent().unwrap().parent().unwrap();
repo_root.join("test-data/mbtiles/osm-standard.z0z4.mbtiles")
}

fn printsep() {
// 80 chars
println!("{}", "-".repeat(80));
}

#[tokio::main]
async fn main() -> Result<()> {
let src = get_utiles_test_osm_mbtiles_path();
println!("mbtiles path: {:?}", src);

printsep();
let mbt = MbtilesClientAsync::open_existing(src).await?; // .await
println!("mbtiles: {:?}", mbt);

printsep();
let metadata = mbt.metadata_rows().await?;
println!("metadata: {:?}", metadata);

printsep();
let count = mbt.tiles_count().await?;
println!("tiles count: {:?}", count);

printsep();
let tile = utile!(0, 0, 0);
let a_tile = mbt.query_tile(&tile).await?;
if let Some(tile_data) = a_tile {
println!("tile (size): {:?}", tile_data.len());
} else {
println!("tile not found: {:?}", tile);
}

Ok(())
}
43 changes: 43 additions & 0 deletions crates/utiles/examples/mbt_sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use anyhow::Result;
use std::path::PathBuf;
use utiles::mbt::Mbtiles;
use utiles_core::utile;
use utiles_core::Tile;
fn get_utiles_test_osm_mbtiles_path() -> PathBuf {
let pwd = std::env::current_dir().unwrap();
let repo_root = pwd.parent().unwrap().parent().unwrap();
repo_root.join("test-data/mbtiles/osm-standard.z0z4.mbtiles")
}

fn printsep() {
// 80 chars
println!("{}", "-".repeat(80));
}

fn main() -> Result<()> {
let src = get_utiles_test_osm_mbtiles_path();
println!("mbtiles path: {:?}", src);

printsep();
let mbt = Mbtiles::open_existing(src) // .await
.expect("Failed to open mbtiles");
println!("mbtiles: {:?}", mbt);
printsep();

let metadata = mbt.metadata();
println!("metadata: {:?}", metadata);

printsep();
let count = mbt.tiles_count();
println!("tiles count: {:?}", count);

printsep();
let tile = utile!(0, 0, 0);
let a_tile = mbt.query_tile(&tile)?;
if let Some(tile_data) = a_tile {
println!("tile (size): {:?}", tile_data.len());
} else {
println!("tile not found: {:?}", tile);
}
Ok(())
}
10 changes: 10 additions & 0 deletions crates/utiles/src/mbt/mbtiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use indoc::indoc;
use rusqlite::{params, Connection, OptionalExtension};
use std::collections::HashSet;
use std::error::Error;
use std::fmt::Debug;
use std::path::Path;
use tilejson::TileJSON;
use tracing::{debug, error, warn};
Expand Down Expand Up @@ -33,6 +34,7 @@ use crate::sqlite_utiles::add_ut_functions;
use crate::utilejson::metadata2tilejson;
use crate::UtilesError;

#[derive(Debug)]
pub struct Mbtiles {
pub dbpath: DbPath,
pub(crate) conn: Connection,
Expand Down Expand Up @@ -128,6 +130,14 @@ impl Mbtiles {
Ok(Mbtiles { conn: res, dbpath })
}

pub fn query_zxy(&self, z: u8, x: u32, y: u32) -> RusqliteResult<Option<Vec<u8>>> {
query_zxy(&self.conn, z, x, y)
}

pub fn query_tile<T: TileLike>(&self, tile: &T) -> RusqliteResult<Option<Vec<u8>>> {
query_tile(&self.conn, tile)
}

pub fn from_conn(conn: Connection) -> UtilesResult<Mbtiles> {
let guessed_fspath = query_db_fspath(&conn);
let dbpath = match guessed_fspath {
Expand Down
2 changes: 1 addition & 1 deletion crates/utiles/src/mbt/mbtiles_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub trait MbtilesAsync: Sized {

async fn query_zxy(&self, z: u8, x: u32, y: u32) -> UtilesResult<Option<Vec<u8>>>;

async fn query_tile(&self, tile: Tile) -> UtilesResult<Option<Vec<u8>>> {
async fn query_tile(&self, tile: &Tile) -> UtilesResult<Option<Vec<u8>>> {
self.query_zxy(tile.z(), tile.x(), tile.y()).await
}

Expand Down
4 changes: 2 additions & 2 deletions crates/utiles/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ async fn get_dataset_tile_zxy(
}
let t = utile!(path.x, path.y, path.z);
let mbt_ds = mbtiles.unwrap();
let tile_data = mbt_ds.mbtiles.query_tile(t).await;
let tile_data = mbt_ds.mbtiles.query_tile(&t).await;
match tile_data {
Ok(data) => match data {
Some(data) => {
Expand Down Expand Up @@ -270,7 +270,7 @@ async fn get_dataset_tile_quadkey(
)
})?;
let mbt_ds = mbt_ds.unwrap();
let tile_data = mbt_ds.mbtiles.query_tile(parsed_tile).await.unwrap();
let tile_data = mbt_ds.mbtiles.query_tile(&parsed_tile).await.unwrap();
match tile_data {
Some(data) => Ok(Response::new(Body::from(data))),
None => Err((StatusCode::NOT_FOUND, "Tile not found".to_string())),
Expand Down

0 comments on commit d2b341e

Please sign in to comment.