-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Clay-foundation/naip-tutorial
Add naip tutorial
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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) |