tiles-client 0.10.0

Client for downloading tiles.
Documentation
use std::path::PathBuf;

/// Information about the tile.
pub struct Tile
{
  pub(crate) tile_x: u32,
  pub(crate) tile_y: u32,
  pub(crate) tile_z: u32,
  /// Filename with the data for the tiles
  pub(crate) filename: PathBuf,
  pub(crate) bounding_box: geo::Rect,
  pub(crate) image: ccutils::sync::ArcMutex<Option<image::DynamicImage>>,
}

impl Tile
{
  fn load_image(&self)
  {
    if !self.filename.exists() || self.image.lock().is_ok_and(|x| x.is_some())
    {
      return;
    }
    let image = image::open(&self.filename);
    match image
    {
      Ok(image) =>
      {
        *self.image.lock().unwrap() = Some(image);
      }
      Err(e) =>
      {
        log::error!(
          "Failed to read image {} with {:?}",
          self.filename.display(),
          e
        );
      }
    }
  }
  /// Return the x-index
  pub fn tile_x(&self) -> u32
  {
    self.tile_x
  }
  /// Return the y-index
  pub fn tile_y(&self) -> u32
  {
    self.tile_y
  }
  /// Return the zoom level index
  pub fn tile_z(&self) -> u32
  {
    self.tile_z
  }
  /// Return the bounding box, in geographic coordinates
  pub fn bounding_box(&self) -> geo::Rect
  {
    self.bounding_box
  }
  /// Return the filename
  pub fn filename(&self) -> &PathBuf
  {
    &self.filename
  }
  /// Return if the file was downloaded
  pub fn is_cached(&self) -> bool
  {
    self.filename.exists()
  }
  /// Size of the tile in pixel, return `(width,height)`
  pub fn pixel_size(&self) -> (u32, u32)
  {
    self.load_image();
    self.image.lock().map_or(Default::default(), |x| {
      x.as_ref()
        .map_or(Default::default(), |x| (x.width(), x.height()))
    })
  }
  /// rgba 8bit data
  pub fn rgba_data(&self) -> Vec<u8>
  {
    self.load_image();
    self.image.lock().map_or(Default::default(), |x| {
      x.as_ref()
        .map_or(Default::default(), |x| x.to_rgba8().into_raw().to_vec())
    })
  }
}