Skip to content

Commit

Permalink
Added bindings for GDALSetRasterNoDataValueAsInt64 and `GDALSetRast…
Browse files Browse the repository at this point in the history
…erNoDataValueAsUInt64`
  • Loading branch information
metasim committed Feb 1, 2024
1 parent 29c1615 commit 401f4a3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changes

## Unreleased
- Added bindings for `GetNoDataValueAsInt64` and `GetNoDataValueAsUInt64`
- Added `no_data_value_u64`, `set_no_data_value_u64`, `no_data_value_i64` and `set_no_data_value_i64` to `RasterBand`.
- <https://github.com/georust/gdal/pull/520>

- Add `DriverIterator` format to iterate through drivers, as well as `DriverManager::all()` method that provides the iterator.
Expand Down
Binary file removed fixtures/labels_i64.tif
Binary file not shown.
Binary file removed fixtures/labels_u64.tif
Binary file not shown.
69 changes: 55 additions & 14 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,13 +696,26 @@ impl<'a> RasterBand<'a> {
None
}

/// Fetch the no-data value for this band.
/// Sets the no-data value of this band.
///
/// This method should ONLY be called on rasters whose data type is `UInt64`.
/// If `no_data` is `None`, any existing no-data value is deleted.
pub fn set_no_data_value(&mut self, no_data: Option<f64>) -> Result<()> {
let rv = if let Some(no_data) = no_data {
unsafe { gdal_sys::GDALSetRasterNoDataValue(self.c_rasterband, no_data) }
} else {
unsafe { gdal_sys::GDALDeleteRasterNoDataValue(self.c_rasterband) }
};

if rv != CPLErr::CE_None {
Err(_last_cpl_err(rv))
} else {
Ok(())
}
}

/// Fetch the no-data value for this band.
///
/// If there is no no-data value, an out of range value will generally be returned.
/// The no-data value for a band is generally a special marker value used to mark pixels that are not valid data.
/// Such pixels should generally not be displayed, nor contribute to analysis operations.
/// This method should ONLY be called on bands whose data type is `UInt64`.
///
/// The no data value returned is 'raw', meaning that it has no offset and scale applied.
///
Expand All @@ -723,13 +736,34 @@ impl<'a> RasterBand<'a> {
None
}

/// Fetch the no data value for this band.
/// Sets the no-data value for a `UInt64` band.
///
/// This method should ONLY be called on bands whose data type is `UInt64`.
///
/// This method should ONLY be called on rasters whose data type is `Int64`.
/// If `no_data` is `None`, any existing no-data value is deleted.
///
/// # Notes
/// See also:
/// [`GDALSetRasterNoDataValueAsUInt64`](https://gdal.org/api/raster_c_api.html#_CPPv432GDALSetRasterNoDataValueAsUInt6415GDALRasterBandH8uint64_t),
/// [`GDALDeleteRasterNoDataValue`](https://gdal.org/api/raster_c_api.html#_CPPv427GDALDeleteRasterNoDataValue15GDALRasterBandH)
#[cfg(all(major_ge_3, minor_ge_5))]
pub fn set_no_data_value_u64(&mut self, no_data: Option<u64>) -> Result<()> {
let rv = if let Some(no_data) = no_data {
unsafe { gdal_sys::GDALSetRasterNoDataValueAsUInt64(self.c_rasterband, no_data) }
} else {
unsafe { gdal_sys::GDALDeleteRasterNoDataValue(self.c_rasterband) }
};

if rv != CPLErr::CE_None {
Err(_last_cpl_err(rv))
} else {
Ok(())
}
}

/// Fetch the no-data value for this band.
///
/// If there is no out of data value, an out of range value will generally be returned.
/// The no-data value for a band is generally a special marker value used to mark pixels that are not valid data.
/// Such pixels should generally not be displayed, nor contribute to analysis operations.
/// This method should ONLY be called on bands whose data type is `Int64`.
///
/// The no data value returned is 'raw', meaning that it has no offset and scale applied.
///
Expand All @@ -750,12 +784,20 @@ impl<'a> RasterBand<'a> {
None
}

