Fix mbtiles extraction and add deflating

This commit is contained in:
Maximilian Ammann 2021-12-18 11:43:55 +01:00
parent 74b62791d1
commit 99ba556110
2 changed files with 18 additions and 11 deletions

View File

@ -12,6 +12,7 @@ vector-tile = { path = "../vector_tile" }
tokio = { version = "1.14", features = ["full"] }
rusqlite = "0.26"
serde_json = "1.0"
flate2 = "1.0"
[[bin]]
name = "download_tiles"

View File

@ -1,11 +1,13 @@
use std::{fs, io};
use std::collections::{BTreeMap, HashMap};
use std::collections::{HashMap};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::path::{Path};
use flate2::bufread::GzDecoder;
use rusqlite::{Connection, Row};
#[derive(Debug)]
pub enum Error {
IO(String)
}
@ -45,12 +47,12 @@ pub fn extract<P: AsRef<Path>, R: AsRef<Path>>(input_mbtiles: P,
fs::create_dir_all(&output_path)?;
extract_metadata(&connection, &output_path);
extract_metadata(&connection, &output_path)?;
// language=SQL
let mut tiles_rows = connection
.prepare("SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles;")?
.query([])?;
let mut prepared_statement = connection
.prepare("SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles;")?;
let mut tiles_rows = prepared_statement.query([])?;
while let Ok(Some(tile)) = tiles_rows.next() {
extract_tile(&tile, &output_path)?;
@ -73,9 +75,12 @@ fn extract_tile(tile: &Row,
fs::create_dir_all(&tile_dir)?;
let tile_path = output_path.join(format!("{}.{}", y, "pbf"));
let tile_path = tile_dir.join(format!("{}.{}", y, "pbf"));
let tile_data = tile.get::<_, Vec<u8>>(3)?;
let mut decoder = GzDecoder::new(tile_data.as_ref());
let mut tile_file = File::create(tile_path)?;
tile_file.write_all(&tile.get::<_, Vec<u8>>(3)?)?;
io::copy(&mut decoder, &mut tile_file)?;
Ok(())
}
@ -83,11 +88,12 @@ fn extract_metadata(connection: &Connection,
output_path: &Path)
-> Result<(), Error> {
// language=SQL
let mut metadata = connection
.prepare("SELECT name, value FROM metadata;")?
let mut prepared_statement = connection
.prepare("SELECT name, value FROM metadata;")?;
let metadata = prepared_statement
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})?;;
})?;
let metadata_map: HashMap<String, String> = metadata.filter_map(|result| {
match result {