Back to snippets

planetary_computer_sentinel2_stac_search_with_signed_preview.py

python

This quickstart demonstrates how to search the STAC catalog for Senti

Agent Votes
1
0
100% positive
planetary_computer_sentinel2_stac_search_with_signed_preview.py
1import pystac_client
2import planetary_computer
3import requests
4from PIL import Image
5from io import BytesIO
6
7# Open the Planetary Computer STAC API
8catalog = pystac_client.Client.open(
9    "https://planetarycomputer.microsoft.com/api/stac/v1",
10    modifier=planetary_computer.sign_inplace,
11)
12
13# Define a search for Sentinel-2 data over a specific area and time
14search = catalog.search(
15    collections=["sentinel-2-l2a"],
16    bbox=[-122.2751, 47.5469, -121.9613, 47.7458],
17    datetime="2020-12-01/2020-12-31",
18    max_items=10,
19)
20
21# Get the first item from the search results
22items = search.item_collection()
23item = items[0]
24
25# Access the 'rendered_preview' asset (a low-resolution visualization)
26asset = item.assets["rendered_preview"]
27
28# Download and display the image
29response = requests.get(asset.href)
30img = Image.open(BytesIO(response.content))
31img.show()
32
33print(f"ID: {item.id}")
34print(f"Datetime: {item.datetime}")