Access

Item

Let’s get the first item in the search:

# get first item in the catalog search
item = items[0]
type(item)
pystac.item.Item

Remember the STAC item is the core object in the catalog.

The item does not contain the data itself, but rather metadata about and links to access the actual data (assets). Some of the metadata:

item.properties
{'gsd': 0.6,
 'datetime': '2020-05-21T00:00:00Z',
 'naip:year': '2020',
 'proj:bbox': [246930.0, 3806808.0, 253260.0, 3814296.0],
 'proj:epsg': 26911,
 'naip:state': 'ca',
 'proj:shape': [12480, 10550],
 'proj:transform': [0.6, 0.0, 246930.0, 0.0, -0.6, 3814296.0, 0.0, 0.0, 1.0]}

Just as the item properties, the item assets are given in a dictionary, with each value being an pystac.asset Let’s check the assets in the item:

item.assets # links to the actual data
{'image': <Asset href=https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/34119/m_3411935_sw_11_060_20200521.tif?st=2023-11-26T21%3A18%3A54Z&se=2023-12-04T21%3A18%3A54Z&sp=rl&sv=2021-06-08&sr=c&skoid=c85c15d6-d1ae-42d4-af60-e2ca0f81359b&sktid=72f988bf-86f1-41af-91ab-2d7cd011db47&skt=2023-11-27T21%3A18%3A53Z&ske=2023-12-04T21%3A18%3A53Z&sks=b&skv=2021-06-08&sig=yOs1LiWCX0hM/gh73pDEEA2ae6DeSdqwRWLQBSvyu3Q%3D>,
 'thumbnail': <Asset href=https://naipeuwest.blob.core.windows.net/naip/v002/ca/2020/ca_060cm_2020/34119/m_3411935_sw_11_060_20200521.200.jpg?st=2023-11-26T21%3A18%3A54Z&se=2023-12-04T21%3A18%3A54Z&sp=rl&sv=2021-06-08&sr=c&skoid=c85c15d6-d1ae-42d4-af60-e2ca0f81359b&sktid=72f988bf-86f1-41af-91ab-2d7cd011db47&skt=2023-11-27T21%3A18%3A53Z&ske=2023-12-04T21%3A18%3A53Z&sks=b&skv=2021-06-08&sig=yOs1LiWCX0hM/gh73pDEEA2ae6DeSdqwRWLQBSvyu3Q%3D>,
 'tilejson': <Asset href=https://planetarycomputer.microsoft.com/api/data/v1/item/tilejson.json?collection=naip&item=ca_m_3411935_sw_11_060_20200521&assets=image&asset_bidx=image%7C1%2C2%2C3&format=png>,
 'rendered_preview': <Asset href=https://planetarycomputer.microsoft.com/api/data/v1/item/preview.png?collection=naip&item=ca_m_3411935_sw_11_060_20200521&assets=image&asset_bidx=image%7C1%2C2%2C3&format=png>}
for key in item.assets.keys():
    print(key, '--', item.assets[key].title)
image -- RGBIR COG tile
thumbnail -- Thumbnail
tilejson -- TileJSON with default rendering
rendered_preview -- Rendered preview

Notice each asset has an href, which is a link to the asset object (i.e. the data). For example, we can use the URL for the rendered preview asset to plot it:

# plot rendered preview
Image(url=item.assets['rendered_preview'].href, width=500)

Load Data

The raster data in our current item is in the image asset. Again, we access this data via its URL. This time, we open it using rioxr.open_rasterio() directly:

sb = rioxr.open_rasterio(item.assets['image'].href)
sb
<xarray.DataArray (band: 4, y: 12480, x: 10550)>
[526656000 values with dtype=uint8]
Coordinates:
  * band         (band) int64 1 2 3 4
  * x            (x) float64 2.469e+05 2.469e+05 ... 2.533e+05 2.533e+05
  * y            (y) float64 3.814e+06 3.814e+06 ... 3.807e+06 3.807e+06
    spatial_ref  int64 0
Attributes:
    AREA_OR_POINT:             Area
    TIFFTAG_IMAGEDESCRIPTION:  OrthoVista
    TIFFTAG_RESOLUTIONUNIT:    1 (unitless)
    TIFFTAG_SOFTWARE:          Trimble Germany GmbH
    TIFFTAG_XRESOLUTION:       1
    TIFFTAG_YRESOLUTION:       1
    _FillValue:                0
    scale_factor:              1.0
    add_offset:                0.0

Notice this raster has four bands. So we cannot use the .plot.imshow() method directly (as this function only works when we have three bands). Thus we need select the bands we want to plot (RGB) before plotting:

# plot raster with correct ratio
size = 6 # height in in of plot height
aspect = sb.rio.width / sb.rio.height 
# select R,G,B bands and plot
sb.sel(band=[1,2,3]).plot.imshow(size=size, aspect=aspect)
<matplotlib.image.AxesImage at 0x7fd129f1c250>

Exercise

The ‘cop-dem-glo-90’ collection contains the Copernicus DEM at 90m resolution (the data we previously used for the Grand Canyon).

Reuse the bbox for Santa Barbara to look for items in this collection. Get the first item in the search and check its assets. Check the item’s rendered preview asset by clicking on it’s URL. Open the item’s data using rioxarray

  1. Use the bbox for Santa Barbara to look for items in this collection/
  2. Get the first item in the search and check its assets
  3. Plot the items rendered preview asset
  4. Open the item’s data using rioaxarray
search = catalog.search(
    collections=['cop-dem-glo-90'],
    intersects=bbox,
    datetime=time_range)
search
<pystac_client.item_search.ItemSearch at 0x7fd1339a5bd0>
sb_item = search.item_collection()
len(items)
1
sb_item
# first item in the catalog and type
type(sb_item[0])
pystac.item.Item
sb_item.assets
AttributeError: 'ItemCollection' object has no attribute 'assets'
Image(url=sb_item.assets['rendered_preview'].href, width=500)
NameError: name 'sb_item' is not defined