Skip to main content

Pydantic data models for the STAC spec

Project description

stac-pydantic tests

Pydantic models for STAC Catalogs, Collections, and Items.

Installation

pip install stac-pydantic

Usage

Loading Models

Load data into models with standard pydantic:

from stac_pydantic import Catalog

stac_catalog = {
  "stac_version": "0.9.0",
  "id": "sample",
  "description": "This is a very basic sample catalog.",
  "links": [
    {
      "href": "item.json",
      "rel": "item"
    }
  ]
}

catalog = Catalog(**stac_catalog)
assert catalog.id == "sample"
assert catalog.links[0].href == "item.json"

Extensions

STAC defines many extensions which let the user customize the data in their catalog. Extensions can be validated implicitly or explicitly:

Implicit

The Catalog/Collection/Item will be validated against the extensions listed in the stac_extensions key, if present.

from stac_pydantic import Item

stac_item = {
    "type": "Feature",
    "stac_extensions": [
        "eo"
    ],
    "geometry": ...,
    "properties": {
        "datetime": "2020-03-09T14:53:23.262208+00:00",
        "eo:gsd": 0.15,
        "eo:cloud_cover": 17
    },
    "links": ...,
    "assets": ...,
}

item = Item(**stac_item)

>>> pydantic.error_wrappers.ValidationError: 1 validation error for Item
    __root__ -> properties -> eo:bands
        field required (eo) (type=value_error.missing)

Explicit

You can control which extensions are validated against by explicitly including them in the model. Implicit extensions are validated on top of explicit ones.

from stac_pydantic import Item, ItemProperties, Extensions

class CustomProperties(Extensions.view, ItemProperties):
    ...

class CustomItem(Item):
    properties: CustomProperties # Override properties model

stac_item = {
    "type": "Feature",
    "geometry": ...,
    "properties": {
        "datetime": "2020-03-09T14:53:23.262208+00:00",
        "view:off_nadir": 3.78,
    },
    "links": ...,
    "assets": ...,
}

item = CustomItem(**stac_item)
assert item.properties.off_nadir == 3.78

Vendor Extensions

STAC allows 3rd parties to define their own extensions for specific implementations which aren't currently covered by the available content extensions. You can validate vendor extensions in a similar fashion:

from pydantic import BaseModel
from stac_pydantic import Extensions, Item

# 1. Create a model for the extension
class LandsatExtension(BaseModel):
    row: int
    column: int

    # Setup extension namespace in model config
    class Config:
        allow_population_by_fieldname = True
        alias_generator = lambda field_name: f"landsat:{field_name}"

# 2. Register the extension
Extensions.register("landsat", LandsatExtension)

# 3. Use model as normal
stac_item = {
    "type": "Feature",
    "stac_extensions": [
        "landsat",
        "view"
],
    "geometry": ...,
    "properties": {
        "datetime": "2020-03-09T14:53:23.262208+00:00",
        "view:off_nadir": 3.78,
        "landsat:row": 230,
        "landsat:column": 178 
    },
    "links": ...,
    "assets": ...,
}

item = Item(**stac_item)
assert item.properties.row == 230
assert item.properties.column == 178

Vendor extensions are often defined in stac_extensions as a remote reference to a JSON schema. When registering extensions, you may use the alias kwarg to indicate that the model represents a specific remote reference:

Extensions.register("landsat", LandsatExtension, alias="https://example.com/stac/landsat-extension/1.0/schema.json")

Exporting Models

Most STAC extensions are namespaced with a colon (ex eo:gsd) to keep them distinct from other extensions. Because Python doesn't support the use of colons in variable names, we use Pydantic aliasing to add the namespace upon model export. This requires exporting the model with the by_alias = True parameter. A convenience method (to_dict()) is provided to export models with extension namespaces:

item_dict = item.to_dict()
assert item_dict['properties']['landsat:row'] == item.properties.row == 250

Testing

python setup.py test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stac-pydantic-1.0.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

stac_pydantic-1.0.1-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file stac-pydantic-1.0.1.tar.gz.

File metadata

  • Download URL: stac-pydantic-1.0.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for stac-pydantic-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c1898155f8eb505903514df84da64a6c2c671dec292bfd211d7535488e7505d1
MD5 2e95ac87a6768b081de98d40705c2e6a
BLAKE2b-256 2012b02f113d7bb63db5f94b623776a8fc0a96444ec6996ca1afd2d1d5aa2393

See more details on using hashes here.

Provenance

File details

Details for the file stac_pydantic-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: stac_pydantic-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6

File hashes

Hashes for stac_pydantic-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 194396bf9400c606b8a2d28e58c8114cc60b3385195735aa6c3b7dce9610f3ea
MD5 0bcc4ea0e8b5117807aed1560473ad71
BLAKE2b-256 b54a0c64e04bbad126048d5f839048606b306c052c1a0ff6654c15bbb005b97b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page