async_geotiff/style/
params.rs1use super::{raster::RasterPlan, TileFormat};
3use crate::error::AgResult;
4use crate::render::pipeline::CompiledTileStyle;
5use crate::warp::resampler::Resampling;
6use std::sync::{Arc, OnceLock};
7
8pub const ALLOWED_TILE_SIZES: [u32; 5] = [64, 128, 256, 512, 1024];
10
11#[derive(Debug, Clone)]
22pub struct TileStyle {
23 pub(crate) raster: Arc<RasterPlan>,
24 pub(crate) nodata: Option<f64>,
25 pub(crate) resampling: Resampling,
26 pub(crate) reproject: Resampling,
27 pub(crate) tilesize: u32,
28 pub(crate) format: TileFormat,
29}
30
31impl Default for TileStyle {
32 fn default() -> Self {
34 Self::from_json(r#"{"version":"3.0","renderer":{"type":"gray","bidx":[1]}}"#)
35 .expect("built-in Raster Style is supported")
36 }
37}
38
39impl TileStyle {
40 pub fn format(&self) -> TileFormat {
42 self.format
43 }
44 pub fn tilesize(&self) -> u32 {
46 self.tilesize
47 }
48 pub fn resampling(&self) -> Resampling {
50 self.resampling
51 }
52}
53
54pub fn compile(style: &TileStyle) -> AgResult<CompiledTileStyle> {
59 Ok(CompiledTileStyle {
60 raster: style.raster.clone(),
61 nodata: style.nodata,
62 resampling: style.resampling,
63 format: style.format,
64 })
65}
66
67pub async fn compile_cached(style: &TileStyle) -> AgResult<Arc<CompiledTileStyle>> {
69 static CACHE: OnceLock<moka::future::Cache<String, Arc<CompiledTileStyle>>> = OnceLock::new();
70 let cache = CACHE.get_or_init(|| moka::future::Cache::new(256));
71 let key = style.raster.key.clone();
72 if let Some(hit) = cache.get(&key).await {
73 return Ok(hit);
74 }
75 let compiled = Arc::new(compile(style)?);
76 cache.insert(key, compiled.clone()).await;
77 Ok(compiled)
78}