Skip to content

feat!: enable fetching only specific planes from a band-interleaved TIFF - #251

Merged
weiji14 merged 30 commits into
mainfrom
specific_band_fetch
Oct 22, 2026
Merged

weiji14 merged 30 commits into
mainfrom
specific_band_fetch

Conversation

@weiji14

@weiji14 weiji14 commented Feb 24, 2026 •

Copy link
Copy Markdown
Member

What I am changing

  • Enable fetching only specific bandsplanes from an IFD, instead of all bandsplanes, useful when data is band-interleaved and/or there are a large number of bandsplanes that don't need to be fetched
  • Considered a breaking change since call changes from ifd.fetch_tile(0, 0, &reader) to ifd.fetch_tile(0, 0, None, &reader)

How I did it

  • Initial implementation to fetch a range of bandsplanes by modifying .fetch_tile()
  • Refactor to allow fetching disjoint bandplane indexes
  • Better handle errors when trying to fetch invalid bandsplanes (kinda done, but not ergonomic)
  • Propagate to .fetch_tiles() method.
  • Propagate to python bindings (both ifd.fetch_tile() and TIFF.fetch_tile())

How you can test it

  • Run cargo test locally.
  • Run cd python/ and uv run --no-project pytest tests --verbose locally.

Related Issues

Comment thread src/ifd.rs Outdated
&self,
x: usize,
y: usize,
bands: Option<Range<usize>>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. maybe it makes sense to create an options object for cleaner forwards compatibility
  2. This should probably be a Vec<usize> instead of a Range. There's no requirement for the bands to be contiguous.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yep, agree with 2. Can you elaborate on 1? What's an options object?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just mean something like

struct FetchOptions {
    bands: Option<Vec<usize>>,
}

so that we can add more options in the future if necessary without changing the primary function signature here again.

@weiji14 weiji14 Feb 25, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right, I was thinking something like that with a Default impl trait (that returns all bands by default) so Option<> won't be needed (unsure if it's possible since bands are runtime determined), or using a builder pattern like .fetch_tiles(x, y, ...).with_bands(0..3).build().

Ok to go with FetchOptions, since we can reuse it for .fetch_tiles() as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah we can implement Default for FetchOptions. But right I think we'll still need to represent bands: None to support all bands when those band indices aren't known till later.

Yes we can reuse it for fetch_tiles and I think we can ignore the niche case where someone might want to fetch different bands for each tile in fetch_tiles.

Change from passing in a Range<usize> to a Vec<usize> so specific bands can be indexed.
Create a FetchOptions struct that allows passing specific parameters (currently only `bands`) when doing a fetch_tile operation. Keep the existing option of passing only None to do the default action of fetching of all bands.
With some special handling to handle 0-based indexing.
Use the same FetchOptions struct, included another unit test using the eox_cloudless.tif data as test case.
@weiji14
weiji14 force-pushed the specific_band_fetch branch from 23a18f6 to 7a3ca77 Compare March 2, 2026 05:30
Comment thread src/ifd.rs Outdated
&self,
x: usize,
y: usize,
fetch_options: Option<FetchOptions>,

@weiji14 weiji14 Mar 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Continuing from #251 (comment):

Yeah we can implement Default for FetchOptions. But right I think we'll still need to represent bands: None to support all bands when those band indices aren't known till later.

Yes we can reuse it for fetch_tiles and I think we can ignore the niche case where someone might want to fetch different bands for each tile in fetch_tiles.

I went with Option<FetchOptions> here, which makes it easier to pass just None if wanting the default all bands instead of instantiating something like FetchOptions {bands: None}, but not sure how I feel about this tbh.

Almost thinking if we should just go back to plain bands: Option<Vec<usize>>. Realistically speaking, are there other fetch related configuration options we might need to handle later to require future-proofing the API like this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Realistically speaking, are there other fetch related configuration options we might need to handle later to require future-proofing the API like this?

Rust doesn't have keyword arguments, only positional arguments. In Python, I have a set of heuristics for when I define positional vs keyword-only arguments. In particular, as much as possible (at least for public facing APIs), arguments that are allowed to be positional should be plainly obvious what they do. E.g. in fetch_tile(x, y) it can be reasonably understood what fetch_tile(1, 2) means without having to look up the function definition or documentation.

