Skip to content

Commit

Permalink
Add Rasterband::set_statistics
Browse files Browse the repository at this point in the history
Signed-off-by: netthier <lp@senselabs.de>
  • Loading branch information
netthier committed Apr 19, 2024
1 parent 88d3124 commit 6c8b4ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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::set_statistics`
- <https://github.com/georust/gdal/pull/529>

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

Expand Down
20 changes: 20 additions & 0 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,26 @@ impl<'a> RasterBand<'a> {
}
}

/// Set statistics on a band
///
/// This method can be used to store min/max/mean/standard deviation statistics on a raster band.
///
/// The default implementation stores them as metadata, and will only work on formats that can save arbitrary metadata.
/// This method cannot detect whether metadata will be properly saved and so may return `Ok(())` even if the statistics will never be saved.
///
/// # Notes
/// See also:
/// [`GDALSetRasterStatistics`](https://gdal.org/api/gdalrasterband_cpp.html#_CPPv4N14GDALRasterBand13SetStatisticsEdddd)
pub fn set_statistics(&mut self, min: f64, max: f64, mean: f64, std_dev: f64) -> Result<()> {
let rv = unsafe {
gdal_sys::GDALSetRasterStatistics(self.c_rasterband, min, max, mean, std_dev)
};
if rv != CPLErr::CE_None {
return Err(_last_cpl_err(rv));
}
Ok(())
}

/// Compute the min/max values for a band.
///
/// If `is_approx_ok` is `true`, then the band’s GetMinimum()/GetMaximum() will be trusted.
Expand Down
14 changes: 13 additions & 1 deletion src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ fn test_raster_stats() {
let fixture = TempFixture::fixture("tinymarble.tif");

let dataset = Dataset::open(&fixture).unwrap();
let rb = dataset.rasterband(1).unwrap();
let mut rb = dataset.rasterband(1).unwrap();

assert!(rb.get_statistics(false, false).unwrap().is_none());

Expand All @@ -794,6 +794,18 @@ fn test_raster_stats() {
max: 255.0,
}
);

assert!(rb.set_statistics(1.0, 2.0, 1.5, 10.0).is_ok());

assert_eq!(
rb.get_statistics(true, false).unwrap().unwrap(),
StatisticsAll {
min: 1.0,
max: 2.0,
mean: 1.5,
std_dev: 10.0,
}
);
}

#[test]
Expand Down

0 comments on commit 6c8b4ee

Please sign in to comment.