Skip to content

Commit

Permalink
Separate mutable vs shared rasterband access.
Browse files Browse the repository at this point in the history
  • Loading branch information
metasim committed Oct 3, 2023
1 parent b9aa365 commit 1a86f38
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ mod tests {
#[test]
fn test_set_description() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let band = dataset.rasterband(1).unwrap();
let mut dataset = driver.create("", 1, 1, 1).unwrap();
let band = dataset.rasterband_mut(1).unwrap();

let description = "A merry and cheerful band description";
assert_eq!(band.description().unwrap(), "");
Expand Down
22 changes: 17 additions & 5 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ use crate::errors::*;
impl Dataset {
/// Fetch a band object for a dataset.
///
/// Applies to raster datasets, and fetches the
/// rasterband at the given _1-based_ index.
pub fn rasterband(&self, band_index: isize) -> Result<&mut RasterBand> {
/// Applies to raster datasets, and fetches the rasterband at the given _1-based_ index.
pub fn rasterband(&self, band_index: isize) -> Result<&RasterBand> {
unsafe {
let c_band = gdal_sys::GDALGetRasterBand(self.as_ptr(), band_index as c_int);
if c_band.is_null() {
return Err(_last_null_pointer_err("GDALGetRasterBand"));
}
Ok(RasterBand::from_ptr(c_band))
}
}

/// Fetch a mutable band object for a dataset.
///
/// Applies to raster datasets, and fetches the rasterband at the given _1-based_ index.
pub fn rasterband_mut(&mut self, band_index: isize) -> Result<&mut RasterBand> {
unsafe {
let c_band = gdal_sys::GDALGetRasterBand(self.as_ptr(), band_index as c_int);
if c_band.is_null() {
Expand Down Expand Up @@ -1097,8 +1109,8 @@ impl Debug for ColorEntry {
///
/// // Create in-memory copy to mutate
/// let mem_driver = DriverManager::get_driver_by_name("MEM")?;
/// let ds = ds.create_copy(&mem_driver, "<mem>", &[])?;
/// let band = ds.rasterband(1)?;
/// let mut ds = ds.create_copy(&mem_driver, "<mem>", &[])?;
/// let band = ds.rasterband_mut(1)?;
/// assert!(band.color_table().is_none());
///
/// // Create a new color table for 3 classes + no-data
Expand Down
30 changes: 15 additions & 15 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn test_read_raster_with_average_resample() {
#[test]
fn test_write_raster() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 20, 10, 1).unwrap();
let mut dataset = driver.create("", 20, 10, 1).unwrap();

// create a 2x1 raster
let raster = ByteBuffer {
Expand All @@ -107,7 +107,7 @@ fn test_write_raster() {
};

// epand it to fill the image (20x10)
let rb = dataset.rasterband(1).unwrap();
let rb = dataset.rasterband_mut(1).unwrap();

let res = rb.write((0, 0), (20, 10), &raster);

Expand Down Expand Up @@ -318,8 +318,8 @@ fn open_mask_band() {
#[test]
fn create_mask_band() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 20, 10, 1).unwrap();
let rb = dataset.rasterband(1).unwrap();
let mut dataset = driver.create("", 20, 10, 1).unwrap();
let rb = dataset.rasterband_mut(1).unwrap();
rb.create_mask_band(false).unwrap();

let mb = rb.open_mask_band().unwrap();
Expand Down Expand Up @@ -427,8 +427,8 @@ fn test_get_no_data_value() {
#[allow(clippy::float_cmp)]
fn test_set_no_data_value() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 20, 10, 1).unwrap();
let rasterband = dataset.rasterband(1).unwrap();
let mut dataset = driver.create("", 20, 10, 1).unwrap();
let rasterband = dataset.rasterband_mut(1).unwrap();
assert_eq!(rasterband.no_data_value(), None);
assert!(rasterband.set_no_data_value(Some(1.23)).is_ok());
assert_eq!(rasterband.no_data_value(), Some(1.23));
Expand Down Expand Up @@ -549,8 +549,8 @@ fn test_get_rasterband_color_interp() {
#[test]
fn test_set_rasterband_color_interp() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let rasterband = dataset.rasterband(1).unwrap();
let mut dataset = driver.create("", 1, 1, 1).unwrap();
let rasterband = dataset.rasterband_mut(1).unwrap();
rasterband
.set_color_interpretation(ColorInterpretation::AlphaBand)
.unwrap();
Expand All @@ -561,8 +561,8 @@ fn test_set_rasterband_color_interp() {
#[test]
fn test_set_rasterband_scale() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let rasterband = dataset.rasterband(1).unwrap();
let mut dataset = driver.create("", 1, 1, 1).unwrap();
let rasterband = dataset.rasterband_mut(1).unwrap();
let scale = 1234.5678;
rasterband.set_scale(scale).unwrap();
assert_eq!(rasterband.scale().unwrap(), scale);
Expand All @@ -571,8 +571,8 @@ fn test_set_rasterband_scale() {
#[test]
fn test_set_rasterband_offset() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
let dataset = driver.create("", 1, 1, 1).unwrap();
let rasterband = dataset.rasterband(1).unwrap();
let mut dataset = driver.create("", 1, 1, 1).unwrap();
let rasterband = dataset.rasterband_mut(1).unwrap();
let offset = -123.456;
rasterband.set_offset(offset).unwrap();
assert_eq!(rasterband.offset().unwrap(), offset);
Expand Down Expand Up @@ -680,11 +680,11 @@ fn test_create_color_table() {
assert!(band.color_table().is_none());

// Create a new file to put color table in
let dataset = dataset
let mut dataset = dataset
.create_copy(&dataset.driver(), &outfile, &[])
.unwrap();
dataset
.rasterband(1)
.rasterband_mut(1)
.unwrap()
.set_no_data_value(None)
.unwrap();
Expand All @@ -698,7 +698,7 @@ fn test_create_color_table() {
assert_eq!(ct.entry(2), Some(ColorEntry::rgba(255, 0, 0, 255)));
assert_eq!(ct.entry(8), None);

dataset.rasterband(1).unwrap().set_color_table(&ct);
dataset.rasterband_mut(1).unwrap().set_color_table(&ct);
}

// Reopen to confirm the changes.
Expand Down

0 comments on commit 1a86f38

Please sign in to comment.