Python: STAC API, rasters and vector data

All public datasets of the Estonian environmental datacube are described in a SpatioTemporal Asset Catalog (STAC) and can be accessed from Python without an account or API key. The STAC API at https://metadata.geokuup.ee/stac/api/v1/ returns the metadata, and every data file is linked as a plain HTTPS address, which the common Python geospatial libraries read directly. This page shows the typical steps with pystac-client (searching the catalogue), rioxarray (raster data) and geopandas (vector data).

The examples were tested with Python 3.13 and need the following packages:

pip install pystac-client rioxarray geopandas

Open the catalogue and list the collections

A collection groups datasets of the same kind (e.g. all Sentinel-2 NDVI composites), and every collection contains items, which in turn link to the actual files (assets). The following example opens the catalogue and prints the identifier and title of every public collection:

from pystac_client import Client

catalog = Client.open("https://metadata.geokuup.ee/stac/api/v1/")

for collection in catalog.get_collections():
    print(collection.id, "-", collection.title)

The same list is available in the STAC Browser, which is often the faster way to find a collection identifier and to look at the descriptions.

Search by area and time

Most raster collections are seasonal composites that cover all of Estonia, for example three seasons per year (spring from 1 April to 31 May, summer from 1 June to 31 August and autumn from 1 September to 31 October) for the vegetation indices. An item therefore has a time range (start_datetime and end_datetime) instead of a single date. The search takes a bounding box in longitude and latitude (WGS84) and a time interval:

tartu = [26.65, 58.35, 26.80, 58.40]  # lon/lat (WGS84): min x, min y, max x, max y

search = catalog.search(
    collections=["estonia-sentinel2-ndvi"],
    bbox=tartu,
    datetime="2024-06-01/2024-08-31",
)
items = list(search.items())

for item in items:
    print(item.id, item.properties["start_datetime"], item.properties["end_datetime"])
    print("  assets:", list(item.assets))

For the summer of 2024 this returns one item, est_s2_ndvi_2024-06-01_2024-08-31, with the assets ndvi_max, ndvi_mean, ndvi_median, ndvi_min, ndvi_range and ndvi_std (the per-pixel statistics of the season).

Read only the area you need

The raster files are Cloud Optimized GeoTIFFs (COG), which store the image in tiles and allow to read a small window over HTTP without downloading the whole file. This matters, because a single Sentinel-2 composite for Estonia at 10 m resolution is close to 900 MB, whereas the Tartu window below is read in a few seconds. The values are stored as integers, and the scale factor to convert them to NDVI is recorded in the STAC metadata of the asset (raster:bands), not in the file itself:

import rioxarray

item = items[0]
asset = item.assets["ndvi_median"]
scale = asset.extra_fields["raster:bands"][0]["scale"]  # 0.001

ndvi = rioxarray.open_rasterio(asset.href, masked=True)
ndvi_tartu = ndvi.rio.clip_box(*tartu, crs="EPSG:4326") * scale

print(ndvi.rio.crs, ndvi.shape, "->", ndvi_tartu.shape)
print("median NDVI:", float(ndvi_tartu.median()))

Most datasets use the Estonian national grid L-EST97 (EPSG:3301), and clip_box transforms the WGS84 bounding box into it. The result is an xarray DataArray, which can be analysed further with xarray or saved as a GeoTIFF (see below).

Vector data

Vector datasets (e.g. the predicted drainage ditches) are stored as FlatGeobuf files, a format that also supports reading only the features inside an area over HTTP. If the area is given as a GeoSeries with a coordinate reference system, geopandas transforms it into the CRS of the file:

import geopandas as gpd
from shapely.geometry import box

vector_items = catalog.search(collections=["estonia-geo-eco-vector-data"]).item_collection()
for vector_item in vector_items:
    print(vector_item.id, vector_item.assets["data"].href)

ditches = vector_items[0]  # estonia_predicted_ditches_50_...
area = gpd.GeoSeries([box(*tartu)], crs="EPSG:4326")

gdf = gpd.read_file(ditches.assets["data"].href, bbox=area)
print(len(gdf), "features,", gdf.crs)

Save the results

Both results can be written to local files and then opened in QGIS or other GIS software:

ndvi_tartu.rio.to_raster("ndvi_median_tartu_2024_summer.tif")
gdf.to_file("ditches_tartu.gpkg")

Without Python: the API directly

The STAC API is a plain JSON web service, so any tool that can send HTTP requests can use it. The following examples use curl and jq to list the collections and to run the same search as above:

curl -s "https://metadata.geokuup.ee/stac/api/v1/collections" | jq -r '.collections[].id'

curl -s -X POST "https://metadata.geokuup.ee/stac/api/v1/search" \
  -H "Content-Type: application/json" \
  -d '{
        "collections": ["estonia-sentinel2-ndvi"],
        "bbox": [26.65, 58.35, 26.80, 58.40],
        "datetime": "2024-06-01T00:00:00Z/2024-08-31T23:59:59Z"
      }' \
  | jq -r '.features[] | .id, .assets.ndvi_median.href'

Every item is a GeoJSON feature with the STAC properties and the links to its files. A shortened example:

{
  "type": "Feature",
  "id": "est_s2_ndvi_2024-06-01_2024-08-31",
  "collection": "estonia-sentinel2-ndvi",
  "properties": {
    "start_datetime": "2024-06-01T00:00:00Z",
    "end_datetime": "2024-08-31T23:59:59Z",
    "proj:code": "EPSG:3301"
  },
  "assets": {
    "ndvi_median": {
      "href": "https://s3.hpc.ut.ee/geokuup/estonia/sentinel2/ndvi/2024/est_s2_ndvi_median_2024-06-01_2024-08-31_cog.tif",
      "type": "image/tiff; application=geotiff; profile=cloud-optimized",
      "raster:bands": [
        {
          "data_type": "int16",
          "nodata": -9999.0,
          "scale": 0.001,
          "spatial_resolution": 10.0
        }
      ]
    }
  }
}

The interactive API description (OpenAPI) at /stac/api/v1/docs lists all endpoints and search parameters.

Licence

The public collections are published under the Creative Commons Attribution 4.0 licence (CC BY 4.0), which is recorded in the license field of every collection (collection.license in pystac). Please credit the data provider named in the collection metadata when you use the data in publications, maps or derived products.