|
| 1 | +use crate::{ |
| 2 | + primitives::{BandSelection, BandSelectionIter}, |
| 3 | + raster::{GridBoundingBox2D, GridIdx2D, GridIdx2DIter, TileInformation, TileInformationIter}, |
| 4 | +}; |
| 5 | + |
| 6 | +#[derive(Clone, Debug)] |
| 7 | +pub struct TileIdxBandCrossProductIter { |
| 8 | + tile_iter: GridIdx2DIter, |
| 9 | + band_iter: BandSelectionIter, // TODO: maybe change this to actual attributes from ResultDescriptor not the Selection? |
| 10 | + current_tile: Option<GridIdx2D>, |
| 11 | +} |
| 12 | + |
| 13 | +impl TileIdxBandCrossProductIter { |
| 14 | + pub fn new(tile_iter: GridIdx2DIter, band_iter: BandSelectionIter) -> Self { |
| 15 | + let mut tile_iter = tile_iter; |
| 16 | + let current_tile = tile_iter.next(); |
| 17 | + Self { |
| 18 | + tile_iter, |
| 19 | + band_iter, |
| 20 | + current_tile, |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn grid_bounds(&self) -> GridBoundingBox2D { |
| 25 | + self.tile_iter.grid_bounds |
| 26 | + } |
| 27 | + |
| 28 | + pub fn band_selection(&self) -> &BandSelection { |
| 29 | + &self.band_iter.band_selection |
| 30 | + } |
| 31 | + |
| 32 | + pub fn with_grid_bounds_and_selection( |
| 33 | + bounds: GridBoundingBox2D, |
| 34 | + band_selection: BandSelection, |
| 35 | + ) -> Self { |
| 36 | + let tile_iter = GridIdx2DIter::new(&bounds); |
| 37 | + let band_iter = BandSelectionIter::new(band_selection); |
| 38 | + Self::new(tile_iter, band_iter) |
| 39 | + } |
| 40 | + |
| 41 | + pub fn reset(&mut self) { |
| 42 | + self.band_iter.reset(); |
| 43 | + self.tile_iter.reset(); |
| 44 | + self.current_tile = self.tile_iter.next(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Iterator for TileIdxBandCrossProductIter { |
| 49 | + type Item = (GridIdx2D, u32); |
| 50 | + |
| 51 | + fn next(&mut self) -> Option<Self::Item> { |
| 52 | + let current_t = self.current_tile; |
| 53 | + |
| 54 | + match (current_t, self.band_iter.next()) { |
| 55 | + (None, _) => None, |
| 56 | + (Some(t), Some(b)) => Some((t, b)), |
| 57 | + (Some(_t), None) => { |
| 58 | + self.band_iter.reset(); |
| 59 | + self.current_tile = self.tile_iter.next(); |
| 60 | + self.current_tile.map(|t| { |
| 61 | + ( |
| 62 | + t, |
| 63 | + self.band_iter |
| 64 | + .next() |
| 65 | + .expect("There must be at least one band"), |
| 66 | + ) |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Clone, Debug)] |
| 74 | +pub struct TileInformationBandCrossProductIter { |
| 75 | + tile_iter: TileInformationIter, |
| 76 | + band_iter: BandSelectionIter, |
| 77 | + current_tile: Option<TileInformation>, |
| 78 | +} |
| 79 | + |
| 80 | +impl TileInformationBandCrossProductIter { |
| 81 | + pub fn new(tile_iter: TileInformationIter, band_iter: BandSelectionIter) -> Self { |
| 82 | + let mut tile_iter = tile_iter; |
| 83 | + let current_tile = tile_iter.next(); |
| 84 | + Self { |
| 85 | + tile_iter, |
| 86 | + band_iter, |
| 87 | + current_tile, |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + pub fn reset(&mut self) { |
| 92 | + self.band_iter.reset(); |
| 93 | + self.tile_iter.reset(); |
| 94 | + self.current_tile = self.tile_iter.next(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl Iterator for TileInformationBandCrossProductIter { |
| 99 | + type Item = (TileInformation, u32); |
| 100 | + |
| 101 | + fn next(&mut self) -> Option<Self::Item> { |
| 102 | + let current_t = self.current_tile; |
| 103 | + |
| 104 | + match (current_t, self.band_iter.next()) { |
| 105 | + (None, _) => None, |
| 106 | + (Some(t), Some(b)) => Some((t, b)), |
| 107 | + (Some(_t), None) => { |
| 108 | + self.band_iter.reset(); |
| 109 | + self.current_tile = self.tile_iter.next(); |
| 110 | + self.current_tile.map(|t| { |
| 111 | + ( |
| 112 | + t, |
| 113 | + self.band_iter |
| 114 | + .next() |
| 115 | + .expect("There must be at least one band"), |
| 116 | + ) |
| 117 | + }) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments