From 402efe871cf08366832819f22584da86e6e45813 Mon Sep 17 00:00:00 2001 From: netthier Date: Thu, 18 Apr 2024 17:37:13 +0200 Subject: [PATCH] Add `Rasterband::fill` Signed-off-by: netthier --- CHANGES.md | 3 +++ src/raster/rasterband.rs | 22 ++++++++++++++++++++++ src/raster/tests.rs | 12 ++++++++++++ 3 files changed, 37 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index ad974f38..eace9875 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ ## Unreleased +- Added `Rasterband::fill` + - + - Added `Dataset::rasterbands`. - diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index 43ccdaa5..e6e6591d 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -815,6 +815,28 @@ impl<'a> RasterBand<'a> { Ok(()) } } + + /// Fill this band with a constant value. + /// + /// If `imaginary_value` is `None`, the imaginary component will be set to 0. + /// + /// # Notes + /// See also: + /// [`GDALFillRaster`](https://gdal.org/api/gdalrasterband_cpp.html#classGDALRasterBand_1a55bf20527df638dc48bf25e2ff26f353) + pub fn fill(&mut self, real_value: f64, imaginary_value: Option) -> Result<()> { + let rv = unsafe { + gdal_sys::GDALFillRaster( + self.c_rasterband, + real_value, + imaginary_value.unwrap_or(0.0), + ) + }; + if rv != CPLErr::CE_None { + return Err(_last_cpl_err(rv)); + } + Ok(()) + } + /// Returns the color interpretation of this band. pub fn color_interpretation(&self) -> ColorInterpretation { let interp_index = unsafe { gdal_sys::GDALGetRasterColorInterpretation(self.c_rasterband) }; diff --git a/src/raster/tests.rs b/src/raster/tests.rs index 0f5898a4..913c4701 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -483,6 +483,18 @@ fn test_set_no_data_value() { assert_eq!(rasterband.no_data_value(), None); } +#[test] +fn test_fill() { + let driver = DriverManager::get_driver_by_name("MEM").unwrap(); + let dataset = driver.create("", 5, 5, 1).unwrap(); + let mut rasterband = dataset.rasterband(1).unwrap(); + assert!(rasterband.fill(5.4, None).is_ok()); + let contents = rasterband + .read_as::((0, 0), (5, 5), (5, 5), None) + .unwrap(); + assert!(contents.data().iter().all(|&v| v == 5)); +} + #[test] fn test_get_scale() { let dataset = Dataset::open(fixture("offset_scaled_tinymarble.tif")).unwrap();