Skip to content

Commit 132d031

Browse files
committed
Add Rasterband::set_statistics
Signed-off-by: netthier <[email protected]>
1 parent 26f2544 commit 132d031

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Added `Rasterband::set_statistics`
6+
- <https://github.com/georust/gdal/pull/529>
7+
58
- Added `Rasterband::fill`
69
- <https://github.com/georust/gdal/pull/528>
710

src/raster/rasterband.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,26 @@ impl<'a> RasterBand<'a> {
10331033
}
10341034
}
10351035

1036+
/// Set statistics on a band
1037+
///
1038+
/// This method can be used to store min/max/mean/standard deviation statistics on a raster band.
1039+
///
1040+
/// The default implementation stores them as metadata, and will only work on formats that can save arbitrary metadata.
1041+
/// This method cannot detect whether metadata will be properly saved and so may return `Ok(())` even if the statistics will never be saved.
1042+
///
1043+
/// # Notes
1044+
/// See also:
1045+
/// [`GDALSetRasterStatistics`](https://gdal.org/api/gdalrasterband_cpp.html#_CPPv4N14GDALRasterBand13SetStatisticsEdddd)
1046+
pub fn set_statistics(&mut self, min: f64, max: f64, mean: f64, std_dev: f64) -> Result<()> {
1047+
let rv = unsafe {
1048+
gdal_sys::GDALSetRasterStatistics(self.c_rasterband, min, max, mean, std_dev)
1049+
};
1050+
if rv != CPLErr::CE_None {
1051+
return Err(_last_cpl_err(rv));
1052+
}
1053+
Ok(())
1054+
}
1055+
10361056
/// Compute the min/max values for a band.
10371057
///
10381058
/// If `is_approx_ok` is `true`, then the band’s GetMinimum()/GetMaximum() will be trusted.

src/raster/tests.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ fn test_raster_stats() {
785785
let fixture = TempFixture::fixture("tinymarble.tif");
786786

787787
let dataset = Dataset::open(&fixture).unwrap();
788-
let rb = dataset.rasterband(1).unwrap();
788+
let mut rb = dataset.rasterband(1).unwrap();
789789

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

@@ -806,6 +806,18 @@ fn test_raster_stats() {
806806
max: 255.0,
807807
}
808808
);
809+
810+
assert!(rb.set_statistics(1.0, 2.0, 1.5, 10.0).is_ok());
811+
812+
assert_eq!(
813+
rb.get_statistics(true, false).unwrap().unwrap(),
814+
StatisticsAll {
815+
min: 1.0,
816+
max: 2.0,
817+
mean: 1.5,
818+
std_dev: 10.0,
819+
}
820+
);
809821
}
810822

811823
#[test]

0 commit comments

Comments
 (0)