Skip to content

Commit 314375f

Browse files
committed
Support multiple schema versions following changes in #1091
1 parent bc0fe85 commit 314375f

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
@@ -12,6 +12,7 @@
1212
- `keep_parent` to Catalog `add_item` and `add_child` to avoid overriding existing parents ([#1117](https://github.com/stac-utils/pystac/pull/1117))
1313
- `owner` attribute to `AssetDefinition` in the item-assets extension ([#1110](https://github.com/stac-utils/pystac/pull/1110))
1414
- Updated raster extension to work with the item_assets extension's AssetDefinition objects ([#1110](https://github.com/stac-utils/pystac/pull/1110))
15+
- 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.
1516

1617
### Changed
1718

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})$'"
@@ -171,8 +177,7 @@ def color_hint(self) -> Optional[str]:
171177
@color_hint.setter
172178
def color_hint(self, v: Optional[str]) -> None:
173179
if v is not None:
174-
color_hint_pattern = re.compile("^([0-9A-F]{6})$")
175-
match = color_hint_pattern.match(v)
180+
match = COLOR_HINT_PATTERN.match(v)
176181
assert (
177182
v is None or match is not None and match.group() == v
178183
), "Must format color hints as '^([0-9A-F]{6})$'"
@@ -502,7 +507,11 @@ def _get_bitfields(self) -> Optional[List[Bitfield]]:
502507

503508
@classmethod
504509
def get_schema_uri(cls) -> str:
505-
return SCHEMA_URI
510+
return SCHEMA_URI_PATTERN.format(version=DEFAULT_VERSION)
511+
512+
@classmethod
513+
def get_schema_uris(cls) -> List[str]:
514+
return [SCHEMA_URI_PATTERN.format(version=v) for v in SUPPORTED_VERSIONS]
506515

507516
@classmethod
508517
def ext(cls, obj: T, add_if_missing: bool = False) -> ClassificationExtension[T]:
@@ -634,7 +643,7 @@ def __repr__(self) -> str:
634643

635644

636645
class ClassificationExtensionHooks(ExtensionHooks):
637-
schema_uri: str = SCHEMA_URI
646+
schema_uri: str = SCHEMA_URI_PATTERN.format(version=DEFAULT_VERSION)
638647
prev_extension_ids = {"classification"}
639648
stac_object_types = {pystac.STACObjectType.ITEM}
640649

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)