georust / geotiff

Reading GeoTIFFs in Rust, nothing else!
MIT License
69 stars 21 forks source link

thread 'main' panicked at 'index out of bounds: the len is 724 but the index is 724 #6

Open LaurentBerder opened 1 year ago

LaurentBerder commented 1 year ago

Hi,

I'm fairly new in Rust and wanted to try reading a .tif hyperspectral imaging file which contains multiple layers (its shape is (356, 724, 757), for 356 layers, width of 724 and height of 757 pixels).

My first test was to just read the file, then look at value at coordinates (1, 1), which I'm guessing should be a vector of size 356.

extern crate geotiff;

fn main() {
    let geotiff = geotiff::TIFF::open("path_to_my_file.tif").unwrap();
    println!("{:?}", geotiff);

    let pixel = geotiff.get_value_at(1, 1);
    println!("{:?}", pixel);
}

But I run into this panic:

thread 'main' panicked at 'index out of bounds: the len is 724 but the index is 724', ...index.crates.io-6f17d22bba15001f\geotiff-0.0.2\src\reader.rs:303:17

Should I understand that the multiplicity of layers is not handled by geotiff, or am I doing something wrong?

dominikbucher commented 1 year ago

Hi Laurent!

It's been a while since I originally started the library, and as a word of caution, I only needed it to read elevation data (single channel / layer) - it served that purpose but is sadly far from implementing the whole GeoTiff standard.

Looking at https://github.com/georust/geotiff/blob/master/src/lib.rs#L35, it should return the first layer, however, your panic seems to be happening already during parsing. Looking at https://github.com/georust/geotiff/blob/master/src/reader.rs#L303 it seems that one of the accessors (in img[curr_x][curr_y][curr_z]) cause the error, and not necessarily the conversion of the value (self.vec_to_value::<Endian>(v);). This could well be because your tiff has multiple layers.

Lots of coulds and woulds, I know (sorry!), but it was 5 years ago that I wrote the code... if you manage to dig into the library and fix the issues, happy to merge a PR! The library is not particularly large so it should be understandable fairly quickly :) (the GeoTiff spec on the other hand...)

Cheers

dominikbucher commented 1 year ago

Related https://github.com/georust/geotiff/issues/4

LaurentBerder commented 1 year ago

Thanks for your reply, @dominikbucher. As much as I'd love to give you a hand on this, I must be lucid on the fact that my level of mastery for Rust is not yet sufficient!