/// Sets the no-data value of this band.
/// Sets the no-data value for a `Int64` band.
///
/// This method should ONLY be called on bands whose data type is `Int64`.
///
/// If `no_data` is `None`, any existing no-data value is deleted.
pub fn set_no_data_value(&mut self, no_data: Option<f64>) -> Result<()> {
///
/// # Notes
/// See also:
/// [`GDALSetRasterNoDataValueAsInt64`](https://gdal.org/api/raster_c_api.html#_CPPv431GDALSetRasterNoDataValueAsInt6415GDALRasterBandH7int64_t),
/// [`GDALDeleteRasterNoDataValue`](https://gdal.org/api/raster_c_api.html#_CPPv427GDALDeleteRasterNoDataValue15GDALRasterBandH)
#[cfg(all(major_ge_3, minor_ge_5))]
pub fn set_no_data_value_i64(&mut self, no_data: Option<i64>) -> Result<()> {
let rv = if let Some(no_data) = no_data {
unsafe { gdal_sys::GDALSetRasterNoDataValue(self.c_rasterband, no_data) }
unsafe { gdal_sys::GDALSetRasterNoDataValueAsInt64(self.c_rasterband, no_data) }
} else {
unsafe { gdal_sys::GDALDeleteRasterNoDataValue(self.c_rasterband) }
};
Expand All @@ -766,7 +808,6 @@ impl<'a> RasterBand<'a> {
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
37 changes: 31 additions & 6 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::dataset::Dataset;
use crate::errors::Result;
use crate::metadata::Metadata;
use crate::raster::rasterband::ResampleAlg;
use crate::raster::{
Expand Down Expand Up @@ -471,18 +472,42 @@ fn test_get_no_data_value() {

#[test]
#[cfg(all(major_ge_3, minor_ge_5))]
fn test_get_no_data_value_i64() {
let dataset = Dataset::open(fixture("labels_i64.tif")).unwrap();
let rasterband = dataset.rasterband(1).unwrap();
fn test_no_data_value_i64() -> Result<()> {
let path = TempFixture::empty("test_no_data_value_i64.tiff");
{
let driver = DriverManager::get_driver_by_name("GTiff")?;
let ds = driver.create_with_band_type::<i64, _>(&path, 1, 1, 1)?;
let mut rasterband = ds.rasterband(1)?;
assert_eq!(rasterband.no_data_value_i64(), None);
rasterband.set_no_data_value_i64(Some(255i64))?;
}

let ds = Dataset::open(&path)?;
let rasterband = ds.rasterband(1)?;

assert_eq!(rasterband.no_data_value_i64(), Some(255i64));

Ok(())
}

#[test]
#[cfg(all(major_ge_3, minor_ge_5))]
fn test_get_no_data_value_u64() {
let dataset = Dataset::open(fixture("labels_u64.tif")).unwrap();
let rasterband = dataset.rasterband(1).unwrap();
fn test_no_data_value_u64() -> Result<()> {
let path = TempFixture::empty("test_no_data_value_u64.tiff");
{
let driver = DriverManager::get_driver_by_name("GTiff")?;
let ds = driver.create_with_band_type::<u64, _>(&path, 1, 1, 1)?;
let mut rasterband = ds.rasterband(1)?;
assert_eq!(rasterband.no_data_value_u64(), None);
rasterband.set_no_data_value_u64(Some(255u64))?;
}

let ds = Dataset::open(&path)?;
let rasterband = ds.rasterband(1)?;

assert_eq!(rasterband.no_data_value_u64(), Some(255u64));

Ok(())
}

#[test]
Expand Down

0 comments on commit 401f4a3

Please sign in to comment.