From de86d770f26723e07e6172f9e67c31ba9a7e1c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Thu, 22 Feb 2024 19:34:19 +0200 Subject: [PATCH] Add Dataset::rasterbands --- src/raster/rasterband.rs | 18 ++++++++++++++++++ src/raster/tests.rs | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index 693aefa9..43ccdaa5 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -42,6 +42,24 @@ impl Dataset { } } + /// Get the bands of a dataset. + /// + /// Returns an iterator over the [`RasterBands`]s in the current [`Dataset`]. + /// + /// # Example + /// + /// ```rust, no_run + /// use gdal::Dataset; + /// # fn main() -> gdal::errors::Result<()> { + /// let ds = Dataset::open("fixtures/tinymarble.tif")?; + /// assert_eq!(ds.rasterbands().count(), 3); + /// # Ok(()) + /// # } + /// ``` + pub fn rasterbands(&self) -> impl Iterator> { + (1..=self.raster_count()).map(|idx| self.rasterband(idx)) + } + /// Builds overviews for the current `Dataset`. See [`GDALBuildOverviews`]. /// /// # Arguments diff --git a/src/raster/tests.rs b/src/raster/tests.rs index a9313fbc..0f5898a4 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -64,6 +64,12 @@ fn test_read_raster() { assert_eq!(buf.data(), vec!(7, 7, 7, 10, 8, 12)); } +#[test] +fn test_read_rasterbands() { + let dataset = Dataset::open(fixture("tinymarble.tif")).unwrap(); + assert_eq!(dataset.rasterbands().count(), 3); +} + #[test] fn test_read_raster_with_default_resample() { let dataset = Dataset::open(fixture("tinymarble.tif")).unwrap();