Skip to content

Commit

Permalink
Merge #308
Browse files Browse the repository at this point in the history
308: Added `Rasterband::set_no_data` accepting `Option<f64>`. r=lnicola a=metasim

- [X] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

Closes #275 

Co-authored-by: Simeon H.K. Fitch <[email protected]>
  • Loading branch information
bors[bot] and metasim authored Sep 12, 2022
2 parents a3d1d22 + 2a5abc3 commit 8f88d40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

- <https://github.com/georust/gdal/pull/303>

- **Breaking** `RasterBand::set_no_data_value` takes `Option<f64>` instead of `f64` so that no _no-data_ can be set.
Also makes it symmetric with `RasterBand::no_data_value` which returns `Option<f64>`.

- <https://github.com/georust/gdal/pull/308>


## 0.13

- Add prebuild bindings for GDAL 3.5
Expand Down
16 changes: 12 additions & 4 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>) -> 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.
Expand Down
4 changes: 3 additions & 1 deletion src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 8f88d40

Please sign in to comment.