Once you get further away from that, though, it becomes hard to know what fetch_tile(1, 2, None) means, without going back to look at what the named parameter for None was.

My suggestion above is essentially applying this same heuristic to Rust. Even if we don't expect more options (which I'm not confident in), then for public APIs like this it may still be better to have an "options object" which essentially acts as Python keyword-only parameters.

I went with Option<FetchOptions> here, which makes it easier to pass just None if wanting the default all bands instead of instantiating something like FetchOptions {bands: None}, but not sure how I feel about this tbh.

I'd suggest against it. I'd rather have FetchOptions and the user can pass in Default::default() if they want default options. Then it's more readable at the callsite that you're getting the default options, instead of having to recall that None means "use default options".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I went with Option<FetchOptions> here, which makes it easier to pass just None if wanting the default all bands instead of instantiating something like FetchOptions {bands: None}, but not sure how I feel about this tbh.

I'd suggest against it. I'd rather have FetchOptions and the user can pass in Default::default() if they want default options. Then it's more readable at the callsite that you're getting the default options, instead of having to recall that None means "use default options".

Gotcha. I'll refactor this after #261. Using Option<FetchOptions> did sound weird, I need to change my Python-way of thinking that None means default 😆

@kylebarron

Copy link
Copy Markdown
Member

I think after #261 this should become very simple and straightforward of just indexing into the ranges array for planar data.

@kylebarron

Copy link
Copy Markdown
Member

I went ahead and updated this PR given #261... but then I realized we have to make a choice... is our band indexing 0-based or 1-based 🙈

async-geotiff has adopted 1-based indexing to match GDAL/Rasterio, but I think it may be best here in async-tiff to use 0-based indexing, since we're trying to be generic for all TIFFs. And here the band numbers are used to index into a list of offsets, which for a general computer science person would obviously be 0-based.

@weiji14 do you have any thoughts?

@weiji14

weiji14 commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

Thanks for handling the merge conflict and adding the Python-bindings!

async-geotiff has adopted 1-based indexing to match GDAL/Rasterio, but I think it may be best here in async-tiff to use 0-based indexing, since we're trying to be generic for all TIFFs. And here the band numbers are used to index into a list of offsets, which for a general computer science person would obviously be 0-based.

@weiji14 do you have any thoughts?

Yeah, would prefer 0-based here (math is easier that way). I secretly wanted to 👎 developmentseed/async-geotiff#14 but won't argue with the experts 🙈

@kylebarron

Copy link
Copy Markdown
Member

I think given the geospatial ecosystem in Python is already extremely reliant on 1-based band indexing, it would be more disruptive to switch to 0-based band indexing there. But here I'm open to it.

