diff --git a/src/coords.rs b/src/coords.rs index 2fdf3baf..71f9a094 100644 --- a/src/coords.rs +++ b/src/coords.rs @@ -1,6 +1,7 @@ //! File which exposes all kinds of coordinates used throughout mapr use std::fmt; +use std::fmt::Formatter; use cgmath::num_traits::Pow; use cgmath::{Matrix4, Point3, Vector3}; @@ -13,11 +14,35 @@ pub const EXTENT_UINT: u32 = 4096; pub const EXTENT_SINT: i32 = EXTENT_UINT as i32; pub const EXTENT: f64 = EXTENT_UINT as f64; pub const TILE_SIZE: f64 = 512.0; +pub const MAX_ZOOM: usize = 32; -pub type Quadkey = [u8; 32]; +#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Copy)] +pub struct Quadkey([u8; MAX_ZOOM]); + +impl Quadkey { + pub fn new(quad_encoded: &[u8]) -> Self { + let mut key = [0u8; 32]; + key[0] = quad_encoded.len() as u8; + for (i, part) in quad_encoded.iter().enumerate() { + key[i + 1] = *part; + } + Self(key) + } +} + +impl fmt::Debug for Quadkey { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let len = self.0[0] as usize; + for part in &self.0[0..len] { + write!(f, "{:?}", part)?; + } + Ok(()) + } +} /// Within each tile there is a separate coordinate system. Usually this coordinate system is -/// within [`crate::coords::EXTENT`]. +/// within [`crate::coords::EXTENT`]. Therefore, `x` and `y` must be within the bounds of +/// [`crate::coords::EXTENT`]. /// /// # Coordinate System Origin /// @@ -120,8 +145,8 @@ impl WorldTileCoords { }) } - pub fn into_world_coords(self, z: u8, zoom: f64) -> WorldCoords { - let tile_scale = 2.0.pow(self.z as f64) * TILE_SIZE; + pub fn into_world_coords(self, _z: u8, _zoom: f64) -> WorldCoords { + let _tile_scale = 2.0.pow(self.z as f64) * TILE_SIZE; let x = self.x as f64 * 512.0; let y = self.y as f64 * 512.0; @@ -160,6 +185,7 @@ impl WorldTileCoords { }) } + /// Adopted from [tilebelt](https://github.com/mapbox/tilebelt) pub fn build_quad_key(&self) -> Option { let bounds = 2u32.pow(self.z as u32); let x = self.x as u32; @@ -184,9 +210,10 @@ impl WorldTileCoords { } key[z as usize] = b; } - Some(key) + Some(Quadkey(key)) } + /// Adopted from [tilebelt](https://github.com/mapbox/tilebelt) pub fn get_children(&self) -> [WorldTileCoords; 4] { [ WorldTileCoords { @@ -421,7 +448,7 @@ mod tests { println!("{:?}\n{:?}", p1, p2); assert_eq!( - WorldCoords::from((p1.x, p1.y, 0.0)).into_world_tile(zoom.floor() as u8, zoom), + WorldCoords::from((p1.x, p1.y)).into_world_tile(zoom.floor() as u8, zoom), tile ); } @@ -435,40 +462,33 @@ mod tests { #[test] fn test_quad_key() { - fn new_quad_key_z1(a: u8) -> Quadkey { - let mut key = [0u8; 32]; - key[0] = 1; - key[1] = a; - key - } - assert_eq!( TileCoords { x: 0, y: 0, z: 1 } .into_world_tile(TileAddressingScheme::TMS) .unwrap() .build_quad_key(), - new_quad_key_z1(2) + Some(Quadkey::new(&[2])) ); assert_eq!( TileCoords { x: 0, y: 1, z: 1 } .into_world_tile(TileAddressingScheme::TMS) .unwrap() .build_quad_key(), - new_quad_key_z1(0) + Some(Quadkey::new(&[0])) ); assert_eq!( TileCoords { x: 1, y: 1, z: 1 } .into_world_tile(TileAddressingScheme::TMS) .unwrap() .build_quad_key(), - new_quad_key_z1(1) + Some(Quadkey::new(&[1])) ); assert_eq!( TileCoords { x: 1, y: 0, z: 1 } .into_world_tile(TileAddressingScheme::TMS) .unwrap() .build_quad_key(), - new_quad_key_z1(3) + Some(Quadkey::new(&[3])) ); } diff --git a/src/input/query_handler.rs b/src/input/query_handler.rs index 29d4f929..e69bca6f 100644 --- a/src/input/query_handler.rs +++ b/src/input/query_handler.rs @@ -1,10 +1,10 @@ -use super::UpdateState; + use crate::coords::WorldCoords; use crate::io::tile_cache::TileCache; -use crate::render::camera::Camera; + use crate::render::render_state::RenderState; -use cgmath::num_traits::Pow; -use cgmath::{EuclideanSpace, Point3, Vector2, Vector3, Zero}; + +use cgmath::{Vector2}; use log::info; use std::time::Duration; use winit::event::{ElementState, MouseButton}; diff --git a/src/io/geometry_index.rs b/src/io/geometry_index.rs index 878381a0..cad0fbf9 100644 --- a/src/io/geometry_index.rs +++ b/src/io/geometry_index.rs @@ -3,7 +3,7 @@ use std::collections::HashMap; use cgmath::num_traits::Signed; use cgmath::Bounded; use geo::prelude::*; -use geo_types::{CoordFloat, CoordNum, Coordinate, Geometry, LineString, Point, Polygon}; +use geo_types::{CoordFloat, Coordinate, Geometry, LineString, Point, Polygon}; use geozero::error::GeozeroError; use geozero::geo_types::GeoWriter; use geozero::{ColumnValue, FeatureProcessor, GeomProcessor, PropertyProcessor}; @@ -95,7 +95,7 @@ where type Envelope = AABB>; fn envelope(&self) -> Self::Envelope { - self.bounds.clone() + self.bounds } } @@ -180,7 +180,7 @@ impl GeomProcessor for IndexProcessor { impl PropertyProcessor for IndexProcessor { fn property( &mut self, - idx: usize, + _idx: usize, name: &str, value: &ColumnValue, ) -> Result { @@ -194,7 +194,7 @@ impl PropertyProcessor for IndexProcessor { impl FeatureProcessor for IndexProcessor { /// Begin of dataset processing - fn dataset_begin(&mut self, name: Option<&str>) -> Result<(), GeozeroError> { + fn dataset_begin(&mut self, _name: Option<&str>) -> Result<(), GeozeroError> { Ok(()) } /// End of dataset processing @@ -202,11 +202,11 @@ impl FeatureProcessor for IndexProcessor { Ok(()) } /// Begin of feature processing - fn feature_begin(&mut self, idx: u64) -> Result<(), GeozeroError> { + fn feature_begin(&mut self, _idx: u64) -> Result<(), GeozeroError> { Ok(()) } /// End of feature processing - fn feature_end(&mut self, idx: u64) -> Result<(), GeozeroError> { + fn feature_end(&mut self, _idx: u64) -> Result<(), GeozeroError> { Ok(()) } /// Begin of feature property processing diff --git a/src/io/scheduler.rs b/src/io/scheduler.rs index 30b5eeab..289cdf06 100644 --- a/src/io/scheduler.rs +++ b/src/io/scheduler.rs @@ -1,9 +1,9 @@ use std::collections::{HashMap, HashSet}; -use std::ops::Index; -use geo_types::{Geometry, Line}; + + use geozero::mvt::Tile; -use geozero::{GeozeroDatasource, ToGeo}; +use geozero::{GeozeroDatasource}; use log::{error, info}; use std::sync::mpsc::{channel, Receiver, SendError, Sender}; use std::sync::{Arc, Mutex}; @@ -83,7 +83,7 @@ impl Drop for ThreadLocalTessellatorState { ); } } -use crate::io::geometry_index::{IndexGeometry, IndexProcessor, TileIndex}; +use crate::io::geometry_index::{IndexProcessor, TileIndex}; use prost::Message; impl ThreadLocalTessellatorState { @@ -174,7 +174,7 @@ impl ThreadLocalTessellatorState { } TileFetchResult::Tile { data, coords } => { info!("parsing tile {} with {}bytes", &coords, data.len()); - let tile = parse_tile_bytes(&data).expect("failed to load tile"); + let tile = parse_tile_bytes(data).expect("failed to load tile"); for to_load in &tile_request.layers { if let Some(layer) = tile diff --git a/src/io/tile_cache.rs b/src/io/tile_cache.rs index 5d816495..46fb012b 100644 --- a/src/io/tile_cache.rs +++ b/src/io/tile_cache.rs @@ -1,9 +1,9 @@ -use crate::coords::{InnerCoords, Quadkey, WorldCoords, WorldTileCoords, TILE_SIZE}; +use crate::coords::{InnerCoords, Quadkey, WorldCoords, WorldTileCoords, EXTENT, TILE_SIZE}; use crate::io::geometry_index::IndexGeometry; use crate::io::{LayerTessellateResult, TileIndexResult}; use cgmath::num_traits::Pow; use std::collections::{btree_map, BTreeMap, HashSet}; -use std::iter; + #[derive(Default)] pub struct TileCache { @@ -57,13 +57,11 @@ impl TileCache { { let scale = 2.0.pow(z as f64 - zoom); - let delta_x = world_coords.x * scale - world_tile_coords.x as f64 * TILE_SIZE; - let delta_y = world_coords.y * scale - world_tile_coords.y as f64 * TILE_SIZE; + let delta_x = world_coords.x / TILE_SIZE * scale - world_tile_coords.x as f64; + let delta_y = world_coords.y / TILE_SIZE * scale - world_tile_coords.y as f64; - let scale = 1.0 / 512.0 * 4096.0; - - let x = delta_x * scale; - let y = delta_y * scale; + let x = delta_x * EXTENT; + let y = delta_y * EXTENT; Some(index.index.point_query(InnerCoords { x, y })) } else { None diff --git a/src/render/render_state.rs b/src/render/render_state.rs index e105f5de..170207f1 100644 --- a/src/render/render_state.rs +++ b/src/render/render_state.rs @@ -6,7 +6,7 @@ use std::{cmp, iter}; use crate::coords::{ViewRegion, TILE_SIZE}; use crate::io::scheduler::IOScheduler; -use crate::io::{LayerTessellateResult, TileRequest}; +use crate::io::{LayerTessellateResult}; use style_spec::layer::LayerPaint; use style_spec::{EncodedSrgb, Style}; use wgpu::{Buffer, Limits, Queue}; @@ -414,7 +414,7 @@ impl RenderState { for world_coords in view_region.iter() { let loaded_layers = self.buffer_pool.get_loaded_layers_at(&world_coords); - if let Some(mut available_layers) = scheduler + if let Some(available_layers) = scheduler .iter_tessellated_layers_at(&world_coords, &loaded_layers) .map(|layers| layers.collect::>()) { diff --git a/src/util/math.rs b/src/util/math.rs index df519d00..2ec52233 100644 --- a/src/util/math.rs +++ b/src/util/math.rs @@ -339,7 +339,7 @@ where } if let (Some(min), Some(max)) = (min, max) { - return Some((min, max)); + Some((min, max)) } else { None }