From 5b6ed09f6a64b01777b60e9da4e45eb3058a7a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 6 Sep 2023 08:35:12 +0200 Subject: [PATCH] Make raster index usize --- CHANGES.md | 4 ++++ src/raster/rasterband.rs | 17 ++++++++++++----- src/raster/rasterize.rs | 5 +++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 70c30100d..668610967 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +- **Breaking**: `Dataset::raster_count` now returns an `usize` and `Dataset::rasterband` now takes `usize` instead of `isize` + + - + - **Breaking**: `ExtendedDataType` no longer implements `Clone`, `PartialEq` and `Eq` - diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index 1cc89163f..f35fb6720 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -23,9 +23,16 @@ 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 { + /// Applies to raster datasets, and fetches the band at the given _1-based_ index. + /// + /// # Errors + /// Returns an error if the band cannot be read, including in the case the index is 0. + /// + /// # Panics + /// Panics if the band index is greater than `c_int::MAX`. + pub fn rasterband(&self, band_index: usize) -> Result { + assert!(band_index <= c_int::MAX as usize); + unsafe { let c_band = gdal_sys::GDALGetRasterBand(self.c_dataset(), band_index as c_int); if c_band.is_null() { @@ -69,8 +76,8 @@ impl Dataset { } /// Fetch the number of raster bands on this dataset. - pub fn raster_count(&self) -> isize { - (unsafe { gdal_sys::GDALGetRasterCount(self.c_dataset()) }) as isize + pub fn raster_count(&self) -> usize { + (unsafe { gdal_sys::GDALGetRasterCount(self.c_dataset()) }) as usize } /// Returns the raster dimensions: (width, height). diff --git a/src/raster/rasterize.rs b/src/raster/rasterize.rs index baf3d2b8d..bcd9ba3f2 100644 --- a/src/raster/rasterize.rs +++ b/src/raster/rasterize.rs @@ -161,7 +161,7 @@ mod tests { /// may be of any GDAL supported datatype. pub fn rasterize( dataset: &mut Dataset, - bands: &[isize], + bands: &[usize], geometries: &[Geometry], burn_values: &[f64], options: Option, @@ -178,8 +178,9 @@ pub fn rasterize( geometries.len() ))); } + let raster_count = dataset.raster_count(); for band in bands { - let is_good = *band > 0 && *band <= dataset.raster_count(); + let is_good = *band > 0 && *band <= raster_count; if !is_good { return Err(GdalError::BadArgument(format!( "Band index {} is out of bounds",