Skip to content

Commit

Permalink
Update datacube extension (#1269)
Browse files Browse the repository at this point in the history
* feat: update datacube extension

* chore: update changelog
  • Loading branch information
gadomski authored Oct 23, 2023
1 parent ce21599 commit 10cb05d
Show file tree
Hide file tree
Showing 7 changed files with 1,697 additions and 344 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `validate_all` now accepts a `STACObject` (in addition to accepting a dict, which is now deprecated), but prohibits supplying a value for `href`, which must be supplied _only_ when supplying an object as a dict. Once `validate_all` removes support for an object as a dict, the `href` parameter will also be removed. ([#1246](https://github.com/stac-utils/pystac/pull/1246))
- Report `href` when schema url resolution fails ([#1263](https://github.com/stac-utils/pystac/pull/1263))
- Version extension updated to v1.2.0 ([#1262](https://github.com/stac-utils/pystac/pull/1262))
- Datacube extension updated to v2.2.0 ([#1269](https://github.com/stac-utils/pystac/pull/1269))

### Fixed

Expand Down
83 changes: 77 additions & 6 deletions pystac/extensions/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"T", pystac.Collection, pystac.Item, pystac.Asset, item_assets.AssetDefinition
)

SCHEMA_URI = "https://stac-extensions.github.io/datacube/v2.0.0/schema.json"
SCHEMA_URI = "https://stac-extensions.github.io/datacube/v2.2.0/schema.json"

PREFIX: str = "cube:"
DIMENSIONS_PROP = PREFIX + "dimensions"
Expand Down Expand Up @@ -44,6 +44,7 @@ class DimensionType(StringEnum):
"""Dimension object types for spatial and temporal Dimension Objects."""

SPATIAL = "spatial"
GEOMETRIES = "geometries"
TEMPORAL = "temporal"


Expand Down Expand Up @@ -75,12 +76,16 @@ def __init__(self, properties: dict[str, Any]) -> None:

@property
def dim_type(self) -> DimensionType | str:
"""The type of the dimension. Must be ``"spatial"`` for :stac-ext:`Horizontal
Spatial Dimension Objects <datacube#horizontal-spatial-dimension-object>` or
"""The type of the dimension. Must be ``"spatial"`` for
:stac-ext:`Horizontal Spatial Dimension Objects
<datacube#horizontal-raster-spatial-dimension-object>` or
:stac-ext:`Vertical Spatial Dimension Objects
<datacube#vertical-spatial-dimension-object>`, and ``"temporal"`` for
:stac-ext:`Temporal Dimension Objects <datacube#temporal-dimension-object>`. May
be an arbitrary string for :stac-ext:`Additional Dimension Objects
<datacube#vertical-spatial-dimension-object>`, ``geometries`` for
:stac-ext:`Spatial Vector Dimension Objects
<datacube#spatial-vector-dimension-object>` ``"temporal"`` for
:stac-ext:`Temporal Dimension Objects
<datacube#temporal-dimension-object>`. May be an arbitrary string for
:stac-ext:`Additional Dimension Objects
<datacube#additional-dimension-object>`."""
return get_required(
self.properties.get(DIM_TYPE_PROP), "cube:dimension", DIM_TYPE_PROP
Expand Down Expand Up @@ -119,6 +124,8 @@ def from_dict(d: dict[str, Any]) -> Dimension:
return VerticalSpatialDimension(d)
else:
return HorizontalSpatialDimension(d)
elif dim_type == DimensionType.GEOMETRIES:
return VectorSpatialDimension(d)
elif dim_type == DimensionType.TEMPORAL:
# The v1.0.0 spec says that AdditionalDimensions can have
# type 'temporal', but it is unclear how to differentiate that
Expand Down Expand Up @@ -225,6 +232,68 @@ def unit(self, v: str | None) -> None:
self.properties[DIM_UNIT_PROP] = v


class VectorSpatialDimension(Dimension):
@property
def axes(self) -> list[str] | None:
"""Axes of the vector dimension as an ordered set of `x`, `y` and `z`."""
return self.properties.get("axes")

@axes.setter
def axes(self, v: list[str]) -> None:
if v is None:
self.properties.pop("axes", None)
else:
self.properties["axes"] = v

@property
def bbox(self) -> list[float]:
"""A single bounding box of the geometries as defined for STAC
Collections but not nested."""
return get_required(self.properties.get("bbox"), "cube:bbox", "bbox")

@bbox.setter
def bbox(self, v: list[float]) -> None:
self.properties["bbox"] = v

@property
def values(self) -> list[str] | None:
"""Optionally, a representation of the geometries. This could be a list
of WKT strings or other identifiers."""
return self.properties.get(DIM_VALUES_PROP)

@values.setter
def values(self, v: list[str] | None) -> None:
if v is None:
self.properties.pop(DIM_VALUES_PROP, None)
else:
self.properties[DIM_VALUES_PROP] = v

@property
def geometry_types(self) -> list[str] | None:
"""A set of geometry types. If not present, mixed geometry types must be
assumed."""
return self.properties.get("geometry_types")

@geometry_types.setter
def geometry_types(self, v: list[str] | None) -> None:
if v is None:
self.properties.pop("geometry_types", None)
else:
self.properties["geometry_types"] = v

@property
def reference_system(self) -> str | float | dict[str, Any] | None:
"""The reference system for the data."""
return self.properties.get(DIM_REF_SYS_PROP)

@reference_system.setter
def reference_system(self, v: str | float | dict[str, Any] | None) -> None:
if v is None:
self.properties.pop(DIM_REF_SYS_PROP, None)
else:
self.properties[DIM_REF_SYS_PROP] = v


class TemporalDimension(Dimension):
@property
def extent(self) -> list[str | None] | None:
Expand Down Expand Up @@ -636,6 +705,8 @@ class DatacubeExtensionHooks(ExtensionHooks):
prev_extension_ids = {
"datacube",
"https://stac-extensions.github.io/datacube/v1.0.0/schema.json",
"https://stac-extensions.github.io/datacube/v2.0.0/schema.json",
"https://stac-extensions.github.io/datacube/v2.1.0/schema.json",
}
stac_object_types = {
pystac.STACObjectType.COLLECTION,
Expand Down
18 changes: 9 additions & 9 deletions tests/data-files/datacube/item.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"stac_version": "1.0.0",
"stac_extensions": [
"https://stac-extensions.github.io/datacube/v2.0.0/schema.json"
"https://stac-extensions.github.io/datacube/v2.2.0/schema.json"
],
"id": "datacube-123",
"type": "Feature",
Expand Down Expand Up @@ -100,15 +100,15 @@
},
"cube:variables": {
"temp": {
"dimensions": [
"time",
"y",
"x",
"pressure_levels"
],
"type": "data"
"dimensions": [
"time",
"y",
"x",
"pressure_levels"
],
"type": "data"
}
}
}
},
"assets": {
"data": {
Expand Down
Loading

0 comments on commit 10cb05d

Please sign in to comment.