maplibre/raster/
process_raster.rs

1use std::marker::PhantomData;
2
3use image::RgbaImage;
4use thiserror::Error;
5
6use crate::{
7    coords::WorldTileCoords,
8    io::apc::Context,
9    raster::transferables::{LayerRaster, RasterTransferables},
10};
11
12#[derive(Error, Debug)]
13pub enum ProcessRasterError {
14    /// Error during processing of the pipeline
15    #[error("processing data in pipeline failed")]
16    Processing(Box<dyn std::error::Error>),
17}
18
19pub struct RasterTileRequest {
20    pub coords: WorldTileCoords,
21}
22
23pub fn process_raster_tile<T: RasterTransferables, C: Context>(
24    data: &[u8],
25    tile_request: RasterTileRequest,
26    context: &mut ProcessRasterContext<T, C>,
27) -> Result<(), ProcessRasterError> {
28    let coords = &tile_request.coords;
29    let img = image::load_from_memory(data).unwrap();
30    let rgba = img.to_rgba8();
31
32    context.layer_raster_finished(coords, "raster".to_string(), rgba)?;
33
34    Ok(())
35}
36pub struct ProcessRasterContext<T: RasterTransferables, C: Context> {
37    context: C,
38    phantom_t: PhantomData<T>,
39}
40
41impl<T: RasterTransferables, C: Context> ProcessRasterContext<T, C> {
42    pub fn new(context: C) -> Self {
43        Self {
44            context,
45            phantom_t: Default::default(),
46        }
47    }
48}
49
50impl<T: RasterTransferables, C: Context> ProcessRasterContext<T, C> {
51    fn layer_raster_finished(
52        &mut self,
53        coords: &WorldTileCoords,
54        layer_name: String,
55        image_data: RgbaImage,
56    ) -> Result<(), ProcessRasterError> {
57        self.context
58            .send_back(T::LayerRaster::build_from(*coords, layer_name, image_data))
59            .map_err(|e| ProcessRasterError::Processing(Box::new(e)))
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::process_raster_tile;
66    use crate::{
67        coords::ZoomLevel,
68        io::apc::tests::DummyContext,
69        raster::{
70            process_raster::{ProcessRasterContext, RasterTileRequest},
71            DefaultRasterTransferables,
72        },
73    };
74
75    #[test] // TODO: Add proper tile byte array
76    #[ignore]
77    fn test() {
78        let _output = process_raster_tile(
79            &[0],
80            RasterTileRequest {
81                coords: (0, 0, ZoomLevel::default()).into(),
82            },
83            &mut ProcessRasterContext::<DefaultRasterTransferables, _>::new(DummyContext),
84        );
85    }
86}