A simple map viewer

tile_source.rs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use coord::TileCoord;
  2. use std::path::PathBuf;
  3. use url_template::UrlTemplate;
  4. #[derive(Debug)]
  5. pub struct TileSource {
  6. id: u32,
  7. url_template: UrlTemplate,
  8. directory: PathBuf,
  9. extension: String,
  10. min_zoom: u32,
  11. max_zoom: u32,
  12. }
  13. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
  14. pub struct TileSourceId {
  15. id: u32,
  16. }
  17. impl TileSource {
  18. pub fn new<S: Into<String>, P: Into<PathBuf>>(
  19. id: u32,
  20. url_template: S,
  21. directory: P,
  22. extension: String,
  23. min_zoom: u32,
  24. max_zoom: u32,
  25. ) -> Result<Self, String> {
  26. Ok(TileSource {
  27. id,
  28. url_template: UrlTemplate::new(url_template)?,
  29. directory: directory.into(),
  30. extension,
  31. min_zoom,
  32. max_zoom,
  33. })
  34. }
  35. pub fn id(&self) -> TileSourceId {
  36. TileSourceId {
  37. id: self.id,
  38. }
  39. }
  40. pub fn local_tile_path(&self, tile_coord: TileCoord) -> PathBuf {
  41. let mut path = PathBuf::from(&self.directory);
  42. path.push(tile_coord.zoom.to_string());
  43. path.push(tile_coord.x.to_string());
  44. path.push(tile_coord.y.to_string() + "." + &self.extension);
  45. path
  46. }
  47. pub fn remote_tile_url(&self, tile_coord: TileCoord) -> Option<String> {
  48. self.url_template.fill(tile_coord)
  49. }
  50. pub fn min_tile_zoom(&self) -> u32 {
  51. self.min_zoom
  52. }
  53. pub fn max_tile_zoom(&self) -> u32 {
  54. self.max_zoom
  55. }
  56. }