-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Fetch tile data from GeoTIFF/Overview #22
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
faffa2e
feat: Fetch tile data from GeoTIFF overview
kylebarron dac422c
Ensure we set top-level mask IFD correctly
kylebarron c97e5d8
Refactor fetch tiles
kylebarron 0f1dbe2
Add fetch_tile to top-level `GeoTIFF`
kylebarron 4ce103e
Add `fetch_tiles` to full-resolution GeoTIFF
kylebarron 31bb832
move into type checking block
kylebarron 1ba7d93
Add test for tile fetch
kylebarron da55ac0
Merge branch 'main' into kyle/fetch-tiles
kylebarron 868f8aa
Add TransformMixin parent
kylebarron 8567be6
Use mixin for defining tile fetching
kylebarron 8d45ddf
Fix typing
kylebarron 8f02864
cleaner association of masks to data ifds
kylebarron 742b2b1
Add IFDReference dataclass as abstraction to hold both the ifd and it…
kylebarron cb9de60
Fix axis ordering
kylebarron a07c599
add test fetching overview
kylebarron 3a13d31
Update tests
kylebarron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from affine import Affine | ||
| from numpy.typing import NDArray | ||
| from pyproj import CRS | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True, eq=False) | ||
| class Array: | ||
| """An array representation of data from a GeoTIFF.""" | ||
|
|
||
| data: NDArray | ||
| """The raw byte data of the array.""" | ||
|
|
||
| mask: NDArray | None | ||
| """The mask array, if any.""" | ||
|
|
||
| width: int | ||
| """The width of the array in pixels.""" | ||
|
|
||
| height: int | ||
| """The height of the array in pixels.""" | ||
|
|
||
| transform: Affine | ||
| """The affine transform mapping pixel coordinates to geographic coordinates.""" | ||
|
|
||
| crs: CRS | ||
| """The coordinate reference system of the array.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
| from affine import Affine | ||
|
|
||
| from async_geotiff import Array | ||
|
|
||
| if TYPE_CHECKING: | ||
| from async_tiff import TIFF | ||
| from async_tiff import Array as AsyncTiffArray | ||
| from pyproj import CRS | ||
|
|
||
|
|
||
| async def fetch_tile( # noqa: PLR0913 | ||
| *, | ||
| x: int, | ||
| y: int, | ||
| tiff: TIFF, | ||
| crs: CRS, | ||
| ifd_index: int, | ||
| mask_ifd_index: int | None, | ||
| transform: Affine, | ||
| tile_width: int, | ||
| tile_height: int, | ||
| ) -> Array: | ||
| tile_fut = tiff.fetch_tile(x, y, ifd_index) | ||
|
|
||
| mask_data: AsyncTiffArray | None = None | ||
| if mask_ifd_index := mask_ifd_index: | ||
| mask_fut = tiff.fetch_tile(x, y, mask_ifd_index) | ||
| tile, mask = await asyncio.gather(tile_fut, mask_fut) | ||
| tile_data, mask_data = await asyncio.gather(tile.decode(), mask.decode()) | ||
| else: | ||
| tile = await tile_fut | ||
| tile_data = await tile.decode() | ||
|
|
||
| tile_transform = transform * Affine.translation( | ||
| x * tile_width, | ||
| y * tile_height, | ||
| ) | ||
|
|
||
| return Array( | ||
| data=np.asarray(tile_data), | ||
| mask=np.asarray(mask_data) if mask_data else None, | ||
| crs=crs, | ||
| transform=tile_transform, | ||
| width=tile_width, | ||
| height=tile_height, | ||
| ) | ||
|
|
||
|
|
||
| async def fetch_tiles( # noqa: PLR0913, D417 | ||
| *, | ||
| xs: list[int], | ||
| ys: list[int], | ||
| tiff: TIFF, | ||
| crs: CRS, | ||
| ifd_index: int, | ||
| mask_ifd_index: int | None, | ||
| transform: Affine, | ||
| tile_width: int, | ||
| tile_height: int, | ||
| ) -> list[Array]: | ||
| """Fetch multiple tiles from this overview. | ||
|
|
||
| Args: | ||
| xs: The x coordinates of the tiles. | ||
| ys: The y coordinates of the tiles. | ||
|
|
||
| """ | ||
| tiles_fut = tiff.fetch_tiles(xs, ys, ifd_index) | ||
|
|
||
| decoded_masks: list[AsyncTiffArray | None] = [None] * len(xs) | ||
| if mask_ifd_index := mask_ifd_index: | ||
| masks_fut = tiff.fetch_tiles(xs, ys, mask_ifd_index) | ||
| tiles, masks = await asyncio.gather(tiles_fut, masks_fut) | ||
|
|
||
| decoded_tile_futs = [tile.decode() for tile in tiles] | ||
| decoded_mask_futs = [mask.decode() for mask in masks] | ||
| decoded_tiles = await asyncio.gather(*decoded_tile_futs) | ||
| decoded_masks = await asyncio.gather(*decoded_mask_futs) | ||
| else: | ||
| tiles = await tiles_fut | ||
| decoded_tiles = await asyncio.gather(*[tile.decode() for tile in tiles]) | ||
|
|
||
| arrays: list[Array] = [] | ||
| for x, y, tile_data, mask_data in zip( | ||
| xs, | ||
| ys, | ||
| decoded_tiles, | ||
| decoded_masks, | ||
| strict=True, | ||
| ): | ||
| tile_transform = transform * Affine.translation( | ||
| x * tile_width, | ||
| y * tile_height, | ||
| ) | ||
| array = Array( | ||
| data=np.asarray(tile_data), | ||
| mask=np.asarray(mask_data) if mask_data else None, | ||
| crs=crs, | ||
| transform=tile_transform, | ||
| width=tile_width, | ||
| height=tile_height, | ||
| ) | ||
| arrays.append(array) | ||
|
|
||
| return arrays |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """Test fetching tiles from a GeoTIFF.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from rasterio.windows import Window | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .conftest import LoadGeoTIFF, LoadRasterio | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_fetch(load_geotiff: LoadGeoTIFF, load_rasterio: LoadRasterio) -> None: | ||
| name = "uint8_rgb_deflate_block64_cog" | ||
|
|
||
| geotiff = await load_geotiff(name) | ||
|
|
||
| tile = await geotiff.fetch_tile(0, 0) | ||
|
|
||
| window = Window(0, 0, geotiff.tile_width, geotiff.tile_height) | ||
| with load_rasterio(name) as rasterio_ds: | ||
| rasterio_data = rasterio_ds.read(window=window) | ||
|
|
||
| np.testing.assert_array_equal(tile.data, rasterio_data) | ||
| assert tile.crs == geotiff.crs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully remember the COG architecture, but I think at one point the Mask where stored after all the image IFD (at least when not using the COG driver)
Does async-geotiff read the ghost header https://gdal.org/en/stable/drivers/raster/cog.html#header-ghost-area ?
also might specify in the library that we don't support external overview/mask 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. I just assumed it would always be data/mask/overview/mask overview.
No, we don't currently read the ghost header. @geospatial-jeff suggested not to in developmentseed/async-tiff#7. I think we might want to allow injecting support for it. In theory that might be something we could inject.
I think in the future, supporting external overview/mask would be possible, but no, not right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
8f02864(this PR) I implemented a cleaner association of data ifds to masks:First create
dictsmapping from(image height, image width): (ifd index, ifd):Then iterate over the data ifds from largest to smallest. If there's a mask IFD of the same height and width, then associate the mask IFD with that data IFD.