Only load a certain extent

This commit is contained in:
Maximilian Ammann 2021-12-18 14:30:19 +01:00
parent 1315cb3dca
commit cd11d9bbf1
7 changed files with 39 additions and 24 deletions

View File

@ -11,13 +11,15 @@ fn main() {
let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = env::var("OUT_DIR").unwrap();
let out = PathBuf::from(Path::new(&out_dir).join("munich-tiles")); let out = PathBuf::from(Path::new(&out_dir).join("munich-tiles"));
fs::remove_dir_all(&out); fs::remove_dir_all(&out).unwrap();
//let out = PathBuf::from(Path::new(&root_dir).join("test-data/munich-tiles")); //let out = PathBuf::from(Path::new(&root_dir).join("test-data/munich-tiles"));
let source = Path::new(&root_dir).join("test-data/maptiler-osm-2017-07-03-v3.6.1-germany_munich.mbtiles"); let source = Path::new(&root_dir).join("test-data/maptiler-osm-2017-07-03-v3.6.1-germany_munich.mbtiles");
// Pack tiles around Maxvorstadt (100 tiles in each direction)
extract(source, extract(source,
out, out,
12,
(2179 - 100)..(2179 + 100),
(1421 - 100)..(1421 + 100),
).unwrap(); ).unwrap();
} }

View File

