Skip to content

Commit 90aef2f

Browse files
committed
Support multiple schema versions following changes in #1091
1 parent 87eb1ad commit 90aef2f

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- MGRS Extension ([#1088](https://github.com/stac-utils/pystac/pull/1088))
1111
- All HTTP requests are logged when level is set to `logging.DEBUG` ([#1096](https://github.com/stac-utils/pystac/pull/1096))
1212
- Updated raster extension to work with the item_assets extension's AssetDefinition objects ([#1110](https://github.com/stac-utils/pystac/pull/1110))
13+
- Classification extension ([#1093](https://github.com/stac-utils/pystac/pull/1093)), with support for adding classification information to item_assets' `AssetDefinition`s and raster's `RasterBand` objects.
1314

1415
### Changed
1516

pystac/extensions/classification.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Iterable,
1111
List,
1212
Optional,
13+
Pattern,
1314
TypeVar,
1415
Union,
1516
cast,
@@ -29,14 +30,20 @@
2930

3031
T = TypeVar("T", pystac.Item, pystac.Asset, item_assets.AssetDefinition, RasterBand)
3132

32-
SCHEMA_URI: str = "https://stac-extensions.github.io/classification/v1.0.0/schema.json"
33-
PREFIX: str = "classification:"
33+
SCHEMA_URI_PATTERN: str = (
34+
"https://stac-extensions.github.io/classification/v{version}/schema.json"
35+
)
36+
DEFAULT_VERSION: str = "1.1.0"
37+
SUPPORTED_VERSIONS: List[str] = ["1.1.0", "1.0.0"]
3438

3539
# Field names
40+
PREFIX: str = "classification:"
3641
BITFIELDS_PROP: str = PREFIX + "bitfields"
3742
CLASSES_PROP: str = PREFIX + "classes"
3843
RASTER_BANDS_PROP: str = "raster:bands"
3944

45+
COLOR_HINT_PATTERN: Pattern[str] = re.compile("^([0-9A-Fa-f]{6})$")
46+
4047

4148
class Classification:
4249
"""Represents a single category of a classification.
@@ -73,8 +80,7 @@ def apply(
7380
self.color_hint = color_hint
7481

7582
if color_hint is not None:
76-
color_hint_pattern = re.compile("^([0-9A-F]{6})$")
77-
match = color_hint_pattern.match(color_hint)
83+
match = COLOR_HINT_PATTERN.match(color_hint)
7884
assert (
7985
color_hint is None or match is not None and match.group() == color_hint
8086
), "Must format color hints as '^([0-9A-F]{6})$'"
@@ -164,8 +170,7 @@ def color_hint(self) -> Optional[str]:
164170
@color_hint.setter
165171
def color_hint(self, v: Optional[str]) -> None:
166172
if v is not None:
167-
color_hint_pattern = re.compile("^([0-9A-F]{6})$")
168-
match = color_hint_pattern.match(v)
173+
match = COLOR_HINT_PATTERN.match(v)
169174
assert (
170175
v is None or match is not None and match.group() == v
171176
), "Must format color hints as '^([0-9A-F]{6})$'"
@@ -495,7 +500,11 @@ def _get_bitfields(self) -> Optional[List[Bitfield]]:
495500

496501
@classmethod
497502
def get_schema_uri(cls) -> str:
498-
return SCHEMA_URI
503+
return SCHEMA_URI_PATTERN.format(version=DEFAULT_VERSION)
504+
505+
@classmethod
506+
def get_schema_uris(cls) -> List[str]:
507+
return [SCHEMA_URI_PATTERN.format(version=v) for v in SUPPORTED_VERSIONS]
499508

500509
@classmethod
501510
def ext(cls, obj: T, add_if_missing: bool = False) -> ClassificationExtension[T]:
@@ -627,7 +636,7 @@ def bitfields(self, v: Optional[List[Bitfield]]) -> None:
627636

628637

629638
class ClassificationExtensionHooks(ExtensionHooks):
630-
schema_uri: str = SCHEMA_URI
639+
schema_uri: str = SCHEMA_URI_PATTERN.format(version=DEFAULT_VERSION)
631640
prev_extension_ids = {"classification"}
632641
stac_object_types = {pystac.STACObjectType.ITEM}
633642

tests/extensions/test_classification.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ def test_stac_extensions(landsat_item: Item) -> None:
5656

5757

5858
def test_get_schema_uri(landsat_item: Item) -> None:
59-
assert ClassificationExtension.get_schema_uri() in landsat_item.stac_extensions
59+
assert any(
60+
[
61+
uri in landsat_item.stac_extensions
62+
for uri in ClassificationExtension.get_schema_uris()
63+
]
64+
)
6065

6166

6267
def test_ext_raises_if_item_does_not_conform(plain_item: Item) -> None:
@@ -78,7 +83,8 @@ def test_apply(plain_item: Item) -> None:
7883
)
7984
]
8085
)
81-
# plain_item.validate() ## THIS FAILS
86+
87+
plain_item.validate()
8288
assert (
8389
ClassificationExtension.ext(plain_item).bitfields is not None
8490
and len(cast(List[Bitfield], ClassificationExtension.ext(plain_item).bitfields))

0 commit comments

Comments
 (0)