Skip to content

Commit a1862e3

Browse files
authored
Add Python example to README (#66)
1 parent b5ca6c2 commit a1862e3

File tree

7 files changed

+164
-88
lines changed

7 files changed

+164
-88
lines changed

python/README.md

+80-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,86 @@
11
# async-tiff
22

3-
<!-- [![PyPI][pypi_badge]][pypi_link]
3+
[![PyPI][pypi_badge]][pypi_link]
44

5-
[pypi_badge]: https://badge.fury.io/py/geoindex-rs.svg
6-
[pypi_link]: https://pypi.org/project/geoindex-rs/ -->
5+
[pypi_badge]: https://badge.fury.io/py/async-tiff.svg
6+
[pypi_link]: https://pypi.org/project/async-tiff/
77

8-
Fast, async TIFF and GeoTIFF reader for Python.
8+
Fast, low-level async TIFF and GeoTIFF reader for Python.
99

10-
<!-- This documentation is for the Python bindings. [Refer here for the Rust crate documentation](https://docs.rs/geo-index). -->
10+
This documentation is for the Python bindings. [Refer here for the Rust crate documentation](https://docs.rs/async-tiff).
11+
12+
## Examples
13+
14+
### Reading NAIP
15+
16+
```py
17+
from async_tiff import TIFF
18+
from async_tiff.store import S3Store
19+
20+
# You'll also need to provide credentials to access a requester pays bucket
21+
store = S3Store("naip-visualization", region="us-west-2", request_payer=True)
22+
path = "ny/2022/60cm/rgb/40073/m_4007307_sw_18_060_20220803.tif"
23+
24+
tiff = await TIFF.open(path, store=store, prefetch=32768)
25+
primary_ifd = tiff.ifds[0]
26+
27+
primary_ifd.geo_key_directory.citation
28+
# 'NAD83 / UTM zone 18N'
29+
30+
primary_ifd.geo_key_directory.projected_type
31+
# 26918
32+
# (EPSG code)
33+
34+
primary_ifd.sample_format
35+
# [<SampleFormat.Uint: 1>, <SampleFormat.Uint: 1>, <SampleFormat.Uint: 1>]
36+
37+
primary_ifd.bits_per_sample
38+
# [8, 8, 8]
39+
40+
tile = await tiff.fetch_tile(0, 0, 4)
41+
decoded_bytes = await tile.decode_async()
42+
43+
# Use rasterio and matplotlib for visualization
44+
import numpy as np
45+
from rasterio.plot import reshape_as_raster, show
46+
47+
# Wrap the rust buffer into a numpy array
48+
arr = np.frombuffer(decoded_bytes, np.uint8)
49+
50+
# We first need to reshape the array into the *existing* "image" axes
51+
arr = arr.reshape(512, 512, 3)
52+
53+
# Then we need to reshape the "image" axes into "raster" axes
54+
# https://rasterio.readthedocs.io/en/stable/topics/image_processing.html
55+
arr = reshape_as_raster(arr)
56+
show(arr, adjust=True)
57+
```
58+
59+
![](assets/naip-example.jpg)
60+
61+
62+
### Reading Sentinel 2 L2A
63+
64+
```py
65+
from async_tiff import TIFF
66+
from async_tiff.store import S3Store
67+
68+
store = S3Store("sentinel-cogs", region="us-west-2", skip_signature=True)
69+
path = "sentinel-s2-l2a-cogs/12/S/UF/2022/6/S2B_12SUF_20220609_0_L2A/B04.tif"
70+
71+
tiff = await TIFF.open(path, store=store, prefetch=32768)
72+
primary_ifd = tiff.ifds[0]
73+
# Text readable citation
74+
primary_ifd.geo_key_directory.citation
75+
# EPSG code
76+
primary_ifd.geo_key_directory.projected_type
77+
78+
primary_ifd.sample_format[0]
79+
# <SampleFormat.Uint: 1>
80+
primary_ifd.bits_per_sample[0]
81+
# 16
82+
83+
tile = await tiff.fetch_tile(0, 0, 0)
84+
decoded_bytes = await tile.decode_async()
85+
```
1186

File renamed without changes.

python/assets/naip-example.jpg

44.4 KB
Loading

python/docs/assets

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../assets

python/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dev-dependencies = [
3131
"mkdocstrings-python>=1.13.0",
3232
"mkdocstrings>=0.27.0",
3333
"numpy>=1",
34-
"obstore",
34+
"obstore>=0.5.1",
3535
"pip>=24.2",
3636
"pytest-asyncio>=0.24.0",
3737
"pytest>=8.3.3",

python/python/async_tiff/_tile.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Tile:
1818
@property
1919
def compression_method(self) -> CompressionMethod | int:
2020
"""The compression method used by this tile."""
21-
async def decode(
21+
async def decode_async(
2222
self,
2323
*,
2424
decoder_registry: DecoderRegistry | None = None,

python/uv.lock

+81-81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)