PySTAC

1.14.3 · active · verified Sat Apr 11

PySTAC is a Python library for working with the SpatioTemporal Asset Catalog (STAC) specification. It provides tools for reading, creating, and modifying STAC Catalogs, Collections, and Items. Currently at version 1.14.3, the library maintains a regular release cadence with updates addressing bug fixes and new features, including support for the latest STAC specification versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic PySTAC Catalog, add an Item with an Asset, and save it to disk as a self-contained catalog. It uses `shapely` for geometry creation and `tempfile` for directory management.

import pystac
from datetime import datetime, timezone
from shapely.geometry import Polygon, mapping
from tempfile import TemporaryDirectory
import os

# 1. Create a temporary directory for the STAC catalog
tmp_dir = TemporaryDirectory()
catalog_dir = os.path.join(tmp_dir.name, 'my_stac_catalog')

# 2. Create a root Catalog
catalog = pystac.Catalog(
    id='example-catalog',
    description='A simple example STAC Catalog for demonstration.'
)

# 3. Create an Item
# Define spatial and temporal extents for the item
bbox = [-10.0, -10.0, 10.0, 10.0]
geometry = mapping(Polygon.from_bounds(*bbox))

# Create a datetime object for the item
dt = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc)

# Create a STAC Item
item = pystac.Item(
    id='example-item-1',
    geometry=geometry,
    bbox=bbox,
    datetime=dt,
    properties={}
)

# Add an asset to the item
asset_href = 'https://example.com/data/image.tif'
item.add_asset(
    key='image',
    asset=pystac.Asset(
        href=asset_href,
        media_type=pystac.MediaType.GEOTIFF,
        roles=['data']
    )
)

# 4. Add the item to the catalog
catalog.add_item(item)

# 5. Normalize HREFs and save the catalog
catalog.normalize_hrefs(catalog_dir)
catalog.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)

print(f"STAC Catalog saved to: {catalog_dir}")

# Clean up the temporary directory (optional)
# tmp_dir.cleanup()

view raw JSON →