use std::collections::BTreeMap;
use std::fmt::Debug;
#[cfg(all(feature = "rendering", target_os = "linux"))]
use std::num::{NonZeroU8, NonZeroUsize};
use std::path::PathBuf;
use chrono::{DateTime, Utc};
use dashmap::{DashMap, Entry};
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub use maplibre_native::Image as StaticImage;
#[cfg(all(feature = "rendering", target_os = "linux"))]
use maplibre_native::Image;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use tracing::{info, warn};
#[cfg(all(feature = "rendering", target_os = "linux"))]
mod error;
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub use error::StyleError;
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub mod render_pool;
#[cfg(all(feature = "rendering", target_os = "linux"))]
use render_pool::RenderPools;
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub use render_pool::{DEFAULT_RENDERERS_PER_WORKER, RenderParams, RenderStyle};
#[cfg(feature = "rendering")]
pub use crate::image_quality::{ImageQuality, LossyQuality};
#[cfg(feature = "rendering")]
mod pixel_ratio;
#[cfg(feature = "rendering")]
pub use pixel_ratio::{InvalidPixelRatios, PixelRatioNotServed, PixelRatios};
#[cfg(feature = "rendering")]
mod tile_size;
#[cfg(feature = "rendering")]
pub use tile_size::{InvalidTileSize, TileSize};
#[cfg(feature = "rendering")]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct ImageQualities {
pub webp: ImageQuality,
pub jpeg: LossyQuality,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
#[cfg_attr(feature = "unstable-schemas", derive(schemars::JsonSchema, utoipa::ToSchema))]
pub enum StyleKind {
Vector,
Raster,
Hybrid,
}
#[skip_serializing_none]
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "unstable-schemas", derive(schemars::JsonSchema, utoipa::ToSchema))]
pub struct CatalogStyleEntry {
#[cfg_attr(feature = "unstable-schemas", schemars(with = "String"))]
#[cfg_attr(feature = "unstable-schemas", schema(value_type = String))]
pub path: PathBuf,
#[serde(rename = "type")]
pub kind: Option<StyleKind>,
pub version_hash: Option<String>,
pub layer_count: Option<u32>,
pub colors: Option<Vec<String>>,
pub last_modified_at: Option<DateTime<Utc>>,
}
pub type StyleCatalog = BTreeMap<String, CatalogStyleEntry>;
#[derive(Debug, Clone, Default)]
pub struct StyleSources {
sources: DashMap<String, StyleSource>,
#[cfg(all(feature = "rendering", target_os = "linux"))]
pools: Option<RenderPools>,
#[cfg(feature = "rendering")]
pixel_ratios: PixelRatios,
#[cfg(all(feature = "rendering", target_os = "linux"))]
png_max_colors: Option<u16>,
#[cfg(all(feature = "rendering", target_os = "linux"))]
image_quality: ImageQualities,
}
#[derive(Clone, Debug)]
pub struct StyleSource {
path: PathBuf,
}
impl StyleSources {
#[must_use]
pub fn style_json_path(&self, style_id: &str) -> Option<PathBuf> {
let style_id = style_id.trim_end_matches(".json").trim();
let item = self.sources.get(style_id)?;
Some(item.path.clone())
}
#[must_use]
pub fn get_catalog(&self) -> StyleCatalog {
let mut entries = StyleCatalog::new();
for source in &self.sources {
entries.insert(
source.key().clone(),
CatalogStyleEntry {
path: source.path.clone(),
kind: None,
version_hash: None,
layer_count: None,
colors: None,
last_modified_at: None,
},
);
}
entries
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
#[must_use]
pub const fn is_rendering_enabled(&self) -> bool {
self.pools.is_some()
}
pub fn add_style(&self, id: String, path: PathBuf) {
debug_assert!(path.is_file());
debug_assert_ne!(id, "");
match self.sources.entry(id) {
Entry::Occupied(v) => {
warn!(
source.id = %v.key(),
style.path.kept = %v.get().path.display(),
style.path.dropped = %path.display(),
"Ignoring duplicate style source: already configured for another path"
);
}
Entry::Vacant(v) => {
info!(
source.id = %v.key(),
style.path = %path.display(),
"Configured style source"
);
v.insert(StyleSource { path });
}
}
}
#[must_use]
pub fn len(&self) -> usize {
self.sources.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.sources.is_empty()
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub async fn render(&self, style: impl Into<RenderStyle>, z: u8, x: u32, y: u32) -> Result<Image, StyleError> {
self.render_with_pixel_ratio(style, z, x, y, NonZeroU8::MIN).await
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub async fn render_with_pixel_ratio(
&self,
style: impl Into<RenderStyle>,
z: u8,
x: u32,
y: u32,
pixel_ratio: NonZeroU8,
) -> Result<Image, StyleError> {
self.pools
.as_ref()
.ok_or(StyleError::RenderingIsDisabled)?
.render_tile_with_pixel_ratio(style, z, x, y, pixel_ratio)
.await
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub async fn render_of_size(
&self,
style: impl Into<RenderStyle>,
z: u8,
x: u32,
y: u32,
tile_size: TileSize,
pixel_ratio: NonZeroU8,
) -> Result<Image, StyleError> {
self.pools
.as_ref()
.ok_or(StyleError::RenderingIsDisabled)?
.render_tile_of_size(style, z, x, y, tile_size, pixel_ratio)
.await
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub async fn render_static(&self, params: RenderParams) -> Result<Image, StyleError> {
self.pools
.as_ref()
.ok_or(StyleError::RenderingIsDisabled)?
.render_static(params)
.await
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub fn enable_rendering(
&mut self,
workers: Option<NonZeroUsize>,
tile_size: TileSize,
renderers_per_worker: NonZeroUsize,
ambient_cache_bytes: Option<u64>,
) -> Result<(), std::io::Error> {
self.pools = Some(RenderPools::new(
workers,
tile_size,
renderers_per_worker,
ambient_cache_bytes,
)?);
Ok(())
}
#[cfg(feature = "rendering")]
pub fn set_pixel_ratios(&mut self, ratios: PixelRatios) {
self.pixel_ratios = ratios;
}
#[cfg(feature = "rendering")]
#[must_use]
pub fn pixel_ratios(&self) -> PixelRatios {
self.pixel_ratios
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub fn disable_rendering(&mut self) {
self.pools = None;
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub fn set_png_max_colors(&mut self, max_colors: Option<u16>) {
self.png_max_colors = max_colors;
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
#[must_use]
pub fn png_max_colors(&self) -> Option<u16> {
self.png_max_colors
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
pub fn set_image_quality(&mut self, quality: ImageQualities) {
self.image_quality = quality;
}
#[cfg(all(feature = "rendering", target_os = "linux"))]
#[must_use]
pub fn image_quality(&self) -> ImageQualities {
self.image_quality
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use super::*;
#[test]
fn style_external() {
let style_dir = Path::new("../tests/fixtures/styles/");
let styles = StyleSources::default();
styles.add_style("maplibre_demo".to_owned(), style_dir.join("maplibre_demo.json"));
styles.add_style(
"maptiler_basic".to_owned(),
style_dir.join("src2").join("maptiler_basic.json"),
);
styles.add_style(
"osm-liberty-lite".to_owned(),
style_dir.join("src2").join("osm-liberty-lite.json"),
);
assert_eq!(styles.sources.len(), 3);
let mut catalog = styles.get_catalog();
for entry in catalog.values_mut() {
entry.path = entry
.path
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "/")
.into();
}
insta::with_settings!({sort_maps => true}, {
insta::assert_json_snapshot!(catalog, @r#"
{
"maplibre_demo": {
"path": "../tests/fixtures/styles/maplibre_demo.json"
},
"maptiler_basic": {
"path": "../tests/fixtures/styles/src2/maptiler_basic.json"
},
"osm-liberty-lite": {
"path": "../tests/fixtures/styles/src2/osm-liberty-lite.json"
}
}
"#);
});
assert_eq!(styles.style_json_path("NON_EXISTENT"), None);
assert_eq!(
styles.style_json_path("maplibre_demo.json"),
Some(style_dir.join("maplibre_demo.json"))
);
assert_eq!(styles.style_json_path("src2"), None);
let src2_dir = style_dir.join("src2");
assert_eq!(
styles.style_json_path("maptiler_basic"),
Some(src2_dir.join("maptiler_basic.json"))
);
assert_eq!(
styles.style_json_path("maptiler_basic.json"),
Some(src2_dir.join("maptiler_basic.json"))
);
assert_eq!(
styles.style_json_path("osm-liberty-lite.json"),
Some(src2_dir.join("osm-liberty-lite.json"))
);
}
}