Skip to content

Added support for summaries #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f50541a
added summaries (WIP)
volaya Feb 20, 2021
d2bebb3
Merge branch 'main' of https://github.com/stac-utils/pystac into summ…
volaya May 11, 2021
ad1d252
updated creation of summaries
volaya May 12, 2021
7e6f0fd
fixed imports in tests
volaya May 12, 2021
1e8d474
fixed call in create_summary method
volaya May 12, 2021
968df84
minor changes to summaries
volaya May 12, 2021
86e09e7
collection now do not know now about summaries and summarizers
volaya May 13, 2021
c5ec2c3
some fixes for typing
volaya May 13, 2021
1d133ee
allow to set a limit for elements in list summaries
volaya May 13, 2021
ccdcd91
use extended information for summarize strategies
volaya May 14, 2021
b21b230
renamed SummaryStrategy.UNDEFINED to SummaryStrategy.DEFAULT
volaya May 14, 2021
a7e1747
Autoformat summaries.py
lossyrob May 18, 2021
1bde14e
Remove type parameter from summary methods.
lossyrob May 18, 2021
af126fb
test_case_5 is not a collection; use items from catalog in test
lossyrob May 18, 2021
a2aac4f
Prefer `Dict[str, Any]` type
lossyrob May 18, 2021
1bf5d26
added some tests for summaries and some minor fixes
volaya May 18, 2021
60fe198
Merge remote-tracking branch 'origin/main' into summaries
lossyrob May 18, 2021
09a2a40
Solve Comparable issues
lossyrob May 18, 2021
41045be
Remove __future__.annotations import
lossyrob May 18, 2021
e362712
Remove deprecation warning by using .assertEqual
lossyrob May 18, 2021
018c527
some fixes and minor changes for summaries
volaya May 21, 2021
ec69f89
Merge branch 'summaries' of https://github.com/volaya/pystac into sum…
volaya May 21, 2021
0223b10
flake8 fixes
volaya May 21, 2021
4c76a2b
formatting fix
volaya May 21, 2021
e175aa6
Use Protocol from typing_extensions for pre-3.8 Python
lossyrob May 21, 2021
dc861b0
Merge remote-tracking branch 'origin/main' into summaries
lossyrob May 27, 2021
93438e5
Remove type parameter from RangeSummary.from_dict
lossyrob May 27, 2021
3912937
Type changes to pass mypy tests
lossyrob May 27, 2021
17ab5a1
cache fields definition in summaries
volaya Jun 1, 2021
eb527e1
use fields-normalized.json for default fields definition file
volaya Jun 1, 2021
ce4e682
Merge remote-tracking branch 'stac-utils/main' into summaries
duckontheweb Jun 1, 2021
6857381
Fix CI failures
duckontheweb Jun 1, 2021
fa8767e
added entry to changelog
volaya Jun 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- solar_illumination field in eo extension ([#356](https://github.com/stac-utils/pystac/issues/356))
- Added `Link.canonical` static method for creating links with "canonical" rel type ([#351](https://github.com/stac-utils/pystac/pull/351))
- Added `RelType` enum containing common `rel` values ([#351](https://github.com/stac-utils/pystac/pull/351))
- Added support for summaries ([#264](https://github.com/stac-utils/pystac/pull/264))

### Changed

Expand Down
2 changes: 1 addition & 1 deletion pystac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
TemporalExtent, # type:ignore
Provider, # type:ignore
Summaries, # type:ignore
RangeSummary, # type:ignore
)
from pystac.summaries import RangeSummary # type:ignore
from pystac.item import Item, Asset, CommonMetadata # type:ignore

import pystac.validation
Expand Down
84 changes: 2 additions & 82 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
from typing import (
Any,
Dict,
Generic,
Iterable,
List,
Optional,
TYPE_CHECKING,
Tuple,
Type,
TypeVar,
Union,
cast,
Expand All @@ -24,7 +22,8 @@
from pystac.catalog import Catalog
from pystac.layout import HrefLayoutStrategy
from pystac.link import Link
from pystac.utils import datetime_to_str, get_required
from pystac.utils import datetime_to_str
from pystac.summaries import Summaries

if TYPE_CHECKING:
from pystac.item import Item as Item_Type
Expand Down Expand Up @@ -422,85 +421,6 @@ def from_dict(d: Dict[str, Any]) -> "Provider":
)


class RangeSummary(Generic[T]):
def __init__(self, minimum: T, maximum: T):
self.minimum = minimum
self.maximum = maximum

def to_dict(self) -> Dict[str, Any]:
return {"minimum": self.minimum, "maximum": self.maximum}

@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "RangeSummary[T]":
minimum: T = get_required(d.get("minimum"), "RangeSummary", "minimum")
maximum: T = get_required(d.get("maximum"), "RangeSummary", "maximum")
return cls(minimum=minimum, maximum=maximum)


class Summaries:
def __init__(self, summaries: Dict[str, Any]) -> None:
self._summaries = summaries

self.lists: Dict[str, List[Any]] = {}
self.ranges: Dict[str, RangeSummary[Any]] = {}
self.schemas: Dict[str, Dict[str, Any]] = {}
self.other: Dict[str, Any] = {}

for prop_key, summary in summaries.items():
self.add(prop_key, summary)

def get_list(self, prop: str, typ: Type[T]) -> Optional[List[T]]:
return self.lists.get(prop)

def get_range(self, prop: str, typ: Type[T]) -> Optional[RangeSummary[T]]:
return self.ranges.get(prop)

def get_schema(self, prop: str) -> Optional[Dict[str, Any]]:
return self.schemas.get(prop)

def add(
self,
prop_key: str,
summary: Union[List[Any], RangeSummary[Any], Dict[str, Any]],
) -> None:
if isinstance(summary, list):
self.lists[prop_key] = summary
elif isinstance(summary, dict):
if "minimum" in summary:
self.ranges[prop_key] = RangeSummary[Any].from_dict(
cast(Dict[str, Any], summary)
)
else:
self.schemas[prop_key] = summary
elif isinstance(summary, RangeSummary):
self.ranges[prop_key] = summary
else:
self.other[prop_key] = summary

def remove(self, prop_key: str) -> None:
self.lists.pop(prop_key, None)
self.ranges.pop(prop_key, None)
self.schemas.pop(prop_key, None)
self.other.pop(prop_key, None)

def is_empty(self) -> bool:
return not (
any(self.lists) or any(self.ranges) or any(self.schemas) or any(self.other)
)

def to_dict(self) -> Dict[str, Any]:
return {
**self.lists,
**{k: v.to_dict() for k, v in self.ranges.items()},
**self.schemas,
**self.other,
}

@classmethod
def empty(cls) -> "Summaries":
return Summaries({})


class Collection(Catalog):
"""A Collection extends the Catalog spec with additional metadata that helps
enable discovery.
Expand Down
6 changes: 3 additions & 3 deletions pystac/extensions/eo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

import pystac
from pystac.collection import RangeSummary
from pystac.summaries import RangeSummary
from pystac.extensions.base import (
ExtensionManagementMixin,
PropertiesExtension,
Expand Down Expand Up @@ -453,7 +453,7 @@ def bands(self) -> Optional[List[Band]]:

return map_opt(
lambda bands: [Band(b) for b in bands],
self.summaries.get_list(BANDS_PROP, Dict[str, Any]),
self.summaries.get_list(BANDS_PROP),
)

@bands.setter
Expand All @@ -463,7 +463,7 @@ def bands(self, v: Optional[List[Band]]) -> None:
@property
def cloud_cover(self) -> Optional[RangeSummary[float]]:
"""Get or sets the range of cloud cover from the summary."""
return self.summaries.get_range(CLOUD_COVER_PROP, float)
return self.summaries.get_range(CLOUD_COVER_PROP)

@cloud_cover.setter
def cloud_cover(self, v: Optional[RangeSummary[float]]) -> None:
Expand Down
6 changes: 3 additions & 3 deletions pystac/extensions/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def data_type(self) -> Optional[List[FileDataType]]:

return map_opt(
lambda x: [FileDataType(t) for t in x],
self.summaries.get_list(DATA_TYPE_PROP, str),
self.summaries.get_list(DATA_TYPE_PROP),
)

@data_type.setter
Expand All @@ -205,7 +205,7 @@ def size(self) -> Optional[pystac.RangeSummary[int]]:
Returns:
int or None
"""
return self.summaries.get_range(SIZE_PROP, int)
return self.summaries.get_range(SIZE_PROP)

@size.setter
def size(self, v: Optional[pystac.RangeSummary[int]]) -> None:
Expand All @@ -214,7 +214,7 @@ def size(self, v: Optional[pystac.RangeSummary[int]]) -> None:
@property
def nodata(self) -> Optional[List[Any]]:
"""Get or sets the list of no data values"""
return self.summaries.get_list(NODATA_PROP, List[Any])
return self.summaries.get_list(NODATA_PROP)

@nodata.setter
def nodata(self, v: Optional[List[Any]]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pystac/extensions/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ def bands(self) -> Optional[List[RasterBand]]:
"""
return map_opt(
lambda bands: [RasterBand(b) for b in bands],
self.summaries.get_list(BANDS_PROP, Dict[str, Any]),
self.summaries.get_list(BANDS_PROP),
)

@bands.setter
Expand Down
Loading