diff --git a/CHANGES.md b/CHANGES.md index eace9875..13b195c1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ ## Unreleased +- Added `Rasterband::set_statistics` + - + - Added `Rasterband::fill` - diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index e6e6591d..212ce8bb 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -1000,7 +1000,7 @@ impl<'a> RasterBand<'a> { /// If approximate statistics are sufficient, the `is_approx_ok` flag can be set to true in which case overviews, or a subset of image tiles may be used in computing the statistics. /// /// If `force` is `false` results will only be returned if it can be done quickly (i.e. without scanning the data). - /// If force` is `false` and results cannot be returned efficiently, the method will return `None`. + /// If `force` is `false` and results cannot be returned efficiently, the method will return `None`. /// /// Note that file formats using PAM (Persistent Auxiliary Metadata) services will generally cache statistics in the .pam file allowing fast fetch after the first request. /// @@ -1033,6 +1033,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. diff --git a/src/raster/tests.rs b/src/raster/tests.rs index 913c4701..1667762e 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -785,7 +785,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()); @@ -806,6 +806,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]