Skip to content

Commit

Permalink
🎨 Initial CogReader struct with decoder field (#7)
Browse files Browse the repository at this point in the history
Defining a Cloud-Optimized GeoTIFF Reader struct with a decoder field containing an instance of tiff::decoder::Decoder with an unlimited decoding buffer size by default. Marking the struct as private for now until more method implementations are done.
  • Loading branch information
weiji14 authored Mar 10, 2024
1 parent 76e31be commit 2e463a7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/io/geotiff.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
use std::io::{Read, Seek};

use ndarray::Array2;
use tiff::decoder::{DecodingResult, Limits};
use tiff::{TiffError, TiffFormatError};
use tiff::decoder::{Decoder, DecodingResult, Limits};
use tiff::{TiffFormatError, TiffResult};

/// Cloud-optimized GeoTIFF reader
struct CogReader<R: Read + Seek> {
decoder: Decoder<R>,
}

impl<R: Read + Seek> CogReader<R> {
/// Create a new GeoTIFF decoder that decodes from a stream buffer
fn new(stream: R) -> TiffResult<Self> {
// Open TIFF stream with decoder
let mut decoder = Decoder::new(stream)?;
decoder = decoder.with_limits(Limits::unlimited());

Ok(Self { decoder })
}
}

/// Synchronously read a GeoTIFF file into an [`ndarray::Array`]
pub fn read_geotiff<R: Read + Seek>(stream: R) -> Result<Array2<f32>, TiffError> {
pub fn read_geotiff<R: Read + Seek>(stream: R) -> TiffResult<Array2<f32>> {
// Open TIFF stream with decoder
let mut decoder = tiff::decoder::Decoder::new(stream)?;
decoder = decoder.with_limits(Limits::unlimited());
let mut reader = CogReader::new(stream)?;

// Get image dimensions
let (width, height): (u32, u32) = decoder.dimensions()?;
let (width, height): (u32, u32) = reader.decoder.dimensions()?;

// Get image pixel data
let DecodingResult::F32(img_data) = decoder.read_image()? else {
let DecodingResult::F32(img_data) = reader.decoder.read_image()? else {
panic!("Cannot read band data")
};

Expand Down

0 comments on commit 2e463a7

Please sign in to comment.