cartography 0.11.0

Cartography is a map rendering library for Geographic features expressed using [georust](https://georust.org/) libraries.
Documentation
use tiles_client::Client;

use crate::{Feature, ImageData, ImageFeature, Layer, Projection};

/// Represent a tile in a tile map (such as coming from OSM tile server).
pub struct Tile(tiles_client::Tile);

impl ImageData for Tile
{
  fn bounding_box(&self) -> geo::Rect
  {
    self.0.bounding_box()
  }
  fn pixel_size(&self) -> (u32, u32)
  {
    self.0.pixel_size()
  }
  fn rgba_data(&self) -> Vec<u8>
  {
    self.0.rgba_data()
  }
}

/// Allow to use tile from a tile server in a rendered map.
pub struct TilesLayer
{
  projection: Projection,
  client: tiles_client::Client,
}

impl TilesLayer
{
  /// Create a new tile layer with the given client.
  pub fn new(client: Client) -> Self
  {
    Self {
      client,
      projection: Projection::wgs84(),
    }
  }
}

impl<TFeature> Layer<TFeature> for TilesLayer
where
  TFeature: Feature + From<ImageFeature<Tile>>,
{
  fn projection(&self) -> &crate::Projection
  {
    &self.projection
  }
  fn features<'a>(
    &'a self,
    rect: geo::Rect,
    zoom_level: f64,
  ) -> Box<dyn Iterator<Item = crate::RefOrValue<'a, TFeature>> + 'a>
  {
    Box::new(
      self
        .client
        .tiles(rect, zoom_level.ceil() as u32, true)
        .into_iter()
        .map(|x| {
          let tile: ImageFeature<Tile> = Tile(x).into();
          let tile: TFeature = tile.into();
          tile.into()
        }),
    )
  }
}