From fec5ccc6233bb7820d83e1121c0d011dc5f65862 Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Thu, 12 May 2022 12:31:35 +0200 Subject: [PATCH 1/3] Add wrapper for `GDALGetRasterUnitType` --- fixtures/114p01_0100_deme_truncated.dem | 1 + src/raster/rasterband.rs | 7 +++++++ src/raster/tests.rs | 13 +++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 fixtures/114p01_0100_deme_truncated.dem diff --git a/fixtures/114p01_0100_deme_truncated.dem b/fixtures/114p01_0100_deme_truncated.dem new file mode 100644 index 000000000..f5e8c3895 --- /dev/null +++ b/fixtures/114p01_0100_deme_truncated.dem @@ -0,0 +1 @@ + 114p01DEMe Base Mapping and Geomatic Services - B.C. Gov. - Victoria -13615 0.0000 59 0 0.00009 BC 1 1 0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3 2 4 -4.905000e+05 2.124000e+05 -4.905000e+05 2.133000e+05 -4.896000e+05 2.133000e+05 -4.896000e+05 2.124000e+05 -3.276700e+04 -3.276700e+04 0.0 07.500000e-017.500000e-011.000000e+00 1 1 1 4 1 1 1201 1 -4.905000000000000e+05 2.124000000000000e+05 0.000000000000000e+00 -3.276700000000000e+04 -3.276700000000000e+04-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 -32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767-32767 \ No newline at end of file diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index ba13ff3bd..f77aece3f 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -476,6 +476,13 @@ impl<'a> RasterBand<'a> { Ok(RasterBand::from_c_rasterband(self.dataset, overview)) } } + + /// Return the unit of the rasterband. + /// If there is no unit, the empty string is returned. + pub fn unit(&self) -> String { + let str_ptr = unsafe { gdal_sys::GDALGetRasterUnitType(self.c_rasterband) }; + _string(str_ptr) + } } impl<'a> MajorObject for RasterBand<'a> { diff --git a/src/raster/tests.rs b/src/raster/tests.rs index b1077c350..a8d647114 100644 --- a/src/raster/tests.rs +++ b/src/raster/tests.rs @@ -693,3 +693,16 @@ fn test_rasterize() { [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,] ); } + +#[test] +fn test_rasterband_unit() { + let dataset = Dataset::open(fixture!("tinymarble.tif")).unwrap(); + let rasterband = dataset.rasterband(1).unwrap(); + + assert!(rasterband.unit().is_empty()); + + let dataset = Dataset::open(fixture!("114p01_0100_deme_truncated.dem")).unwrap(); + let rasterband = dataset.rasterband(1).unwrap(); + + assert_eq!(rasterband.unit(), "m".to_string()); +} From 9990ed3923e0d0f5514ac5d7d97e807d37fca110 Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Thu, 12 May 2022 12:48:18 +0200 Subject: [PATCH 2/3] add changes to `CHANGES.md` --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 892ebb128..d4bdd844d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,10 @@ - +- Add `gdal::raster::rasterband::RasterBand::unit` as wrapper for `GDALGetRasterUnitType` + + - + ## 0.12 - Bump Rust edition to 2021 From f747c817b3dc39d0e3803519f9c58052ae961c4c Mon Sep 17 00:00:00 2001 From: Christian Beilschmidt Date: Thu, 12 May 2022 12:52:26 +0200 Subject: [PATCH 3/3] Check `GDALGetRasterUnitType` for null ptr --- src/raster/rasterband.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/raster/rasterband.rs b/src/raster/rasterband.rs index f77aece3f..2ae3cbfeb 100644 --- a/src/raster/rasterband.rs +++ b/src/raster/rasterband.rs @@ -481,6 +481,11 @@ impl<'a> RasterBand<'a> { /// If there is no unit, the empty string is returned. pub fn unit(&self) -> String { let str_ptr = unsafe { gdal_sys::GDALGetRasterUnitType(self.c_rasterband) }; + + if str_ptr.is_null() { + return String::new(); + } + _string(str_ptr) } }