@ -1,11 +1,12 @@
use std::{fs, io}; use std::{fs, io};
use std::collections::{HashMap}; use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::path::{Path}; use std::ops::Range;
use std::path::Path;
use flate2::bufread::GzDecoder; use flate2::bufread::GzDecoder;
use rusqlite::{Connection, Row}; use rusqlite::{Connection, params, Row};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -32,8 +33,10 @@ impl From<rusqlite::Error> for Error {
} }
pub fn extract<P: AsRef<Path>, R: AsRef<Path>>(input_mbtiles: P, pub fn extract<P: AsRef<Path>, R: AsRef<Path>>(input_mbtiles: P,
output_dir: R) output_dir: R,
-> Result<(), Error> { z: u32,
x_range: Range<u32>,
y_range: Range<u32>) -> Result<(), Error> {
let input_path = input_mbtiles.as_ref().to_path_buf(); let input_path = input_mbtiles.as_ref().to_path_buf();
if !input_path.is_file() { if !input_path.is_file() {
return Err(Error::IO(format!("Input file {:?} is not a file", input_path))); return Err(Error::IO(format!("Input file {:?} is not a file", input_path)));
@ -51,8 +54,17 @@ pub fn extract<P: AsRef<Path>, R: AsRef<Path>>(input_mbtiles: P,
// language=SQL // language=SQL
let mut prepared_statement = connection let mut prepared_statement = connection
.prepare("SELECT zoom_level, tile_column, tile_row, tile_data FROM tiles;")?; .prepare("SELECT zoom_level, tile_column, tile_row, tile_data
let mut tiles_rows = prepared_statement.query([])?; FROM tiles
WHERE (zoom_level = ?1) AND
(tile_column BETWEEN ?2 AND ?3) AND
(tile_row BETWEEN ?4 AND ?5);")?;
let mut tiles_rows = prepared_statement.query(params![
z,
x_range.start, x_range.end,
flip_vertical_axis(z, y_range.end), flip_vertical_axis(z, y_range.start) // in mbtiles it is stored flipped
])?;
while let Ok(Some(tile)) = tiles_rows.next() { while let Ok(Some(tile)) = tiles_rows.next() {
extract_tile(&tile, &output_path)?; extract_tile(&tile, &output_path)?;
@ -61,6 +73,11 @@ pub fn extract<P: AsRef<Path>, R: AsRef<Path>>(input_mbtiles: P,
Ok(()) Ok(())
} }
fn flip_vertical_axis(zoom: u32, value: u32)-> u32 {
2u32.pow(zoom) - 1 - value
}
fn extract_tile(tile: &Row, fn extract_tile(tile: &Row,
output_path: &Path) output_path: &Path)
-> Result<(), Error> { -> Result<(), Error> {
@ -69,7 +86,7 @@ fn extract_tile(tile: &Row,
tile.get::<_, u32>(2)?); tile.get::<_, u32>(2)?);
// Flip vertical axis // Flip vertical axis
y = 2u32.pow(z) - 1 - y; y = flip_vertical_axis(z, y);
let tile_dir = output_path.join(format!("{}/{}", z, x)); let tile_dir = output_path.join(format!("{}/{}", z, x));

View File

@ -215,10 +215,10 @@ impl Decode<Feature> for (&ProtoLayer, ProtoFeature) {
let value = chunk[1]; let value = chunk[1];
let keys = &layer.keys; let keys = &layer.keys;
if let Some(actualKey) = keys.get(key as usize) { if let Some(actual_key) = keys.get(key as usize) {
let values = &layer.values; let values = &layer.values;
if let Some(actualValue) = values.get(value as usize) { if let Some(actual_value) = values.get(value as usize) {
properties.insert(actualKey.clone(), actualValue.clone().decode()); properties.insert(actual_key.clone(), actual_value.clone().decode());
} }
} }
} }

View File

@ -1,5 +1,5 @@
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, Read}; use std::io::{BufRead, BufReader};
use std::path::{Path}; use std::path::{Path};
use protobuf::Message; use protobuf::Message;
@ -21,7 +21,7 @@ pub mod grid;
pub mod error; pub mod error;
pub fn parse_tile<P: AsRef<Path>>(path: P) -> Result<Tile, Error> { pub fn parse_tile<P: AsRef<Path>>(path: P) -> Result<Tile, Error> {
let mut f = File::open(path)?; let f = File::open(path)?;
let mut reader = BufReader::new(f); let mut reader = BufReader::new(f);
parse_tile_reader(&mut reader).into() parse_tile_reader(&mut reader).into()
} }

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
use crate::geometry::Geometry; use crate::geometry::Geometry;
use crate::protos::vector_tile::{ use crate::protos::vector_tile::{
Tile as ProtoTile, Tile_Feature, Tile_Feature as ProtoFeature, Tile_Layer as ProtoLayer, Tile as ProtoTile, Tile_Feature as ProtoFeature, Tile_Layer as ProtoLayer,
}; };
#[derive(Debug)] #[derive(Debug)]

View File

@ -5,9 +5,7 @@ use include_dir::{Dir, File, include_dir};
static TILES: Dir = include_dir!("$OUT_DIR/munich-tiles"); static TILES: Dir = include_dir!("$OUT_DIR/munich-tiles");
pub fn get_tile_count() -> usize { static mut TEST: u32 = 0;
TILES.files().count()
}
pub fn get_source_path() -> &'static str { pub fn get_source_path() -> &'static str {
concat!(env!("OUT_DIR"), "/munich-tiles") concat!(env!("OUT_DIR"), "/munich-tiles")
@ -18,12 +16,11 @@ pub fn get_tile(x: u32, y: u32, z: u32) -> Option<&'static File<'static>> {
} }
mod tests { mod tests {
use crate::io::static_database::{get_tile, get_tile_count}; use super::{get_tile};
#[test] #[test]
fn test_tiles_available() { fn test_tiles_available() {
assert_eq!(get_tile_count(), 1); // World overview assert!(get_tile(0,0,0).is_none()); // World overview
assert!(get_tile(0,0,0).is_some()); // World overview
assert!(get_tile(2179, 1421,12).is_some()); // Maxvorstadt Munich assert!(get_tile(2179, 1421,12).is_some()); // Maxvorstadt Munich
} }
} }

View File

@ -1,7 +1,6 @@
use std::f32::consts::FRAC_PI_2; use std::f32::consts::FRAC_PI_2;
use cgmath::prelude::*; use cgmath::prelude::*;
use log::info;
use crate::render::shader_ffi::CameraUniform; use crate::render::shader_ffi::CameraUniform;