Skip to content

Commit

Permalink
Merge pull request #447 from duckontheweb/add/gh-330-summary-migration
Browse files Browse the repository at this point in the history
Add migration for pre-1.0.0-rc.1 Stats Object
  • Loading branch information
Jon Duckworth authored Jun 16, 2021
2 parents e73c570 + e7eb186 commit 12866c4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `ItemCollection` class for working with GeoJSON FeatureCollections containing only
STAC Items ([#430](https://github.com/stac-utils/pystac/pull/430))
- Support for Python 3.9 ([#420](https://github.com/stac-utils/pystac/pull/420))
- Migration for pre-1.0.0-rc.1 Stats Objects (renamed to Range Objects in 1.0.0-rc.3) ([#447](https://github.com/stac-utils/pystac/pull/447))

### Changed

Expand Down
13 changes: 13 additions & 0 deletions pystac/serialization/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,23 @@ def _migrate_catalog(
d["stac_extensions"] = list(info.extensions)


def _migrate_collection_summaries(
d: Dict[str, Any], version: STACVersionID, info: STACJSONDescription
) -> None:
if version < "1.0.0-rc.1":
for prop, summary in d.get("summaries", {}).items():
if isinstance(summary, dict) and "min" in summary and "max" in summary:
d["summaries"][prop] = {
"minimum": summary["min"],
"maximum": summary["max"],
}


def _migrate_collection(
d: Dict[str, Any], version: STACVersionID, info: STACJSONDescription
) -> None:
_migrate_catalog(d, version, info)
_migrate_collection_summaries(d, version, info)


def _migrate_item(
Expand Down
15 changes: 14 additions & 1 deletion tests/serialization/test_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
merge_common_properties,
migrate_to_latest,
)
from pystac.utils import str_to_datetime
from pystac.utils import str_to_datetime, get_required

from tests.utils import TestCases

Expand Down Expand Up @@ -83,3 +83,16 @@ def test_migrates_renamed_extension(self) -> None:

self.assertTrue(ItemAssetsExtension.has_extension(collection))
self.assertIn("item_assets", collection.extra_fields)

def test_migrates_pre_1_0_0_rc1_stats_summary(self) -> None:
collection = pystac.Collection.from_file(
TestCases.get_path(
"data-files/examples/1.0.0-beta.2/collection-spec/"
"examples/sentinel2.json"
)
)
datetime_summary = get_required(
collection.summaries.get_range("datetime"), collection.summaries, "datetime"
)
self.assertEqual(datetime_summary.minimum, "2015-06-23T00:00:00Z")
self.assertEqual(datetime_summary.maximum, "2019-07-10T13:44:56Z")
16 changes: 15 additions & 1 deletion tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pystac.extensions.eo import EOExtension
from pystac.validation import validate_dict
from pystac import Collection, Item, Extent, SpatialExtent, TemporalExtent, CatalogType
from pystac.utils import datetime_to_str
from pystac.utils import datetime_to_str, get_required
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX

TEST_DATETIME = datetime(2020, 3, 14, 16, 32)
Expand Down Expand Up @@ -182,6 +182,20 @@ def test_assets(self) -> None:
collection = pystac.Collection.from_dict(data)
collection.validate()

def test_schema_summary(self) -> None:
collection = pystac.Collection.from_file(
TestCases.get_path(
"data-files/examples/1.0.0/collection-only/collection-with-schemas.json"
)
)
instruments_schema = get_required(
collection.summaries.get_schema("instruments"),
collection.summaries,
"instruments",
)

self.assertIsInstance(instruments_schema, dict)


class ExtentTest(unittest.TestCase):
def test_spatial_allows_single_bbox(self) -> None:
Expand Down

0 comments on commit 12866c4

Please sign in to comment.