Stac

STAC catalog create tutorial

https://pystac.readthedocs.io/en/stable/tutorials/how-to-create-stac-catalogs.html

First, have a file locally. Here is a snippet to download a .tif locally:

wget https://spacenet-dataset.s3.amazonaws.com/spacenet/SN5_roads/train/AOI_7_Moscow/MS/SN5_roads_train_AOI_7_Moscow_MS_chip996.tif

cp SN5_roads_train_AOI_7_Moscow_MS_chip996.tif image.tif

See the spec of a STAC catalog

import pystac


print(pystac.Catalog.__doc__)

Create an empty catalog:

catalog = pystac.Catalog(id="test-catalog", description="Tutorial catalog.")

and show it is empty

print(list(catalog.get_children()))

print(list(catalog.get_items()))

Create an item. This requires 'geometry', 'bbox', 'datetime', and 'properties'

print(pystac.Item.__doc__)

from datetime import datetime


item = pystac.Item(id="image", geometry=None, bbox=None, datetime=datetime.utcnow(), properties={})

add to the catalog

catalog.add_item(item)

check it's been added

item.get_parent()

see the catalog

catalog.describe()

add an item to the asset

print(pystac.Asset.__doc__)


asset = pystac.Asset(href="image.tif")

item.add_asset(key="image", asset=asset)

check the item

import json


print(json.dumps(item.to_dict(), indent=4))

note the link href is null (link in the format of a URL)

catalog.get_self_href()

item.get_self_href()

Provide default hrefs

catalog.normalize_hrefs("stac"))


catalog.get_self_href()

item.get_self_href()

save the catalog with local paths

catalog.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)

check you can read it

with open(catalog.self_href) as f:

print(f.read())

and read the item

with open(item.self_href) as f:

print(f.read())

save the catalog with absolute paths

catalog.save(catalog_type=pystac.CatalogType.ABSOLUTE_PUBLISHED)


with open(item.self_href) as f:

print(f.read())

Read it in

catalog2 = pystac.read_file("stac/catalog.json")

list(catalog2.get_items())