Skip to content

Commit 8b4737e

Browse files
author
Jon Duckworth
authored
Merge pull request #487 from duckontheweb/fix/stac-object-identification
Fix identification of invalid objects with stac_version field
2 parents 94d4426 + 6267c77 commit 8b4737e

File tree

6 files changed

+184
-139
lines changed

6 files changed

+184
-139
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
### Fixed
1717

18+
- Bug in `pystac.serialization.identify_stac_object_type` where invalid objects with
19+
`stac_version == 1.0.0` were incorrectly identified as Catalogs
20+
([#487](https://github.com/stac-utils/pystac/pull/487))
21+
1822
### Removed
1923

2024
- `STAC_IO` class in favor of `StacIO`. This was deprecated in v1.0.0-beta.1 and has

pystac/serialization/identify.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class STACVersionRange:
7878

7979
def __init__(
8080
self,
81-
min_version: Union[str, STACVersionID] = "0.4.0",
81+
min_version: Union[str, STACVersionID] = "0.8.0",
8282
max_version: Optional[Union[str, STACVersionID]] = None,
8383
):
8484
if isinstance(min_version, str):
@@ -180,17 +180,34 @@ def identify_stac_object_type(
180180
Args:
181181
json_dict : The dict of JSON to identify.
182182
"""
183-
# Try to identify using 'type' property, if present
184-
if "type" in json_dict:
185-
# Try to find 'type' property in known STACObjectType values
186-
for t in pystac.STACObjectType:
187-
if json_dict["type"].lower() == t.value.lower():
188-
return t
189-
183+
stac_version = (
184+
STACVersionID(json_dict["stac_version"])
185+
if "stac_version" in json_dict
186+
else None
187+
)
190188
obj_type = json_dict.get("type")
191189

190+
# Try to identify using 'type' property for v1.0.0-rc.1 and higher
191+
introduced_type_attribute = STACVersionID("1.0.0-rc.1")
192+
if stac_version is not None and stac_version >= introduced_type_attribute:
193+
194+
# Since v1.0.0-rc.1 requires a "type" field for all STAC objects, any object
195+
# that is missing this attribute is not a valid STAC object.
196+
if obj_type is None:
197+
return None
198+
199+
# Try to match the "type" attribute
200+
if obj_type == pystac.STACObjectType.CATALOG:
201+
return pystac.STACObjectType.CATALOG
202+
elif obj_type == pystac.STACObjectType.COLLECTION:
203+
return pystac.STACObjectType.COLLECTION
204+
elif obj_type == pystac.STACObjectType.ITEM:
205+
return pystac.STACObjectType.ITEM
206+
else:
207+
return None
208+
192209
# For pre-1.0 objects for version 0.8.* or later 'stac_version' must be present
193-
if "stac_version" in json_dict:
210+
if stac_version is not None:
194211
# Pre-1.0 STAC objects with 'type' == "Feature" are Items
195212
if obj_type == "Feature":
196213
return pystac.STACObjectType.ITEM

pystac/serialization/migrate.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
def _migrate_catalog(
1717
d: Dict[str, Any], version: STACVersionID, info: STACJSONDescription
1818
) -> None:
19-
if version < "0.8":
20-
d["stac_extensions"] = list(info.extensions)
19+
d["type"] = pystac.STACObjectType.CATALOG
2120

2221

2322
def _migrate_collection_summaries(
@@ -35,7 +34,7 @@ def _migrate_collection_summaries(
3534
def _migrate_collection(
3635
d: Dict[str, Any], version: STACVersionID, info: STACJSONDescription
3736
) -> None:
38-
_migrate_catalog(d, version, info)
37+
d["type"] = pystac.STACObjectType.COLLECTION
3938
_migrate_collection_summaries(d, version, info)
4039

4140

pystac/stac_object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class STACObjectType(str, Enum):
1515
def __str__(self) -> str:
1616
return str(self.value)
1717

18-
CATALOG = "CATALOG"
19-
COLLECTION = "COLLECTION"
20-
ITEM = "ITEM"
18+
CATALOG = "Catalog"
19+
COLLECTION = "Collection"
20+
ITEM = "Feature"
2121

2222

2323
class STACObject(ABC):

0 commit comments

Comments
 (0)