Skip to content

Commit

Permalink
Merge pull request georust#462 from metasim/feature/resample-str
Browse files Browse the repository at this point in the history
Added `{Display|FromStr} for ResampleAlg` and `ResampleAlg::iter`.
  • Loading branch information
metasim committed Nov 7, 2023
2 parents a1aee76 + 6dbe9b6 commit 00e41d6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
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 `{Display|FromStr} for ResampleAlg` and `ResampleAlg::iter`.

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

- **Breaking**: Replaced `TryFrom<&[(&str, &str); N]> for CslStringList` with `impl FromIterator<CslStringListEntry> for CslStringList`, `impl FromIterator<String> for CslStringList` and `impl<'a> FromIterator<&'a str> for CslStringList`
- Added `Extend<CslStringListEntry> for CslStringList`, and `CslStringList::merge`

Expand Down
53 changes: 51 additions & 2 deletions src/raster/rasterband.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ use gdal_sys::{
};
use libc::c_int;
use std::ffi::CString;
use std::fmt::{Debug, Formatter};
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
use std::str::FromStr;

#[cfg(feature = "ndarray")]
use ndarray::Array2;

use crate::errors::*;
use crate::raster::ResampleAlg::{
Average, Bilinear, Cubic, CubicSpline, Gauss, Lanczos, Mode, NearestNeighbour,
};

/// [Dataset] methods for raster datasets.
impl Dataset {
Expand Down Expand Up @@ -106,7 +110,7 @@ impl Dataset {
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(u32)]
pub enum ResampleAlg {
/// Nearest neighbour
Expand All @@ -132,6 +136,51 @@ impl ResampleAlg {
pub fn to_gdal(&self) -> GDALRIOResampleAlg::Type {
*self as GDALRIOResampleAlg::Type
}

/// Get an iterator over all the valid enumeration values.
pub fn iter() -> impl Iterator<Item = ResampleAlg> {
use ResampleAlg::*;
[
NearestNeighbour,
Bilinear,
Cubic,
CubicSpline,
Lanczos,
Average,
Mode,
Gauss,
]
.into_iter()
}
}

impl Display for ResampleAlg {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
// Display format is the same as debug format.
Debug::fmt(self, f)
}
}

impl FromStr for ResampleAlg {
type Err = GdalError;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"nearestneighbour" => Ok(NearestNeighbour),
"bilinear" => Ok(Bilinear),
"cubic" => Ok(Cubic),
"cubicspline" => Ok(CubicSpline),
"lanczos" => Ok(Lanczos),
"average" => Ok(Average),
"mode" => Ok(Mode),
"gauss" => Ok(Gauss),
o => Err(GdalError::BadArgument(format!(
"'{}' does not match one of {:?}",
o,
Self::iter().map(|e| e.to_string()).collect::<Vec<_>>()
))),
}
}
}

/// Wrapper type for gdal mask flags.
Expand Down
13 changes: 13 additions & 0 deletions src/raster/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::test_utils::{fixture, TempFixture};
use crate::vsi::unlink_mem_file;
use crate::DriverManager;
use std::path::Path;
use std::str::FromStr;

#[cfg(feature = "ndarray")]
use ndarray::arr2;
Expand Down Expand Up @@ -749,3 +750,15 @@ fn test_raster_stats() {
}
);
}

#[test]
fn test_resample_str() {
assert!(ResampleAlg::from_str("foobar").is_err());

for e in ResampleAlg::iter() {
let stringed = e.to_string();
let parsed = ResampleAlg::from_str(&stringed);
assert!(parsed.is_ok(), "{stringed}");
assert_eq!(parsed.unwrap(), e, "{stringed}");
}
}

0 comments on commit 00e41d6

Please sign in to comment.