Skip to content

Commit

Permalink
Merge pull request #3 from Clay-foundation/naip-tutorial
Browse files Browse the repository at this point in the history
Add naip tutorial
  • Loading branch information
yellowcap authored Jun 7, 2024
2 parents 38da4e9 + 8195e4d commit 39afdbb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nav:
- Indexer: "indexer.md"
- Chipper: "chipper.md"
- Processors: "processors.md"
- Tutorial: "naip-tutorial.md"

plugins:
- search
Expand Down
59 changes: 59 additions & 0 deletions docs/src/naip-tutorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
The following code example shows how to obtain RGB+NIR chips from
NAIP imagery and plot them.

```python
import random

import pystac_client
from stacchip.indexer import NoStatsChipIndexer
from stacchip.chipper import Chipper
import os
import matplotlib.pyplot as plt

# Optimize GDAL settings for cloud optimized reading
os.environ["GDAL_DISABLE_READDIR_ON_OPEN"] = "EMPTY_DIR"
os.environ["AWS_REQUEST_PAYER"] = "requester"

# Query STAC catalog for NAIP data
catalog = pystac_client.Client.open("https://earth-search.aws.element84.com/v1")


items = catalog.search(
collections=["naip"],
max_items=100,
)

items = items.item_collection()

items_list = list(items)
random.shuffle(items_list)

chips = []
for item in items_list[:10]:
print(f"Working on {item}")

# Index the chips in the item
indexer = NoStatsChipIndexer(item)

# Instanciate the chipper
chipper = Chipper(indexer, asset_blacklist=["metadata"])

# Get first chip for the "image" asset key
for chip_id in random.sample(range(0, len(chipper)), 5):
chips.append(chipper[chip_id]["image"])


fig, axs = plt.subplots(5, 10, gridspec_kw={'wspace': 0.01, 'hspace': 0.01}, squeeze=True)

for idx, ax in enumerate(axs.flatten()):
chip = chips[idx]
# Visualize the data
ax.imshow(chip[:3].swapaxes(0, 1).swapaxes(1, 2))

plt.tight_layout()
plt.show()
```

Resutling in the following plot

![naip-rgb](https://github.com/Clay-foundation/stacchip/assets/901647/86844530-9297-4971-b9e5-dd5c25b28b0e)

0 comments on commit 39afdbb

Please sign in to comment.