gistools/writers/tile/
mod.rs

1/// File Based Tile Writer
2#[cfg(feature = "std")]
3pub mod file;
4
5use crate::util::{CompressionFormat, Date};
6use alloc::{collections::BTreeMap, vec::Vec};
7#[cfg(feature = "std")]
8pub use file::*;
9use s2_tilejson::Metadata;
10use s2json::Face;
11
12/// A base interface for all tile stores.
13pub trait TileWriter {
14    /// Write a Web Mercator tile to the folder location given its (zoom, x, y) coordinates.
15    fn write_tile_wm(&mut self, zoom: u8, x: u32, y: u32, data: Vec<u8>);
16    /// Write a S2 tile to the folder location given its (face, zoom, x, y) coordinates.
17    fn write_tile_s2(&mut self, face: Face, zoom: u8, x: u32, y: u32, data: Vec<u8>);
18    /// Write the metadata to the folder location.
19    fn commit(&mut self, metadata: Metadata, tile_compression: Option<CompressionFormat>);
20}
21
22/// A base interface for all tile stores.
23pub trait TemporalTileWriter {
24    /// Write a time series tile to the folder location given its (t, z, x, y) coordinates.
25    fn write_temporal_tile_wm(&mut self, time: Date, zoom: u8, x: u32, y: u32, data: Vec<u8>);
26    /// Write a time series tile to the folder location given its (t, face, zoom, x, y) coordinates.
27    fn write_temporal_tile_s2(
28        &mut self,
29        time: Date,
30        face: Face,
31        zoom: u8,
32        x: u32,
33        y: u32,
34        data: Vec<u8>,
35    );
36}
37
38/// Key store for Web Mercator tiles
39#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Clone, Default)]
40pub struct WMTileKey {
41    /// The zoom level
42    pub zoom: u8,
43    /// The tile X coordinate
44    pub x: u32,
45    /// The tile Y coordinate
46    pub y: u32,
47}
48
49/// Key store for S2 tiles
50#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Clone, Default)]
51pub struct S2TileKey {
52    /// The Open S2 projection face
53    pub face: Face,
54    /// The zoom level
55    pub zoom: u8,
56    /// The tile X coordinate
57    pub x: u32,
58    /// The tile Y coordinate
59    pub y: u32,
60}
61
62/// A Temporal key store for Web Mercator
63#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Clone)]
64pub struct WMTemporalTileKey {
65    /// The date of the data
66    pub time: Date,
67    /// The zoom level
68    pub zoom: u8,
69    /// The tile X coordinate
70    pub x: u32,
71    /// The tile Y coordinate
72    pub y: u32,
73}
74
75/// A Temporal key store for S2
76#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Clone)]
77pub struct S2TemporalTileKey {
78    /// The date of the data
79    pub time: Date,
80    /// The Open S2 projection face
81    pub face: Face,
82    /// The zoom level
83    pub zoom: u8,
84    /// The tile X coordinate
85    pub x: u32,
86    /// The tile Y coordinate
87    pub y: u32,
88}
89
90/// A key store for Web Mercator and S2 tiles
91#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Clone)]
92pub enum TileKey {
93    /// A key store for Web Mercator tiles
94    WM(WMTileKey),
95    /// A key store for S2 tiles
96    S2(S2TileKey),
97    /// A key store for Web Mercator time series tiles
98    WMTime(WMTemporalTileKey),
99    /// A key store for S2 time series tiles
100    S2Time(S2TemporalTileKey),
101}
102
103/// A Local Memory Tile Write Store
104///
105/// Useful for testing
106#[derive(Default, Debug, Clone)]
107pub struct LocalTileWriter {
108    /// The metadata
109    pub metadata: Option<Metadata>,
110    /// The tiles
111    pub tiles: BTreeMap<TileKey, Vec<u8>>,
112}
113impl LocalTileWriter {
114    /// Create a new Local Memory Tile Write Store
115    pub fn new() -> LocalTileWriter {
116        LocalTileWriter { metadata: None, tiles: BTreeMap::new() }
117    }
118
119    /// Grab the metadata
120    pub fn metadata(&self) -> Option<Metadata> {
121        self.metadata.clone()
122    }
123
124    /// Grab a WM tile
125    pub fn get_tile_wm(&self, zoom: u8, x: u32, y: u32) -> Option<Vec<u8>> {
126        let key = WMTileKey { zoom, x, y };
127        self.tiles.get(&TileKey::WM(key)).cloned()
128    }
129
130    /// Grab an S2 tile
131    pub fn get_tile_s2(&self, face: Face, zoom: u8, x: u32, y: u32) -> Option<Vec<u8>> {
132        let key = S2TileKey { face, zoom, x, y };
133        self.tiles.get(&TileKey::S2(key)).cloned()
134    }
135}
136impl TileWriter for LocalTileWriter {
137    fn write_tile_wm(&mut self, zoom: u8, x: u32, y: u32, data: Vec<u8>) {
138        let key = WMTileKey { zoom, x, y };
139        self.tiles.insert(TileKey::WM(key), data);
140    }
141    fn write_tile_s2(&mut self, face: Face, zoom: u8, x: u32, y: u32, data: Vec<u8>) {
142        let key = S2TileKey { face, zoom, x, y };
143        self.tiles.insert(TileKey::S2(key), data);
144    }
145    fn commit(&mut self, metadata: Metadata, _tile_compression: Option<CompressionFormat>) {
146        self.metadata = Some(metadata);
147    }
148}
149impl TemporalTileWriter for LocalTileWriter {
150    fn write_temporal_tile_wm(&mut self, time: Date, zoom: u8, x: u32, y: u32, data: Vec<u8>) {
151        let key = WMTemporalTileKey { time, zoom, x, y };
152        self.tiles.insert(TileKey::WMTime(key), data);
153    }
154    fn write_temporal_tile_s2(
155        &mut self,
156        time: Date,
157        face: Face,
158        zoom: u8,
159        x: u32,
160        y: u32,
161        data: Vec<u8>,
162    ) {
163        let key = S2TemporalTileKey { time, face, zoom, x, y };
164        self.tiles.insert(TileKey::S2Time(key), data);
165    }
166}