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::write_block #490

Merged
merged 1 commit into from
Dec 11, 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- Added `RasterBand::write_block`.

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

- `RasterBand::read_block` now checks that the requested type matches the band type.

- <https://github.com/georust/gdal/pull/489>
Expand Down
107 changes: 106 additions & 1 deletion src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,30 @@ impl<'a> RasterBand<'a> {
/// * `block_index` - the block index
///
/// # Notes
/// The Matrix shape is (rows, cols) and raster shape is (cols in x-axis, rows in y-axis).
/// Blocks indexes start from 0 and are of form (x, y), where x grows in the horizontal direction.
///
/// The matrix shape is (rows, cols) and raster shape is (cols in x-axis, rows in y-axis).
///
/// The block size of the band can be determined using [`RasterBand::block_size`].
/// The last blocks in both directions can be smaller.
/// [`RasterBand::actual_block_size`] will report the correct dimensions of a block.
///
/// # Errors
/// If the block index is not valid, GDAL will return an error.
///
/// # Example
///
/// ```rust, no_run
/// # fn main() -> gdal::errors::Result<()> {
/// use gdal::Dataset;
///
/// let dataset = Dataset::open("fixtures/m_3607824_se_17_1_20160620_sub.tif")?;
/// let band1 = dataset.rasterband(1)?;
/// let arr = band1.read_block::<u8>((0, 0))?;
/// assert_eq!(arr.shape(), &[300, 6]);
/// # Ok(())
/// # }
/// ```
pub fn read_block<T: Copy + GdalType>(&self, block_index: (usize, usize)) -> Result<Array2<T>> {
if T::gdal_ordinal() != self.band_type() as u32 {
return Err(GdalError::BadArgument(
Expand Down Expand Up @@ -562,6 +585,88 @@ impl<'a> RasterBand<'a> {
Array2::from_shape_vec((size.1, size.0), data).map_err(Into::into)
}

#[cfg(feature = "ndarray")]
#[cfg_attr(docsrs, doc(cfg(feature = "array")))]
/// Write a [`Array2<T>`] from a [`Dataset`] block, where `T` implements [`GdalType`].
///
/// # Arguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a reference to the method for getting the block layout (so one can know how many blocks there are in each dimension)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example would be helpful here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an example and a note to block_size and actual_block_size.

/// * `block_index` - the block index
///
/// # Notes
/// Blocks indexes start from 0 and are of form (x, y), where x grows in the horizontal direction.
///
/// The matrix shape is (rows, cols) and raster shape is (cols in x-axis, rows in y-axis).
///
/// The block size of the band can be determined using [`RasterBand::block_size`].
/// The last blocks in both directions can be smaller.
/// [`RasterBand::actual_block_size`] will report the correct dimensions of a block.
///
/// # Errors
/// If the block index is not valid, GDAL will return an error.
///
/// # Example
///
/// ```rust, no_run
/// # fn main() -> gdal::errors::Result<()> {
/// use gdal::DriverManager;
/// use gdal::raster::RasterCreationOption;
/// use ndarray::Array2;
///
/// let driver = DriverManager::get_driver_by_name("GTiff").unwrap();
/// let options = [
/// RasterCreationOption {
/// key: "TILED",
/// value: "YES",
/// },
/// RasterCreationOption {
/// key: "BLOCKXSIZE",
/// value: "16",
/// },
/// RasterCreationOption {
/// key: "BLOCKYSIZE",
/// value: "16",
/// },
/// ];
/// let dataset = driver
/// .create_with_band_type_with_options::<u16, _>(
/// "/vsimem/test_write_block.tif",
/// 32,
/// 32,
/// 1,
/// &options,
/// )?;
/// let mut band1 = dataset.rasterband(1)?;
/// let arr = Array2::from_shape_fn((16, 16), |(y, x)| y as u16 * 16 + x as u16);
/// band1.write_block((0, 0), arr)?;
/// # Ok(())
/// # }
/// ```
pub fn write_block<T: Copy + GdalType>(
&mut self,
block_index: (usize, usize),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the block index is out of bounds? Do we get an error, or panic (or worse)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no way it can panic, of course :-). Added a note in the docs.

block: Array2<T>,
) -> Result<()> {
if T::gdal_ordinal() != self.band_type() as u32 {
return Err(GdalError::BadArgument(
"array type must match band data type".to_string(),
));
}

let mut data = block.into_raw_vec();
let rv = unsafe {
gdal_sys::GDALWriteBlock(
self.c_rasterband,
block_index.0 as c_int,
block_index.1 as c_int,
data.as_mut_ptr() as GDALRasterBandH,
)
};
if rv != CPLErr::CE_None {
return Err(_last_cpl_err(rv));
}
Ok(())
}

/// Write a [`Buffer<T>`] into a [`Dataset`].
///
/// # Arguments
Expand Down
51 changes: 50 additions & 1 deletion src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::path::Path;
use std::str::FromStr;

#[cfg(feature = "ndarray")]
use ndarray::arr2;
use ndarray::{arr2, Array2, Axis};

#[test]
fn test_open() {
Expand Down Expand Up @@ -393,6 +393,55 @@ fn test_read_block_data() {
assert_eq!(array[[0, 99]], 51);
}

#[test]
#[cfg(feature = "ndarray")]
fn test_write_block() {
let driver = DriverManager::get_driver_by_name("GTiff").unwrap();
let options = [
RasterCreationOption {
key: "TILED",
value: "YES",
},
RasterCreationOption {
key: "BLOCKXSIZE",
value: "16",
},
RasterCreationOption {
key: "BLOCKYSIZE",
value: "16",
},
];
let dataset = driver
.create_with_band_type_with_options::<u16, _>(
"/vsimem/test_write_block.tif",
32,
32,
1,
&options,
)
.unwrap();

let mut block_11 = Array2::from_shape_fn((16, 16), |(y, x)| y as u16 * 16 + x as u16 + 1000u16);
let mut block_12 = Array2::from_shape_fn((16, 16), |(y, x)| y as u16 * 16 + x as u16 + 3000u16);
let block_21 = Array2::from_shape_fn((16, 16), |(y, x)| y as u16 * 16 + x as u16 + 2000u16);
let block_22 = Array2::from_shape_fn((16, 16), |(y, x)| y as u16 * 16 + x as u16 + 4000u16);

let mut band = dataset.rasterband(1).unwrap();
band.write_block((0, 0), block_11.clone()).unwrap();
band.write_block((0, 1), block_12.clone()).unwrap();
block_11.append(Axis(1), block_21.view()).unwrap();
band.write_block((1, 0), block_21).unwrap();
block_12.append(Axis(1), block_22.view()).unwrap();
band.write_block((1, 1), block_22).unwrap();
block_11.append(Axis(0), block_12.view()).unwrap();

let buf = band
.read_as::<u16>((0, 0), (32, 32), (32, 32), None)
.unwrap();
let arr = ndarray::Array2::from_shape_vec((32, 32), buf.data).unwrap();
assert_eq!(arr, block_11);
}

#[test]
fn test_get_band_type() {
let driver = DriverManager::get_driver_by_name("MEM").unwrap();
Expand Down