Skip to content
Closed
Binary file not shown.
Binary file not shown.
30 changes: 20 additions & 10 deletions valhalla-graphtile/src/graph_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::tile_hierarchy::{STANDARD_LEVELS, TRANSIT_LEVEL};
use crate::tile_hierarchy::ValhallaLevel;
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer};
use std::fmt::{Display, Formatter};
Expand Down Expand Up @@ -44,8 +44,8 @@ pub enum InvalidGraphIdError {
/// # Hierarchy (level + Tile ID)
///
/// Valhalla organizes tiles into several levels.
/// For a description of the common ones, see [`STANDARD_LEVELS`]
/// and [`TRANSIT_LEVEL`].
/// For a description of the common ones, see [`STANDARD_LEVELS`](crate::tile_hierarchy::STANDARD_LEVELS)
/// and [`TRANSIT_LEVEL`](crate::tile_hierarchy::TRANSIT_LEVEL).
/// Each level includes progressively more "local" roads.
/// This helps improve efficiency.
///
Expand Down Expand Up @@ -213,16 +213,14 @@ impl GraphId {
///
/// # Errors
///
/// This will fail if the tile is invalid for this level of tiling.
/// This will fail if the level is not a standard level or the transit level,
/// or if the tile is invalid for this level of tiling.
/// TODO: It seems like we could do this check at tile creation time?
pub fn file_path(&self, extension: &str) -> Result<PathBuf, InvalidGraphIdError> {
// This is quite ugly and prone to failure; this is a pretty literal C++ translation
let level_number = self.level();
let level = if level_number == TRANSIT_LEVEL.level {
&*TRANSIT_LEVEL
} else {
&STANDARD_LEVELS[level_number as usize]
};
let level = ValhallaLevel::try_from(self.level())
.map_err(|_| InvalidGraphIdError::Level)?
.definition();

let max_id = level.tiling_system.n_cols * level.tiling_system.n_rows - 1;
let tile_id = self.tile_id();
Expand Down Expand Up @@ -423,4 +421,16 @@ mod tests {
Ok("3/001/000/000.gph".into())
);
}

#[test]
fn test_graph_id_file_path_unknown_level() {
for level in 4..=MAX_HIERARCHY_LEVEL {
assert_eq!(
GraphId::try_from_components(level, 0, 0)
.unwrap()
.file_path("gph"),
Err(InvalidGraphIdError::Level)
);
}
}
}
Loading
Loading