Planetary Computer SDK

1.0.0 · active · verified Thu Apr 16

The Planetary Computer SDK for Python (version 1.0.0) provides a client library for interacting with the Microsoft Planetary Computer, a platform that offers petabytes of Earth observation data and computational resources. It simplifies programmatic access to the SpatioTemporal Asset Catalog (STAC) API, handles data authentication by signing Azure Blob Storage URLs with Shared Access Signature (SAS) tokens, and facilitates integration with other open-source geospatial Python libraries. The project maintains an active development status with frequent updates for datasets and features, with new dataset releases typically on a roughly quarterly cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to the Planetary Computer STAC API using `pystac-client`, perform a geospatial and temporal search for a Landsat image, and automatically sign the resulting asset URLs using `planetary_computer.sign_inplace`. It then shows how to open a signed Cloud Optimized GeoTIFF (COG) asset using `rasterio`. An optional environment variable `PC_SDK_SUBSCRIPTION_KEY` can be set for improved rate limits.

import os
import planetary_computer as pc
from pystac_client import Client
import rasterio

# Optionally set your Planetary Computer API subscription key for higher rate limits
# os.environ['PC_SDK_SUBSCRIPTION_KEY'] = os.environ.get('PC_SDK_SUBSCRIPTION_KEY', '')

# Open the Planetary Computer STAC API endpoint
# Using sign_inplace ensures all assets retrieved through this client are automatically signed
catalog = Client.open(
    "https://planetarycomputer.microsoft.com/api/stac/v1",
    modifier=pc.sign_inplace
)

# Define a bounding box and time range for a search (e.g., Landsat 8 image in Seattle area)
bbox = [-122.33, 47.60, -122.00, 47.75]
time_range = "2020-01-01/2020-01-31"

# Search for Landsat C2 L2 items
search = catalog.search(
    collections=["landsat-c2-l2"],
    bbox=bbox,
    datetime=time_range,
    query={'eo:cloud_cover': {'lt': 10}} # Example: filter for low cloud cover
)

# Get the first item from the search results
items = search.item_collection()
if items:
    first_item = items[0]
    print(f"Found item: {first_item.id}")

    # Access a signed asset (e.g., the red band)
    red_band_asset = first_item.assets["SR_B4"]
    signed_href = red_band_asset.href
    print(f"Signed URL for red band: {signed_href[:100]}...")

    # Open the signed asset with rasterio
    try:
        with rasterio.open(signed_href) as ds:
            print(f"Successfully opened asset with shape: {ds.shape}, CRS: {ds.crs}")
            # You can now read data, e.g., ds.read(1)
    except Exception as e:
        print(f"Error opening asset: {e}")
else:
    print("No items found for the given search criteria.")

view raw JSON →