diff --git a/CHANGES.md b/CHANGES.md index 6b45c58a..8dacd32c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,12 @@ - +- **Breaking** `RasterBand::set_no_data_value` takes `Option` instead of `f64` so that no _no-data_ can be set. + Also makes it symmetric with `RasterBand::no_data_value` which returns `Option`. + + - + + ## 0.13 - Add prebuild bindings for GDAL 3.5 diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index 80c02a37..6cd7ce46 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -431,12 +431,20 @@ impl<'a> RasterBand<'a> { } /// Set the no data value of this band. - pub fn set_no_data_value(&mut self, no_data: f64) -> Result<()> { - let rv = unsafe { gdal_sys::GDALSetRasterNoDataValue(self.c_rasterband, no_data) }; + /// + /// If `no_data` is `None`, any existing no-data value is deleted. + pub fn set_no_data_value(&mut self, no_data: Option) -> Result<()> { + let rv = if let Some(no_data) = no_data { + unsafe { gdal_sys::GDALSetRasterNoDataValue(self.c_rasterband, no_data) } + } else { + unsafe { gdal_sys::GDALDeleteRasterNoDataValue(self.c_rasterband) } + }; + if rv != CPLErr::CE_None { - return Err(_last_cpl_err(rv)); + Err(_last_cpl_err(rv)) + } else { + Ok(()) } - Ok(()) } /// Returns the color interpretation of this band. diff --git a/src/raster/tests.rs b/src/raster/tests.rs index 9f99c17f..ba67a2a4 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -557,8 +557,10 @@ fn test_set_no_data_value() { let dataset = driver.create("", 20, 10, 1).unwrap(); let mut rasterband = dataset.rasterband(1).unwrap(); assert_eq!(rasterband.no_data_value(), None); - assert!(rasterband.set_no_data_value(1.23).is_ok()); + assert!(rasterband.set_no_data_value(Some(1.23)).is_ok()); assert_eq!(rasterband.no_data_value(), Some(1.23)); + assert!(rasterband.set_no_data_value(None).is_ok()); + assert_eq!(rasterband.no_data_value(), None); } #[test]