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

Added {Display|FromStr} for ResampleAlg and ResampleAlg::iter. #462

Merged
merged 1 commit into from
Nov 7, 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 `{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}");
}
}