|
| 1 | +# Opening Data with Rasterio |
| 2 | + |
| 3 | +This guide shows how to use `obspec-utils` readers to open cloud-hosted raster data with [rasterio](https://rasterio.readthedocs.io/). |
| 4 | + |
| 5 | +## Opening a Cloud-Hosted GeoTIFF |
| 6 | + |
| 7 | +Use [`EagerStoreReader`][obspec_utils.readers.EagerStoreReader] to provide a file-like interface that rasterio can read: |
| 8 | + |
| 9 | +```python exec="on" source="above" session="rasterio" result="code" |
| 10 | +import rasterio |
| 11 | +from obstore.store import S3Store |
| 12 | +from obspec_utils.readers import EagerStoreReader |
| 13 | + |
| 14 | +# Access public Arctic DEM data (no credentials needed) |
| 15 | +store = S3Store( |
| 16 | + bucket="pgc-opendata-dems", |
| 17 | + aws_region="us-west-2", |
| 18 | + skip_signature=True, |
| 19 | +) |
| 20 | + |
| 21 | +path = "arcticdem/mosaics/v4.1/2m_dem_tiles.vrt" |
| 22 | + |
| 23 | +with EagerStoreReader(store, path) as reader: |
| 24 | + with rasterio.open(reader) as src: |
| 25 | + print(f"CRS: {src.crs}") |
| 26 | + print(f"Bounds: {src.bounds}") |
| 27 | + print(f"Shape: {src.width} x {src.height}") |
| 28 | +``` |
| 29 | + |
| 30 | +## Using with Xarray and rioxarray |
| 31 | + |
| 32 | +For analysis workflows, combine with [rioxarray](https://corteva.github.io/rioxarray/) to load raster data as xarray datasets: |
| 33 | + |
| 34 | +```python exec="on" source="above" session="rasterio" result="code" |
| 35 | +import xarray as xr |
| 36 | +from obstore.store import S3Store |
| 37 | +from obspec_utils.readers import EagerStoreReader |
| 38 | + |
| 39 | +store = S3Store( |
| 40 | + bucket="pgc-opendata-dems", |
| 41 | + aws_region="us-west-2", |
| 42 | + skip_signature=True, |
| 43 | +) |
| 44 | + |
| 45 | +path = "arcticdem/mosaics/v4.1/2m_dem_tiles.vrt" |
| 46 | + |
| 47 | +with EagerStoreReader(store, path) as reader: |
| 48 | + ds = xr.open_dataset(reader, engine="rasterio") |
| 49 | + print(ds) |
| 50 | +``` |
| 51 | + |
| 52 | +## Choosing a Reader |
| 53 | + |
| 54 | +For raster data, the choice of reader depends on your access pattern: |
| 55 | + |
| 56 | +| Reader | Best for | |
| 57 | +|--------|----------| |
| 58 | +| [`EagerStoreReader`][obspec_utils.readers.EagerStoreReader] | Small files or when you need the entire file (metadata parsing, VRT files) | |
| 59 | +| [`BlockStoreReader`][obspec_utils.readers.BlockStoreReader] | Large files with sparse access patterns (reading specific tiles/windows) | |
| 60 | + |
| 61 | +For most rasterio use cases, [`EagerStoreReader`][obspec_utils.readers.EagerStoreReader] works well since rasterio typically needs to read file headers and metadata which requires random access across the file. |
0 commit comments