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 Rasterband::set_statistics #529

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 `Rasterband::fill`
- <https://github.com/georust/gdal/pull/528>

Expand Down
22 changes: 21 additions & 1 deletion src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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]
Expand Down
Loading