Skip to content

Commit

Permalink
Add Rasterband::fill
Browse files Browse the repository at this point in the history
Signed-off-by: netthier <lp@senselabs.de>
  • Loading branch information
netthier authored and lnicola committed Apr 19, 2024
1 parent 54fbdfd commit 26f2544
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Added `Rasterband::fill`
- <https://github.com/georust/gdal/pull/528>

- Added `Dataset::rasterbands`.
- <https://github.com/georust/gdal/pull/523>

Expand Down
22 changes: 22 additions & 0 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>) -> 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) };
Expand Down
12 changes: 12 additions & 0 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u8>((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();
Expand Down

0 comments on commit 26f2544

Please sign in to comment.