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 method to set default histogram for RasterBand #486

Merged
merged 2 commits into from
Dec 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@

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

- Added histogram calculation
- Added raster histogram methods (setter and getter)

- Getter: <https://github.com/georust/gdal/pull/468>
- Setter: <https://github.com/georust/gdal/pull/486>

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

## 0.16

Expand Down
34 changes: 33 additions & 1 deletion src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use gdal_sys::{
GDALCreateColorRamp, GDALCreateColorTable, GDALDestroyColorTable, GDALGetDefaultHistogramEx,
GDALGetPaletteInterpretation, GDALGetRasterHistogramEx, GDALGetRasterStatistics,
GDALMajorObjectH, GDALPaletteInterp, GDALRIOResampleAlg, GDALRWFlag, GDALRasterBandH,
GDALRasterIOExtraArg, GDALSetColorEntry, GDALSetRasterColorTable,
GDALRasterIOExtraArg, GDALSetColorEntry, GDALSetDefaultHistogramEx, GDALSetRasterColorTable,
};
use libc::c_int;
use std::ffi::CString;
Expand Down Expand Up @@ -884,6 +884,36 @@ impl<'a> RasterBand<'a> {
}
}

/// Set default raster histogram.
///
/// # Arguments
///
/// * `min` - Histogram lower bound
/// * `max` - Histogram upper bound
/// * `counts` - Histogram values for each bucket
///
/// # Panics
/// Panics if the `counts.len()` is greater than `i32::MAX`.
pub fn set_default_histogram(&self, min: f64, max: f64, counts: &mut [u64]) -> Result<()> {
let n_buckets = counts.len();
spadarian marked this conversation as resolved.
Show resolved Hide resolved
assert!(n_buckets <= i32::MAX as usize);

let rv = unsafe {
GDALSetDefaultHistogramEx(
self.c_rasterband,
min,
max,
n_buckets as i32,
counts.as_mut_ptr(),
)
};

match CplErrType::from(rv) {
CplErrType::None => Ok(()),
_ => Err(_last_cpl_err(rv)),
}
}

/// Compute raster histogram.
///
/// # Arguments
Expand All @@ -907,6 +937,8 @@ impl<'a> RasterBand<'a> {
));
}

assert!(n_buckets <= i32::MAX as usize);

let mut counts = vec![0; n_buckets];

let rv = unsafe {
Expand Down
34 changes: 33 additions & 1 deletion src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ fn test_raster_stats() {
}

#[test]
fn test_raster_histogram() {
fn test_raster_get_histogram() {
let fixture = TempFixture::fixture("tinymarble.tif");

let dataset = Dataset::open(&fixture).unwrap();
Expand Down Expand Up @@ -788,6 +788,38 @@ fn test_raster_histogram() {
hist.expect_err("histogram with 0 buckets should panic");
}

#[test]
fn test_raster_set_histogram() {
let fixture = TempFixture::fixture("tinymarble.tif");

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

let hist = rb.default_histogram(false).unwrap();
assert!(hist.is_none());

let mut expected = [
548, 104, 133, 127, 141, 125, 156, 129, 130, 117, 94, 94, 80, 81, 78, 63, 50, 66, 48, 48,
33, 38, 41, 35, 41, 39, 32, 40, 26, 27, 25, 24, 18, 25, 29, 27, 20, 34, 17, 24, 29, 11, 20,
21, 12, 19, 16, 16, 11, 10, 19, 5, 11, 10, 6, 9, 7, 12, 13, 6, 8, 7, 8, 14, 9, 14, 4, 8, 5,
12, 6, 10, 7, 9, 8, 6, 3, 7, 5, 8, 9, 5, 4, 8, 3, 9, 3, 6, 11, 7, 6, 3, 9, 9, 7, 6, 9, 10,
10, 4, 7, 2, 4, 7, 2, 12, 7, 10, 4, 6, 5, 2, 4, 5, 7, 3, 5, 7, 7, 14, 9, 12, 6, 6, 8, 5, 8,
3, 3, 5, 11, 4, 9, 7, 14, 7, 10, 11, 6, 6, 5, 4, 9, 6, 6, 9, 5, 12, 11, 9, 3, 8, 5, 6, 4,
2, 9, 7, 9, 9, 9, 6, 6, 8, 5, 9, 13, 4, 9, 4, 7, 13, 10, 5, 7, 8, 11, 12, 5, 17, 9, 11, 9,
8, 9, 5, 8, 9, 5, 6, 9, 11, 8, 7, 7, 6, 7, 8, 8, 8, 5, 6, 7, 5, 8, 5, 6, 8, 7, 4, 8, 6, 5,
11, 8, 8, 5, 4, 6, 4, 9, 7, 6, 6, 7, 7, 12, 6, 9, 17, 12, 20, 18, 17, 21, 24, 30, 29, 57,
72, 83, 21, 11, 9, 18, 7, 13, 10, 2, 4, 0, 1, 3, 4, 1, 1,
];

// This values come from test_raster_get_histogram
rb.set_default_histogram(-0.5, 255.5, &mut expected)
.unwrap();

let hist = rb.default_histogram(false).unwrap();
assert!(hist.is_some());
assert_eq!(hist.unwrap().counts(), expected);
}

#[test]
fn test_resample_str() {
assert!(ResampleAlg::from_str("foobar").is_err());
Expand Down