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

✨ CogReader ndarray method to decode TIFF into an ndarray Array #10

Merged
merged 2 commits into from
Mar 15, 2024

Conversation

weiji14
Copy link
Owner

@weiji14 weiji14 commented Mar 14, 2024

Moving more of the TIFF decoding functionality from the read_geotiff function to impl methods under the CogReader struct that was made in #7. Nothing new actually, same-ish code from #3.

TODO:

  • Create as_vec method for decoding TIFF to Vec (merged into one ndarray function below)
  • Create ndarray method for decoding TIFF to Vec and then converting to an ndarray Array
  • Refactor as needed

Making the decoder.read_image() code an impl method under the CogReader struct. Reworded the previous panic message into an unimplemented error with a more helpful message that dtypes other than float32 are not yet supported.
@weiji14 weiji14 added the enhancement New feature or request label Mar 14, 2024
@weiji14 weiji14 added this to the 0.1.0 milestone Mar 14, 2024
@weiji14 weiji14 self-assigned this Mar 14, 2024
Move the previous tiff-decoder -> Vec -> ndarray code to an impl method under CogReader. Was going to separate the as_vec and ndarray code, but got caught up with move semantics when trying to use the methods in pyo3, so just having an all-in-one ndarray method now. Added a unit test to check that an ndarray is properly returned.
@weiji14 weiji14 changed the title ♻️ Turn TIFF to ndarray decoding code into CogReader methods ❇️ CogReader ndarray method to decode TIFF into an ndarray Array Mar 14, 2024
@weiji14 weiji14 changed the title ❇️ CogReader ndarray method to decode TIFF into an ndarray Array ✨ CogReader ndarray method to decode TIFF into an ndarray Array Mar 14, 2024
Comment on lines +24 to +41
/// Decode GeoTIFF image to an [`ndarray::Array`]
fn ndarray(&mut self) -> TiffResult<Array2<f32>> {
// Get image dimensions
let (width, height): (u32, u32) = self.decoder.dimensions()?;

// Get image pixel data
let decode_result = self.decoder.read_image()?;
let image_data: Vec<f32> = match decode_result {
DecodingResult::F32(img_data) => img_data,
_ => unimplemented!("Data types other than float32 are not yet supported."),
};

// Put image pixel data into an ndarray
let vec_data = Array2::from_shape_vec((height as usize, width as usize), image_data)
.map_err(|_| TiffFormatError::InvalidDimensions(height, width))?;

Ok(vec_data)
}
Copy link
Owner Author

@weiji14 weiji14 Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was playing around with wrapping parts of this in a TryFrom trait, but got caught up fighting the borrow checker when trying to use it in another part of the crate, so will leave things here for now.

@weiji14 weiji14 marked this pull request as ready for review March 15, 2024 00:31
@weiji14 weiji14 merged commit d4786a1 into main Mar 15, 2024
13 checks passed
@weiji14 weiji14 deleted the to_vec_and_ndarray branch March 15, 2024 00:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant