# tile_pipelines.rs - source
Source of the Rust file `maplibre/src/io/tile_pipelines.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156

    use crate::io::geometry_index::IndexProcessor;
    use crate::io::pipeline::{DataPipeline, PipelineContext, PipelineEnd, Processable};
    use crate::io::{TileRequest, TileRequestID};
    use crate::tessellation::zero_tessellator::ZeroTessellator;
    use crate::tessellation::IndexDataType;
    use geozero::GeozeroDatasource;
    use prost::Message;
    use std::collections::HashSet;

    pub struct ParseTile;

    impl Processable for ParseTile {
        type Input = (TileRequest, TileRequestID, Box<[u8]>);
        type Output = (TileRequest, TileRequestID, geozero::mvt::Tile);

        // TODO (perf): Maybe force inline
        fn process(
            &self,
            (tile_request, request_id, data): Self::Input,
            _context: &mut PipelineContext,
        ) -> Self::Output {
            let tile = geozero::mvt::Tile::decode(data.as_ref()).expect("failed to load tile");
            (tile_request, request_id, tile)
        }
    }

    pub struct IndexLayer;

    impl Processable for IndexLayer {
        type Input = (TileRequest, TileRequestID, geozero::mvt::Tile);
        type Output = (TileRequest, TileRequestID, geozero::mvt::Tile);

        // TODO (perf): Maybe force inline
        fn process(
            &self,
            (tile_request, request_id, tile): Self::Input,
            context: &mut PipelineContext,
        ) -> Self::Output {
            let index = IndexProcessor::new();

            context
                .processor_mut()
                .layer_indexing_finished(&tile_request.coords, index.get_geometries());
            (tile_request, request_id, tile)
        }
    }

    pub struct TessellateLayer;

    impl Processable for TessellateLayer {
        type Input = (TileRequest, TileRequestID, geozero::mvt::Tile);
        type Output = (TileRequest, TileRequestID, geozero::mvt::Tile);

        // TODO (perf): Maybe force inline
        fn process(
            &self,
            (tile_request, request_id, mut tile): Self::Input,
            context: &mut PipelineContext,
        ) -> Self::Output {
            let coords = &tile_request.coords;

            for layer in &mut tile.layers {
                let cloned_layer = layer.clone();
                let layer_name: &str = &cloned_layer.name;
                if !tile_request.layers.contains(layer_name) {
                    continue;
                }

                tracing::info!("layer {} at {} ready", layer_name, coords);

                let mut tessellator = ZeroTessellator::<IndexDataType>::default();
                if let Err(e) = layer.process(&mut tessellator) {
                    context
                        .processor_mut()
                        .layer_unavailable(coords, layer_name);

                    tracing::error!(
                        "layer {} at {} tesselation failed {:?}",
                        layer_name,
                        &coords,
                        e
                    );
                } else {
                    context.processor_mut().layer_tesselation_finished(
                        coords,
                        tessellator.buffer.into(),
                        tessellator.feature_indices,
                        cloned_layer,
                    )
                }
            }

            let available_layers: HashSet<_> = tile
                .layers
                .iter()
                .map(|layer| layer.name.clone())
                .collect::<HashSet<_>>();

            for missing_layer in tile_request.layers.difference(&available_layers) {
                context
                    .processor_mut()
                    .layer_unavailable(coords, missing_layer);

                tracing::info!(
                    "requested layer {} at {} not found in tile",
                    missing_layer,
                    &coords
                );
            }

            tracing::info!("tile tessellated at {} finished", &tile_request.coords);

            context
                .processor_mut()
                .tile_finished(request_id, &tile_request.coords);

            (tile_request, request_id, tile)
        }
    }

    pub fn build_vector_tile_pipeline() -> impl Processable<Input = <ParseTile as Processable>::Input> {
        DataPipeline::new(
            ParseTile,
            DataPipeline::new(TessellateLayer, PipelineEnd::default()),
        )
    }

    #[cfg(test)]
    mod tests {
        use super::build_vector_tile_pipeline;
        use crate::coords::ZoomLevel;
        use crate::io::pipeline::{PipelineContext, PipelineProcessor, Processable};
        use crate::io::TileRequest;
        pub struct DummyPipelineProcessor;

        impl PipelineProcessor for DummyPipelineProcessor {}

        #[test] // TODO: Add proper tile byte array
        #[ignore]
        fn test() {
            let mut context = PipelineContext::new(DummyPipelineProcessor);

            let pipeline = build_vector_tile_pipeline();
            let output = pipeline.process(
                (
                    TileRequest {
                        coords: (0, 0, ZoomLevel::default()).into(),
                        layers: Default::default(),
                    },
                    0,
                    Box::new([0]),
                ),
                &mut context,
            );
        }
    }