Using the Microsoft Planetary Computer

Item

Lets get the first item in the list

#get first item
item = items[0]
type(item)
pystac.item.Item

Remember the STAC item is the core object in the catalog and. The item does not contain the data itself, but rather metadata about and links to access the actual data (assets). Some of the metadata:

# print item id and properties
print('id:' , item.id)
item.properties
id: ca_m_3411935_sw_11_060_20200521
{'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
{'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-12-05T20%3A42%3A59Z&se=2023-12-13T20%3A42%3A59Z&sp=rl&sv=2021-06-08&sr=c&skoid=c85c15d6-d1ae-42d4-af60-e2ca0f81359b&sktid=72f988bf-86f1-41af-91ab-2d7cd011db47&skt=2023-12-06T20%3A42%3A58Z&ske=2023-12-13T20%3A42%3A58Z&sks=b&skv=2021-06-08&sig=2uh2KCyXvXpYBfrI0Q14dGwv%2BEyq/rzoEYPZJ2qb788%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-12-05T20%3A42%3A59Z&se=2023-12-13T20%3A42%3A59Z&sp=rl&sv=2021-06-08&sr=c&skoid=c85c15d6-d1ae-42d4-af60-e2ca0f81359b&sktid=72f988bf-86f1-41af-91ab-2d7cd011db47&skt=2023-12-06T20%3A42%3A58Z&ske=2023-12-13T20%3A42%3A58Z&sks=b&skv=2021-06-08&sig=2uh2KCyXvXpYBfrI0Q14dGwv%2BEyq/rzoEYPZJ2qb788%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 0x72f3a2644ad0>

Exercise:

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

  1. Reuse 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. Check the item’s rendered preview asset by clicking on it’s URL.
  4. Open the item’s data using rioxarray.
#Reuse the bbox for Santa Barbara to look for items in this collection.
#Catalog Search:
search_1 = catalog.search(
    collections = ['cop-dem-glo-90'],
    intersects = bbox)
#Check the collection
items_1 = search_1.item_collection()
#Get the first item in the search.
C_DEM = items_1[0]
#check its assets
C_DEM.assets
{'data': <Asset href=https://elevationeuwest.blob.core.windows.net/copernicus-dem/COP90_hh/Copernicus_DSM_COG_30_N34_00_W120_00_DEM.tif?st=2023-12-05T20%3A43%3A51Z&se=2023-12-13T20%3A43%3A52Z&sp=rl&sv=2021-06-08&sr=c&skoid=c85c15d6-d1ae-42d4-af60-e2ca0f81359b&sktid=72f988bf-86f1-41af-91ab-2d7cd011db47&skt=2023-12-06T20%3A43%3A50Z&ske=2023-12-13T20%3A43%3A50Z&sks=b&skv=2021-06-08&sig=0yTIfzgRp2a89msKT6w3F2XfJHiO%2BvCfPi04Zwi6vJQ%3D>,
 'tilejson': <Asset href=https://planetarycomputer.microsoft.com/api/data/v1/item/tilejson.json?collection=cop-dem-glo-90&item=Copernicus_DSM_COG_30_N34_00_W120_00_DEM&assets=data&colormap_name=terrain&rescale=-1000%2C4000&format=png>,
 'rendered_preview': <Asset href=https://planetarycomputer.microsoft.com/api/data/v1/item/preview.png?collection=cop-dem-glo-90&item=Copernicus_DSM_COG_30_N34_00_W120_00_DEM&assets=data&colormap_name=terrain&rescale=-1000%2C4000&format=png>}
for key in C_DEM.assets.keys():
    print(key, '--', C_DEM.assets[key].title)
data -- N34_00_W120_00
tilejson -- TileJSON with default rendering
rendered_preview -- Rendered preview
# Check the item’s rendered preview asset by clicking on it’s URL.
Image(url=C_DEM.assets['rendered_preview'].href, width=500)
#Open the item’s data using rioxarray.
DEM = rioxr.open_rasterio(C_DEM.assets['data'].href)
DEM
<xarray.DataArray (band: 1, y: 1200, x: 1200)>
[1440000 values with dtype=float32]
Coordinates:
  * band         (band) int64 1
  * x            (x) float64 -120.0 -120.0 -120.0 ... -119.0 -119.0 -119.0
  * y            (y) float64 35.0 35.0 35.0 35.0 35.0 ... 34.0 34.0 34.0 34.0
    spatial_ref  int64 0
Attributes:
    AREA_OR_POINT:  Point
    scale_factor:   1.0
    add_offset:     0.0