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::fill #528

Merged
merged 1 commit into from
Apr 19, 2024
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
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::fill`
- <https://github.com/georust/gdal/pull/528>

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

Expand Down
22 changes: 22 additions & 0 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,28 @@ impl<'a> RasterBand<'a> {
Ok(())
}
}

/// Fill this band with a constant value.
netthier marked this conversation as resolved.
Show resolved Hide resolved
///
/// If `imaginary_value` is `None`, the imaginary component will be set to 0.
///
/// # Notes
/// See also:
/// [`GDALFillRaster`](https://gdal.org/api/gdalrasterband_cpp.html#classGDALRasterBand_1a55bf20527df638dc48bf25e2ff26f353)
pub fn fill(&mut self, real_value: f64, imaginary_value: Option<f64>) -> Result<()> {
let rv = unsafe {
gdal_sys::GDALFillRaster(
self.c_rasterband,
real_value,
imaginary_value.unwrap_or(0.0),
)
};
if rv != CPLErr::CE_None {
return Err(_last_cpl_err(rv));
}
Ok(())
}

/// Returns the color interpretation of this band.
pub fn color_interpretation(&self) -> ColorInterpretation {
let interp_index = unsafe { gdal_sys::GDALGetRasterColorInterpretation(self.c_rasterband) };
Expand Down
12 changes: 12 additions & 0 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,18 @@ fn test_set_no_data_value() {
assert_eq!(rasterband.no_data_value(), None);
}

#[test]
fn test_fill() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 5, 5, 1).unwrap();
let mut rasterband = dataset.rasterband(1).unwrap();
assert!(rasterband.fill(5.4, None).is_ok());
let contents = rasterband
.read_as::<u8>((0, 0), (5, 5), (5, 5), None)
.unwrap();
assert!(contents.data().iter().all(|&v| v == 5));
}

#[test]
fn test_get_scale() {
let dataset = Dataset::open(fixture("offset_scaled_tinymarble.tif")).unwrap();
Expand Down
Loading