A simple map viewer

tile_source.rs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. use tile::Tile;
  2. use std::path::{Path, PathBuf};
  3. #[derive(Clone, Debug)]
  4. pub struct TileSource {
  5. id: u32,
  6. url_template: String,
  7. directory: PathBuf,
  8. max_zoom: u32,
  9. }
  10. #[derive(Copy, Clone, Debug)]
  11. pub struct TileSourceId {
  12. id: u32,
  13. }
  14. impl TileSource {
  15. pub fn new<S: Into<String>, P: Into<PathBuf>>(
  16. id: u32,
  17. url_template: S,
  18. directory: P,
  19. max_zoom: u32,
  20. ) -> Self {
  21. TileSource {
  22. id: id,
  23. url_template: url_template.into(),
  24. directory: directory.into(),
  25. max_zoom: max_zoom,
  26. }
  27. }
  28. pub fn id(&self) -> TileSourceId {
  29. TileSourceId {
  30. id: self.id,
  31. }
  32. }
  33. pub fn local_tile_path(&self, tile: Tile) -> PathBuf {
  34. let mut path = PathBuf::from(&self.directory);
  35. path.push(tile.zoom.to_string());
  36. path.push(tile.tile_x.to_string());
  37. path.push(tile.tile_y.to_string() + ".png");
  38. path
  39. }
  40. pub fn remote_tile_url(&self, tile: Tile) -> String {
  41. Self::fill_template(&self.url_template, tile)
  42. }
  43. pub fn max_tile_zoom(&self) -> u32 {
  44. self.max_zoom
  45. }
  46. fn fill_template(template: &str, tile: Tile) -> String {
  47. let x_str = tile.tile_x.to_string();
  48. let y_str = tile.tile_y.to_string();
  49. let z_str = tile.zoom.to_string();
  50. //let len = (template.len() + x_str.len() + y_str.len() + z_str.len()).saturating_sub(9);
  51. //TODO use the regex crate for templates or some other more elegant method
  52. let string = template.replacen("{x}", &x_str, 1);
  53. let string = string.replacen("{y}", &y_str, 1);
  54. let string = string.replacen("{z}", &z_str, 1);
  55. return string;
  56. }
  57. }