<jupyter_start><jupyter_text>How to create STAC Catalogs  STAC Community Sprint, Arlington, November 7th 2019 This notebook runs through some of the basics of using PySTAC to create a static STAC. It was part of a 30 minute presentation at the [community STAC sprint](https://github.com/radiantearth/community-sprints/tree/master/11052019-arlignton-va) in Arlington, VA in November 2019. This tutorial will require the `boto3`, `rasterio`, and `shapely` libraries:<jupyter_code>!pip install boto3
!pip install rasterio
!pip install shapely<jupyter_output>Requirement already satisfied: boto3 in /Users/rob/proj/stac/pystac/venv/lib/python3.6/site-packages (1.10.8)
Requirement already satisfied: botocore<1.14.0,>=1.13.8 in /Users/rob/proj/stac/pystac/venv/lib/python3.6/site-packages (from boto3) (1.13.8)
Requirement already satisfied: s3transfer<0.3.0,>=0.2.0 in /Users/rob/proj/stac/pystac/venv/lib/python3.6/site-packages (from boto3) (0.2.1)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /Users/rob/proj/stac/pystac/venv/lib/python3.6/site-packages (from boto3) (0.9.4)
Requirement already satisfied: urllib3<1.26,>=1.20; python_version >= "3.4" in /Users/rob/proj/stac/pystac/venv/lib/python3.6/site-packages (from botocore<1.14.0,>=1.13.8->boto3) (1.25.6)
Requirement already satisfied: docutils<0.16,>=0.10 in /Users/rob/proj/stac/pystac/venv/lib/python3.6/site-packages (from botocore<1.14.0,>=1.13.8->boto3) (0.15.2)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1; python_version >= "2.7" in /Users/rob/proj/stac/pystac/v[...]<jupyter_text>We can import pystac and access most of the functionality we need with the single import:<jupyter_code>import pystac<jupyter_output><empty_output><jupyter_text>Creating a catalog from a local file To give us some material to work with, lets download a single image from the [Spacenet 5 challenge](https://www.topcoder.com/challenges/30099956). We'll use a temporary directory to save off our single-item STAC.<jupyter_code>import os
import urllib.request
from tempfile import TemporaryDirectory

tmp_dir = TemporaryDirectory()
img_path = os.path.join(tmp_dir.name, 'image.tif')
url = ('http://spacenet-dataset.s3.amazonaws.com/'
       'spacenet/SN5_roads/train/AOI_7_Moscow/MS/'
       'SN5_roads_train_AOI_7_Moscow_MS_chip996.tif')
urllib.request.urlretrieve(url, img_path)<jupyter_output><empty_output><jupyter_text>We want to create a Catalog. Let's check the pydocs for `Catalog` to see what information we'll need. (We use `__doc__` instead of `help()` here to avoid printing out all the docs for the class.)<jupyter_code>print(pystac.Catalog.__doc__)<jupyter_output>A PySTAC Catalog represents a STAC catalog in memory.

    A Catalog is a :class:`~pystac.STACObject` that may contain children,
    which are instances of :class:`~pystac.Catalog` or :class:`~pystac.Collection`,
    as well as :class:`~pystac.Item` s.

    Args:
        id (str): Identifier for the catalog. Must be unique within the STAC.
        description (str): Detailed multi-line description to fully explain the catalog.
            `CommonMark 0.28 syntax <http://commonmark.org/>`_ MAY be used for rich text
            representation.
        title (str or None): Optional short descriptive one-line title for the catalog.
        stac_extensions (List[str]): Optional list of extensions the Catalog implements.
        href (str or None): Optional HREF for this catalog, which be set as the catalog's
            self link's HREF.

    Attributes:
        id (str): Identifier for the catalog.
        description (str): Detailed multi-line description to fully explain the catalog.
   [...]<jupyter_text>Let's just give an ID and a description. We don't have to worry about the HREF right now;  that  will be set later.<jupyter_code>catalog = pystac.Catalog(id='test-catalog', description='Tutorial catalog.')<jupyter_output><empty_output><jupyter_text>There are no children or items in the catalog, since we haven't added anything yet.<jupyter_code>print(list(catalog.get_children()))
print(list(catalog.get_items()))<jupyter_output>[]
[]<jupyter_text>We'll now create an Item to represent the image. Check the pydocs to see what you need to supply:<jupyter_code>print(pystac.Item.__doc__)<jupyter_output>An Item is the core granular entity in a STAC, containing the core metadata
    that enables any client to search or crawl online catalogs of spatial 'assets' -
    satellite imagery, derived data, DEM's, etc.

    Args:
        id (str): Provider identifier. Must be unique within the STAC.
        geometry (dict): Defines the full footprint of the asset represented by this item,
            formatted according to `RFC 7946, section 3.1 (GeoJSON)
            <https://tools.ietf.org/html/rfc7946>`_.
        bbox (List[float] or None):  Bounding Box of the asset represented by this item using
            either 2D or 3D geometries. The length of the array must be 2*n where n is the
            number of dimensions. Could also be None in the case of a null geometry.
        datetime (datetime or None): Datetime associated with this item. If None,
            a start_datetime and end_datetime must be supplied in the properties.
        properties (dict): A dictionary of additional metadata[...]<jupyter_text>Using [rasterio](https://rasterio.readthedocs.io/en/stable/), we can pull out the bounding box of the image to use for the image metadata. If the image contained a NoData border, we would ideally pull out the footprint and save it as the geometry; in this case, we're working with a small chip that most likely has no NoData values.<jupyter_code>import rasterio
from shapely.geometry import Polygon, mapping

def get_bbox_and_footprint(raster_uri):
    with rasterio.open(raster_uri) as ds:
        bounds = ds.bounds
        bbox = [bounds.left, bounds.bottom, bounds.right, bounds.top]
        footprint = Polygon([
            [bounds.left, bounds.bottom],
            [bounds.left, bounds.top],
            [bounds.right, bounds.top],
            [bounds.right, bounds.bottom]
        ])
        
        return (bbox, mapping(footprint))
bbox, footprint = get_bbox_and_footprint(img_path)
print(bbox)
print(footprint)<jupyter_output>[37.6616853489879, 55.73478197572927, 37.66573047610874, 55.73882710285011]
{'type': 'Polygon', 'coordinates': (((37.6616853489879, 55.73478197572927), (37.6616853489879, 55.73882710285011), (37.66573047610874, 55.73882710285011), (37.66573047610874, 55.73478197572927), (37.6616853489879, 55.73478197572927)),)}<jupyter_text>We're also using `datetime.utcnow()` to supply the required datetime property for our Item. Since this is a required property, you might often find yourself making up a time to fill in if you don't know the exact capture time.<jupyter_code>from datetime import datetime

item = pystac.Item(id='local-image',
                 geometry=footprint,
                 bbox=bbox,
                 datetime=datetime.utcnow(),
                 properties={})<jupyter_output><empty_output><jupyter_text>We haven't added it to a catalog yet, so it's parent isn't set. Once we add it to the catalog, we can see it correctly links to it's parent.<jupyter_code>item.get_parent() is None
catalog.add_item(item)
item.get_parent()<jupyter_output><empty_output><jupyter_text>`describe()` is a useful method on `Catalog` - but be careful when using it on large catalogs, as it will walk the entire tree of the STAC.<jupyter_code>catalog.describe()<jupyter_output>* <Catalog id=test-catalog>
  * <Item id=local-image><jupyter_text>Adding AssetsWe've created an Item, but there aren't any assets associated with it. Let's create one:<jupyter_code>print(pystac.Asset.__doc__)
item.add_asset(
    key='image', 
    asset=pystac.Asset(
        href=img_path, 
        media_type=pystac.MediaType.GEOTIFF
    )
)<jupyter_output><empty_output><jupyter_text>At any time we can call `to_dict()` on STAC objects to see how the STAC JSON is shaping up. Notice the asset is now set:<jupyter_code>import json
print(json.dumps(item.to_dict(), indent=4))<jupyter_output>{
    "type": "Feature",
    "stac_version": "1.0.0-beta.2",
    "id": "local-image",
    "properties": {
        "datetime": "2020-08-03T03:47:48.786929Z"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    37.6616853489879,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73478197572927
                ]
            ]
        ]
    },
    "links": [
        {
            "rel": "root",
            "href": null,
            "type": "application/json"
        },
        {
            "rel":[...]<jupyter_text>Note that the link `href` properties are `null`. This is OK, as we're working with the STAC in memory. Next, we'll talk about writing the catalog out, and how to set those HREFs.  Saving the catalog As the JSON above indicates, there's no HREFs set on these in-memory items. PySTAC uses the `self` link on STAC objects to track where the file lives. Because we haven't set them, they evaluate to `None`:<jupyter_code>print(catalog.get_self_href() is None)
print(item.get_self_href() is None)<jupyter_output>True
True<jupyter_text>In order to set them, we can use `normalize_hrefs`. This method will create a normalized set of HREFs for each STAC object in the catalog, according to the [best practices document](https://github.com/radiantearth/stac-spec/blob/v0.8.1/best-practices.mdcatalog-layout)'s recommendations on how to lay out a catalog.<jupyter_code>catalog.normalize_hrefs(os.path.join(tmp_dir.name, 'stac'))<jupyter_output><empty_output><jupyter_text>Now that we've normalized to a root directory (the temporary directory), we see that the `self` links are set:<jupyter_code>print(catalog.get_self_href())
print(item.get_self_href())<jupyter_output>/var/folders/sv/zr8j0t4j1f726nhlt3vb8c300000gn/T/tmpt1wuelid/stac/catalog.json
/var/folders/sv/zr8j0t4j1f726nhlt3vb8c300000gn/T/tmpt1wuelid/stac/local-image/local-image.json<jupyter_text>We can now call `save` on the catalog, which will recursively save all the STAC objects to their respective self HREFs.Save requires a `CatalogType` to be set. You can review the [API docs](https://pystac.readthedocs.io/en/stable/api.htmlcatalogtype) on `CatalogType` to see what each type means (unfortunately `help` doesn't show docstrings for attributes).<jupyter_code>catalog.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)
!ls {tmp_dir.name}/stac/*
with open(catalog.get_self_href()) as f:
    print(f.read())
with open(item.get_self_href()) as f:
    print(f.read())<jupyter_output>{
    "type": "Feature",
    "stac_version": "1.0.0-beta.2",
    "id": "local-image",
    "properties": {
        "datetime": "2020-08-03T03:47:48.786929Z"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    37.6616853489879,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73478197572927
                ]
            ]
        ]
    },
    "links": [
        {
            "rel": "root",
            "href": "../catalog.json",
            "type": "application/json"
        },
        {
     [...]<jupyter_text>As you can see, all links are saved with relative paths. That's because we used `catalog_type=CatalogType.SELF_CONTAINED`. If we save an Absolute Published catalog, we'll see absolute paths:<jupyter_code>catalog.save(catalog_type=pystac.CatalogType.ABSOLUTE_PUBLISHED)<jupyter_output><empty_output><jupyter_text>Now the links included in the STAC item are all absolute:<jupyter_code>with open(item.get_self_href()) as f:
    print(f.read())<jupyter_output>{
    "type": "Feature",
    "stac_version": "1.0.0-beta.2",
    "id": "local-image",
    "properties": {
        "datetime": "2020-08-03T03:47:48.786929Z"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    37.6616853489879,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73478197572927
                ]
            ]
        ]
    },
    "links": [
        {
            "rel": "self",
            "href": "/var/folders/sv/zr8j0t4j1f726nhlt3vb8c300000gn/T/tmpt1wuelid/stac/local-image/local[...]<jupyter_text>Notice that the Asset HREF is absolute in both cases. We can make the Asset HREF relative to the STAC Item by using `.make_all_asset_hrefs_relative()`:<jupyter_code>catalog.make_all_asset_hrefs_relative()
catalog.save(catalog_type=pystac.CatalogType.SELF_CONTAINED)
with open(item.get_self_href()) as f:
    print(f.read())<jupyter_output>{
    "type": "Feature",
    "stac_version": "1.0.0-beta.2",
    "id": "local-image",
    "properties": {
        "datetime": "2020-08-03T03:47:48.786929Z"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [
                    37.6616853489879,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73882710285011
                ],
                [
                    37.66573047610874,
                    55.73478197572927
                ],
                [
                    37.6616853489879,
                    55.73478197572927
                ]
            ]
        ]
    },
    "links": [
        {
            "rel": "root",
            "href": "../catalog.json",
            "type": "application/json"
        },
        {
     [...]<jupyter_text>Creating an Item that implements the EO extensionIn the code above our item only implemented the core STAC Item specification. With [extensions](https://github.com/radiantearth/stac-spec/tree/v0.9.0/extensions) we can record more information and add additional functionality to the Item. Given that we know this is a World View 3 image that has earth observation data, we can enable the [eo extension](https://github.com/radiantearth/stac-spec/tree/v0.8.1/extensions/eo) to add band information. To add eo information to an item we'll need to specify some more data. First, let's define the bands of World View 3:<jupyter_code>from pystac.extensions.eo import Band

# From: https://www.spaceimagingme.com/downloads/sensors/datasheets/DG_WorldView3_DS_2014.pdf

wv3_bands = [Band.create(name='Coastal', description='Coastal: 400 - 450 nm', common_name='coastal'),
             Band.create(name='Blue', description='Blue: 450 - 510 nm', common_name='blue'),
             Band.create(name='Green', description='Green: 510 - 580 nm', common_name='green'),
             Band.create(name='Yellow', description='Yellow: 585 - 625 nm', common_name='yellow'),
             Band.create(name='Red', description='Red: 630 - 690 nm', common_name='red'),
             Band.create(name='Red Edge', description='Red Edge: 705 - 745 nm', common_name='rededge'),
             Band.create(name='Near-IR1', description='Near-IR1: 770 - 895 nm', common_name='nir08'),
             Band.create(name='Near-IR2', description='Near-IR2: 860 - 1040 nm', common_name='nir09')]<jupyter_output><empty_output><jupyter_text>Notice that we used the `.create` method create new band information. We can now create an Item, enable the eo extension, add the band information and add it to our catalog:<jupyter_code>eo_item = pystac.Item(id='local-image-eo',
                      geometry=footprint,
                      bbox=bbox,
                      datetime=datetime.utcnow(),
                      properties={})

eo_item.ext.enable(pystac.Extensions.EO)
eo_item.ext.eo.apply(bands=wv3_bands)<jupyter_output><empty_output><jupyter_text>There are also [common metadata](https://github.com/radiantearth/stac-spec/blob/v0.9.0/item-spec/common-metadata.md) fields that we can use to capture additional information about the WorldView 3 imagery:<jupyter_code>eo_item.common_metadata.platform = "Maxar"
eo_item.common_metadata.instrument="WorldView3"
eo_item.common_metadata.gsd = 0.3
eo_item<jupyter_output><empty_output><jupyter_text>We can use the eo extension to add bands to the assets we add to the item:<jupyter_code>eo_ext = eo_item.ext.eo
help(eo_ext.set_bands)

#eo_item.add_asset(key='image', asset=)
asset = pystac.Asset(href=img_path, 
                     media_type=pystac.MediaType.GEOTIFF)
eo_ext.set_bands(wv3_bands, asset)
eo_item.add_asset("image", asset)<jupyter_output><empty_output><jupyter_text>If we look at the asset's JSON representation, we can see the appropriate band indexes are set:<jupyter_code>asset.to_dict()<jupyter_output><empty_output><jupyter_text>Let's clear the in-memory catalog, add the EO item, and save to a new STAC:<jupyter_code>catalog.clear_items()
list(catalog.get_items())
catalog.add_item(eo_item)
list(catalog.get_items())
catalog.normalize_and_save(root_href=os.path.join(tmp_dir.name, 'stac-eo'), 
                           catalog_type=pystac.CatalogType.SELF_CONTAINED)<jupyter_output><empty_output><jupyter_text>Now, if we read the catalog from the filesystem, PySTAC recognizes that the item implements eo and so use it's functionality, e.g. getting the bands off the asset:<jupyter_code>catalog2 = pystac.read_file(os.path.join(tmp_dir.name, 'stac-eo', 'catalog.json'))
list(catalog2.get_items())
item = next(catalog2.get_all_items())
item.ext.implements('eo')
item.ext.eo.get_bands(item.assets['image'])<jupyter_output><empty_output><jupyter_text>CollectionsCollections are a subtype of Catalog that have some additional properties to make them more searchable. They also can define common properties so that items in the collection don't have to duplicate common data for each item. Let's create a collection to hold common properties between two images from the Spacenet 5 challenge.First we'll get another image, and it's bbox and footprint:<jupyter_code>url2 = ('http://spacenet-dataset.s3.amazonaws.com/'
       'spacenet/SN5_roads/train/AOI_7_Moscow/MS/'
       'SN5_roads_train_AOI_7_Moscow_MS_chip997.tif')
img_path2 = os.path.join(tmp_dir.name, 'image.tif')
urllib.request.urlretrieve(url2, img_path2)
bbox2, footprint2 = get_bbox_and_footprint(img_path2)<jupyter_output><empty_output><jupyter_text>We can take a look at the pydocs for Collection to see what information we need to supply in order to satisfy the spec.<jupyter_code>print(pystac.Collection.__doc__)<jupyter_output>A Collection extends the Catalog spec with additional metadata that helps
    enable discovery.

    Args:
        id (str): Identifier for the collection. Must be unique within the STAC.
        description (str): Detailed multi-line description to fully explain the collection.
            `CommonMark 0.28 syntax <http://commonmark.org/>`_ MAY be used for rich text
            representation.
        extent (Extent): Spatial and temporal extents that describe the bounds of
            all items contained within this Collection.
        title (str or None): Optional short descriptive one-line title for the collection.
        stac_extensions (List[str]): Optional list of extensions the Collection implements.
        href (str or None): Optional HREF for this collection, which be set as the collection's
            self link's HREF.
        license (str):  Collection's license(s) as a `SPDX License identifier
            <https://spdx.org/licenses/>`_, `various`, or `proprietary`. If co[...]<jupyter_text>Beyond what a Catalog reqiures, a Collection requires a license, and an `Extent` that describes the range of space and time that the items it hold occupy.<jupyter_code>print(pystac.Extent.__doc__)<jupyter_output>Describes the spatio-temporal extents of a Collection.

    Args:
        spatial (SpatialExtent): Potential spatial extent covered by the collection.
        temporal (TemporalExtent): Potential temporal extent covered by the collection.

    Attributes:
        spatial (SpatialExtent): Potential spatial extent covered by the collection.
        temporal (TemporalExtent): Potential temporal extent covered by the collection.<jupyter_text>An Extent is comprised of a SpatialExtent and a TemporalExtent. These hold one or more bounding boxes and time intervals, respectively, that completely  cover the items contained in the collections.Let's start with creating two new items - these will be core Items. We can set these items to implement the `eo` extension by specifying them in the `stac_extensions`.<jupyter_code>collection_item = pystac.Item(id='local-image-col-1',
                               geometry=footprint,
                               bbox=bbox,
                               datetime=datetime.utcnow(),
                               properties={},
                               stac_extensions=[pystac.Extensions.EO])

collection_item.common_metadata.gsd = 0.3
collection_item.common_metadata.platform = 'Maxar'
collection_item.common_metadata.instruments = ['WorldView3']

asset = pystac.Asset(href=img_path, 
                      media_type=pystac.MediaType.GEOTIFF)
collection_item.ext.eo.set_bands(wv3_bands, asset)
collection_item.add_asset('image', asset)

collection_item2 = pystac.Item(id='local-image-col-2',
                               geometry=footprint2,
                               bbox=bbox2,
                               datetime=datetime.utcnow(),
                               properties={},
                               stac_extensions=[pystac.Extensions.EO])

collection_item2.common_metadata.gsd = 0.3
collection_item2.common_metadata.platform = 'Maxar'
collection_item2.common_metadata.instruments = ['WorldView3']

asset2 = pystac.Asset(href=img_path,
                     media_type=pystac.MediaType.GEOTIFF)
collection_item2.ext.eo.set_bands([
    band for band in wv3_bands if band.name in ["Red", "Green", "Blue"]
], asset2)

collection_item2.add_asset('image', asset2)<jupyter_output><empty_output><jupyter_text>We can use our two items' metadata to find out what the proper bounds are:<jupyter_code>from shapely.geometry import shape

unioned_footprint = shape(footprint).union(shape(footprint2))
collection_bbox = list(unioned_footprint.bounds)
spatial_extent = pystac.SpatialExtent(bboxes=[collection_bbox])
collection_interval = sorted([collection_item.datetime, collection_item2.datetime])
temporal_extent = pystac.TemporalExtent(intervals=[collection_interval])
collection_extent = pystac.Extent(spatial=spatial_extent, temporal=temporal_extent)
collection = pystac.Collection(id='wv3-images',
                               description='Spacenet 5 images over Moscow',
                               extent=collection_extent,
                               license='CC-BY-SA-4.0')<jupyter_output><empty_output><jupyter_text>Now if we add our items to our Collection, and our Collection to our Catalog, we get the following STAC that can be saved:<jupyter_code>collection.add_items([collection_item, collection_item2])
catalog.clear_items()
catalog.clear_children()
catalog.add_child(collection)
catalog.describe()
catalog.normalize_and_save(root_href=os.path.join(tmp_dir.name, 'stac-collection'), 
                           catalog_type=pystac.CatalogType.SELF_CONTAINED)<jupyter_output><empty_output><jupyter_text>CleanupDon't forget to clean up the temporary directory!<jupyter_code>tmp_dir.cleanup()<jupyter_output><empty_output><jupyter_text>Creating a STAC of imagery from Spacenet 5 data Now, let's take what we've learned and create a Catalog with more data in it.  Allowing PySTAC to read from AWS S3PySTAC aims to be virtually zero-dependency (notwithstanding the why-isn't-this-in-stdlib datetime-util), so it doesn't have the ability to read from or write to anything but the local file system. However, we can hook into PySTAC's IO in the following way. Learn more about how to use STAC_IO in the [documentation on the topic](https://pystac.readthedocs.io/en/latest/concepts.htmlusing-stac-io):<jupyter_code>from urllib.parse import urlparse
import boto3
from pystac import STAC_IO

def my_read_method(uri):
    parsed = urlparse(uri)
    if parsed.scheme == 's3':
        bucket = parsed.netloc
        key = parsed.path[1:]
        s3 = boto3.resource('s3')
        obj = s3.Object(bucket, key)
        return obj.get()['Body'].read().decode('utf-8')
    else:
        return STAC_IO.default_read_text_method(uri)

def my_write_method(uri, txt):
    parsed = urlparse(uri)
    if parsed.scheme == 's3':
        bucket = parsed.netloc
        key = parsed.path[1:]
        s3 = boto3.resource("s3")
        s3.Object(bucket, key).put(Body=txt)
    else:
        STAC_IO.default_write_text_method(uri, txt)

STAC_IO.read_text_method = my_read_method
STAC_IO.write_text_method = my_write_method<jupyter_output><empty_output><jupyter_text>We'll need a utility to list keys for reading the lists of files from S3:<jupyter_code># From https://alexwlchan.net/2017/07/listing-s3-keys/

def get_s3_keys(bucket, prefix):
    """Generate all the keys in an S3 bucket."""
    s3 = boto3.client('s3')
    kwargs = {'Bucket': bucket, 'Prefix': prefix}
    while True:
        resp = s3.list_objects_v2(**kwargs)
        for obj in resp['Contents']:
            yield obj['Key']

        try:
            kwargs['ContinuationToken'] = resp['NextContinuationToken']
        except KeyError:
            break<jupyter_output><empty_output><jupyter_text>Let's make a STAC of imagery over Moscow as part of the Spacenet 5 challenge. As a first step, we can list out the imagery and extract IDs from each of the chips.<jupyter_code>moscow_training_chip_uris = list(get_s3_keys(bucket='spacenet-dataset', 
                                             prefix='spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS'))
import re

chip_id_to_data = {}

def get_chip_id(uri):
    return re.search(r'.*\_chip(\d+)\.', uri).group(1)

for uri in moscow_training_chip_uris:
    chip_id = get_chip_id(uri)
    chip_id_to_data[chip_id] = { 'img': 's3://spacenet-dataset/{}'.format(uri) }<jupyter_output><empty_output><jupyter_text>For this tutorial, we'll only take a subset of the data.<jupyter_code>chip_id_to_data = dict(list(chip_id_to_data.items())[:10])
chip_id_to_data<jupyter_output><empty_output><jupyter_text>Let's turn each of those chips into a STAC Item that represents the image.<jupyter_code>chip_id_to_items = {}<jupyter_output><empty_output><jupyter_text>We'll create core `Item`s for our imagery, but mark them with the `eo` extension as we did above, and store the `eo` data in a `Collection`.Note that the image CRS is in WGS:84 (Lat/Lng). If it wasn't, we'd have to reproject the footprint to WGS:84 in order to be compliant with the spec (which can easily be done with [pyproj](https://github.com/pyproj4/pyproj)).Here we're taking advantage of `rasterio`'s ability to read S3 URIs, which only grabs the GeoTIFF metadata and does not pull the whole file down.<jupyter_code>for chip_id in chip_id_to_data:
    img_uri = chip_id_to_data[chip_id]['img']
    print('Processing {}'.format(img_uri))
    bbox, footprint = get_bbox_and_footprint(img_uri)

    item = pystac.Item(id='img_{}'.format(chip_id), 
                       geometry=footprint,
                       bbox=bbox,
                       datetime=datetime.utcnow(),
                       properties={},
                       stac_extensions=[pystac.Extensions.EO]) 
    
    item.common_metadata.gsd = 0.3
    item.common_metadata.platform = 'Maxar'
    item.common_metadata.instruments = ['WorldView3']
    
    item.ext.eo.bands = wv3_bands
    asset = pystac.Asset(href=img_uri,
                         media_type=pystac.MediaType.COG)
    item.ext.eo.set_bands(wv3_bands, asset)
    item.add_asset(key='ps-ms', asset=asset)
    chip_id_to_items[chip_id] = item<jupyter_output>Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip0.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip1.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip10.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip100.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip1000.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip1001.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip1002.tif
Processing s3://spacenet-dataset/spacenet/SN5_roads/train/AOI_7_Moscow/PS-MS/SN5_roads_train_AOI_7_Moscow_PS-MS_chip1003.tif
Processin[...]<jupyter_text>Creating the CollectionAll of these images are over Moscow. In Spacenet 5, we have a couple cities that have imagery; a good way to separate these collections of imagery. We can store all of the common `eo` metadata in the collection.<jupyter_code>from shapely.geometry import (shape, MultiPolygon)

footprints = list(map(lambda i: shape(i.geometry).envelope, 
                      chip_id_to_items.values()))
collection_bbox = MultiPolygon(footprints).bounds
spatial_extent = pystac.SpatialExtent(bboxes=[collection_bbox])
datetimes = sorted(list(map(lambda i: i.datetime,
                            chip_id_to_items.values())))
temporal_extent = pystac.TemporalExtent(intervals=[[datetimes[0], datetimes[-1]]])
collection_extent = pystac.Extent(spatial=spatial_extent, temporal=temporal_extent)
collection = pystac.Collection(id='wv3-images',
                               description='Spacenet 5 images over Moscow',
                               extent=collection_extent,
                               license='CC-BY-SA-4.0')
collection.add_items(chip_id_to_items.values())
collection.describe()<jupyter_output>* <Collection id=wv3-images>
  * <Item id=img_0>
  * <Item id=img_1>
  * <Item id=img_10>
  * <Item id=img_100>
  * <Item id=img_1000>
  * <Item id=img_1001>
  * <Item id=img_1002>
  * <Item id=img_1003>
  * <Item id=img_1004>
  * <Item id=img_1005><jupyter_text>Now, we can create a Catalog and add the collection.<jupyter_code>catalog = pystac.Catalog(id='spacenet5', description='Spacenet 5 Data (Test)')
catalog.add_child(collection)
catalog.describe()<jupyter_output>* <Catalog id=spacenet5>
    * <Collection id=wv3-images>
      * <Item id=img_0>
      * <Item id=img_1>
      * <Item id=img_10>
      * <Item id=img_100>
      * <Item id=img_1000>
      * <Item id=img_1001>
      * <Item id=img_1002>
      * <Item id=img_1003>
      * <Item id=img_1004>
      * <Item id=img_1005><jupyter_text>Adding items with the label extension to the Spacenet 5 catalogWe can use the [label extension](https://github.com/radiantearth/stac-spec/tree/v0.8.1/extensions/label) of the STAC spec to represent the training data in our STAC. For this, we need to grab the URIs of the GeoJSON of roads:<jupyter_code>moscow_training_geojson_uris = list(get_s3_keys(bucket='spacenet-dataset',
                                               prefix='spacenet/SN5_roads/train/AOI_7_Moscow/geojson_roads_speed/'))
for uri in moscow_training_geojson_uris:
    chip_id = get_chip_id(uri)
    if chip_id in chip_id_to_data:
        chip_id_to_data[chip_id]['label'] = 's3://spacenet-dataset/{}'.format(uri)<jupyter_output><empty_output><jupyter_text>We'll add the items to their own subcatalog; since they don't inherit the Collection's `eo` properties, they shouldn't go in the Collection.<jupyter_code>label_catalog = pystac.Catalog(id='spacenet-data-labels', description='Labels for Spacenet 5')
catalog.add_child(label_catalog)<jupyter_output><empty_output><jupyter_text>To see the required fields for the label extension we can check the pydocs on the `apply` method of the extension:<jupyter_code>from pystac.extensions import label
print(label.LabelItemExt.apply.__doc__)<jupyter_output>Applies label extension properties to the extended Item.

        Args:
            label_description (str): A description of the label, how it was created,
                and what it is recommended for
            label_type (str): An ENUM of either vector label type or raster label type. Use
                one of :class:`~pystac.LabelType`.
            label_properties (list or None): These are the names of the property field(s) in each
                Feature of the label asset's FeatureCollection that contains the classes
                (keywords from label:classes if the property defines classes).
                If labels are rasters, this should be None.
            label_classes (List[LabelClass]): Optional, but reqiured if ussing categorical data.
                A list of LabelClasses defining the list of possible class names for each
                label:properties. (e.g., tree, building, car, hippo)
            label_tasks (List[str]): Recommended to be a subset of 'reg[...]<jupyter_text>This loop creates our label items and associates each to the appropriate source image Item.<jupyter_code>for chip_id in chip_id_to_data:
    img_item = collection.get_item('img_{}'.format(chip_id))
    label_uri = chip_id_to_data[chip_id]['label']
    
    label_item = pystac.Item(id='label_{}'.format(chip_id),
                             geometry=img_item.geometry,
                             bbox=img_item.bbox,
                             datetime=datetime.utcnow(),
                             properties={},
                             stac_extensions=[pystac.Extensions.LABEL])
    label_item.ext.label.apply(label_description="SpaceNet 5 Road labels",
                               label_type=label.LabelType.VECTOR,
                               label_tasks=['segmentation', 'regression'])
    label_item.ext.label.add_source(img_item)
    label_item.ext.label.add_geojson_labels(label_uri)
    
    label_catalog.add_item(label_item)<jupyter_output><empty_output><jupyter_text>Now we have a STAC of training data!<jupyter_code>catalog.describe()
label_item = catalog.get_child('spacenet-data-labels').get_item('label_1')
label_item.to_dict()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10235599143275842, 'fraction_numerical': 0.04162439409311239, 'mean_word_length': 3.123286079479433, 'pattern_counts': {'":': 48, '<': 208, '<?xml version=': 0, '>': 212, 'https://': 15, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '47', 'dir': 'jupyter-structured-clean-dedup', 'id': '202472', 'license': 'Apache-2.0', 'n_tokens_mistral': 12388, 'n_tokens_neox': 11206, 'n_words': 2759, 'path': 'docs/tutorials/how-to-create-stac-catalogs.ipynb', 'repo_name': 'gadomski/pystac', 'size': '35484'}