Comment thread python/src/tiff.rs Outdated
Comment on lines +86 to +101
future_into_py(py, async move {
let tile = ifd
.fetch_tile(x, y, reader.as_ref())
.fetch_tile(x, y, reader.as_ref(), Default::default())

@weiji14 weiji14 Mar 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Should the TIFF.fetch_tile() method also expose specific band fetching? Or limit it to be only on the IFD level?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes it should expose band fetching; good catch

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm happy to just keep it at the IFD level to be honest. But I suppose you want to align with the Typescript implementation in deck.gl-raster?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it makes sense to mirror the same options in both sets of fetch_tile. And it's a pretty simple option to pass along

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok, finally added TIFF.fetch_tile(..., bands=?) and TIFF.fetch_tiles(..., bands=?) at 0d33cba. Please check the .pyi type stubs though.

@mdsumner

Copy link
Copy Markdown

I think given the geospatial ecosystem in Python is already extremely reliant on 1-based band indexing, it would be more disruptive to switch to 0-based band indexing there. But here I'm open to it.

And GDAL overview 0 is IFD level 1. I have internalized mental reminders to keep track. 🙏

@kylebarron

Copy link
Copy Markdown
Member

I think given the geospatial ecosystem in Python is already extremely reliant on 1-based band indexing, it would be more disruptive to switch to 0-based band indexing there. But here I'm open to it.

And GDAL overview 0 is IFD level 1. I have internalized mental reminders to keep track. 🙏

@mdsumner you may be better served by using https://github.com/developmentseed/async-geotiff, if you're not already, because it automatically associates images with overviews and their masks, and makes it easy to access specific tiles in a geospatial context.

Add `bands` kwarg to TIFF.fetch_tile and TIFF.fetch_tiles to allow fetching only specific bands (or by default, all bands when `None` is given). Included a unit test to ensure it is possible to fetch only 2 bands from an RGB TIFF.
Include updated uint8_1band_deflate_block128_unaligned_predictor2.tif file from developmentseed/geotiff-test-data#74
Try pulling 3 bands out of 64 from the sample AlphaEarth Foundations embedding TIFF dataset. Updated geotiff-test-data from b7158c4 to 0b1055b which includes the tiled sample AEF embedding TIFF from developmentseed/geotiff-test-data#79
Comment thread python/python/async_tiff/_ifd.pyi Outdated
Args:
x: The column index within the ifd to read from.
y: The row index within the ifd to read from.
bands: The band indexes to read from. If `None`, all bands will be read.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should be explicit here how the integer is used; I think this is 0-based indexing?

@weiji14 weiji14 Oct 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Mentioned 0-based indexing now at commit bd1c14f

Comment thread python/python/async_tiff/_tiff.pyi Resolved
Comment thread python/python/async_tiff/_tiff.pyi Resolved
Comment thread src/ifd.rs Resolved
Comment thread src/ifd.rs
Comment on lines +841 to +843
return Err(AsyncTiffError::General(
"Band selection is not supported for chunky TIFFs".to_string(),
));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is fine for now but a little messy that sometimes bands works and sometimes it doesn't.

Comment thread src/ifd.rs Outdated
PlanarConfiguration::Chunky => {
if bands.is_some() {
return Err(AsyncTiffError::General(
"Band selection is not supported for chunky TIFFs".to_string(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"Band selection is not supported for chunky TIFFs".to_string(),
"Band selection is not supported for pixel-interleaved TIFFs".to_string(),

The enum uses Chunky because that's the wording in the TIFF spec IIRC. But I think pixel-interleaved is a clearer wording

@weiji14 weiji14 Oct 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Again, pixel-interleaved (and it's sibling band-interleaved) is more commonly used in a geo-/raster context than regular images. I think we should follow the TIFF spec's language here (though no strong opinions either if you prefer pixel-interleave).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think I left this comment before I saw #360 to rename band to plane.

But I feel you're being contradictory here and #360 (comment). There you suggest using the raster term and here you suggest using the TIFF term.

I feel like we should use the TIFF terms in async-tiff and the raster terms in async-geotiff.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The comment I left at #360 (comment) was on a unit test, so non-user-facing (and the unit test is on a GeoTIFF, so ok to use band-interleave, since we devs know what it means).

Here though, the error message is intended for async-tiff users, so yes, it should use TIFF terms (Chunky/Planar) as you said.

Comment thread src/ifd.rs Resolved
Comment thread src/ifd.rs Resolved
@weiji14 weiji14 changed the title feat!: fetch only specific bands instead of all bands feat!: enable fetching only specific planes from a band-interleaved TIFF Oct 16, 2026
E.g. [0, 1, 2] for the first three planes/bands.
@weiji14
weiji14 force-pushed the specific_band_fetch branch from 6919b31 to bd1c14f Compare October 16, 2026 10:33
"""
async def fetch_tiles(self, xy: Sequence[tuple[int, int]]) -> list[Tile]:
async def fetch_tiles(
self, xy: Sequence[tuple[int, int]], *, planes: Sequence[int] | None = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so here we only support reading the same sequence of planes from all tiles? I think that's probably simpler than supporting varying planes per tile

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep. If someone wants varying planes per tile, they can call multiple fetch_tile operations with the different plane indexes.

@kylebarron

Copy link
Copy Markdown
Member

feel free to merge when ready

@weiji14
weiji14 merged commit 292218d into main Oct 22, 2026
14 checks passed
@weiji14
weiji14 deleted the specific_band_fetch branch October 22, 2026 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants