Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_scale and set_offset to Rasterband #294

Merged
merged 3 commits into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -75,6 +75,10 @@

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

- Add `Rasterband::set_scale` and `Rasterband::set_offset` methods

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

## 0.12

- Bump Rust edition to 2021
Expand Down
18 changes: 18 additions & 0 deletions src/raster/rasterband.rs
Expand Up @@ -472,6 +472,15 @@ impl<'a> RasterBand<'a> {
None
}

/// Set the scale for this band.
pub fn set_scale(&mut self, scale: f64) -> Result<()> {
let rv = unsafe { gdal_sys::GDALSetRasterScale(self.c_rasterband, scale) };
if rv != CPLErr::CE_None {
return Err(_last_cpl_err(rv));
}
Ok(())
}

/// Returns the offset of this band if set.
pub fn offset(&self) -> Option<f64> {
let mut pb_success = 1;
Expand All @@ -482,6 +491,15 @@ impl<'a> RasterBand<'a> {
None
}

/// Set the offset for this band.
pub fn set_offset(&mut self, offset: f64) -> Result<()> {
let rv = unsafe { gdal_sys::GDALSetRasterOffset(self.c_rasterband, offset) };
if rv != CPLErr::CE_None {
return Err(_last_cpl_err(rv));
}
Ok(())
}

/// Get actual block size (at the edges) when block size
/// does not divide band size.
#[cfg(any(all(major_is_2, minor_ge_2), major_ge_3))] // GDAL 2.2 .. 2.x or >= 3
Expand Down
20 changes: 20 additions & 0 deletions src/raster/tests.rs
Expand Up @@ -693,6 +693,26 @@ fn test_set_rasterband_color_interp() {
assert_eq!(band_interp, ColorInterpretation::AlphaBand);
}

#[test]
fn test_set_rasterband_scale() {
let driver = Driver::get_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let mut rasterband = dataset.rasterband(1).unwrap();
let scale = 1234.5678;
rasterband.set_scale(scale).unwrap();
assert_eq!(rasterband.scale().unwrap(), scale);
}

#[test]
fn test_set_rasterband_offset() {
let driver = Driver::get_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let mut rasterband = dataset.rasterband(1).unwrap();
let offset = -123.456;
rasterband.set_offset(offset).unwrap();
assert_eq!(rasterband.offset().unwrap(), offset);
}

#[test]
fn test_color_interp_names() {
assert_eq!(ColorInterpretation::AlphaBand.name(), "Alpha");
Expand Down