Skip to content

Commit c62f0fb

Browse files
author
Jon Duckworth
authored
Merge pull request #264 from volaya/summaries
Added support for summaries
2 parents c2b7e0e + fa8767e commit c62f0fb

File tree

12 files changed

+1000
-94
lines changed

12 files changed

+1000
-94
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- solar_illumination field in eo extension ([#356](https://github.com/stac-utils/pystac/issues/356))
99
- Added `Link.canonical` static method for creating links with "canonical" rel type ([#351](https://github.com/stac-utils/pystac/pull/351))
1010
- Added `RelType` enum containing common `rel` values ([#351](https://github.com/stac-utils/pystac/pull/351))
11+
- Added support for summaries ([#264](https://github.com/stac-utils/pystac/pull/264))
1112

1213
### Changed
1314

pystac/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
TemporalExtent, # type:ignore
3232
Provider, # type:ignore
3333
Summaries, # type:ignore
34-
RangeSummary, # type:ignore
3534
)
35+
from pystac.summaries import RangeSummary # type:ignore
3636
from pystac.item import Item, Asset, CommonMetadata # type:ignore
3737

3838
import pystac.validation

pystac/collection.py

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
from typing import (
44
Any,
55
Dict,
6-
Generic,
76
Iterable,
87
List,
98
Optional,
109
TYPE_CHECKING,
1110
Tuple,
12-
Type,
1311
TypeVar,
1412
Union,
1513
cast,
@@ -24,7 +22,8 @@
2422
from pystac.catalog import Catalog
2523
from pystac.layout import HrefLayoutStrategy
2624
from pystac.link import Link
27-
from pystac.utils import datetime_to_str, get_required
25+
from pystac.utils import datetime_to_str
26+
from pystac.summaries import Summaries
2827

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

424423

425-
class RangeSummary(Generic[T]):
426-
def __init__(self, minimum: T, maximum: T):
427-
self.minimum = minimum
428-
self.maximum = maximum
429-
430-
def to_dict(self) -> Dict[str, Any]:
431-
return {"minimum": self.minimum, "maximum": self.maximum}
432-
433-
@classmethod
434-
def from_dict(cls, d: Dict[str, Any]) -> "RangeSummary[T]":
435-
minimum: T = get_required(d.get("minimum"), "RangeSummary", "minimum")
436-
maximum: T = get_required(d.get("maximum"), "RangeSummary", "maximum")
437-
return cls(minimum=minimum, maximum=maximum)
438-
439-
440-
class Summaries:
441-
def __init__(self, summaries: Dict[str, Any]) -> None:
442-
self._summaries = summaries
443-
444-
self.lists: Dict[str, List[Any]] = {}
445-
self.ranges: Dict[str, RangeSummary[Any]] = {}
446-
self.schemas: Dict[str, Dict[str, Any]] = {}
447-
self.other: Dict[str, Any] = {}
448-
449-
for prop_key, summary in summaries.items():
450-
self.add(prop_key, summary)
451-
452-
def get_list(self, prop: str, typ: Type[T]) -> Optional[List[T]]:
453-
return self.lists.get(prop)
454-
455-
def get_range(self, prop: str, typ: Type[T]) -> Optional[RangeSummary[T]]:
456-
return self.ranges.get(prop)
457-
458-
def get_schema(self, prop: str) -> Optional[Dict[str, Any]]:
459-
return self.schemas.get(prop)
460-
461-
def add(
462-
self,
463-
prop_key: str,
464-
summary: Union[List[Any], RangeSummary[Any], Dict[str, Any]],
465-
) -> None:
466-
if isinstance(summary, list):
467-
self.lists[prop_key] = summary
468-
elif isinstance(summary, dict):
469-
if "minimum" in summary:
470-
self.ranges[prop_key] = RangeSummary[Any].from_dict(
471-
cast(Dict[str, Any], summary)
472-
)
473-
else:
474-
self.schemas[prop_key] = summary
475-
elif isinstance(summary, RangeSummary):
476-
self.ranges[prop_key] = summary
477-
else:
478-
self.other[prop_key] = summary
479-
480-
def remove(self, prop_key: str) -> None:
481-
self.lists.pop(prop_key, None)
482-
self.ranges.pop(prop_key, None)
483-
self.schemas.pop(prop_key, None)
484-
self.other.pop(prop_key, None)
485-
486-
def is_empty(self) -> bool:
487-
return not (
488-
any(self.lists) or any(self.ranges) or any(self.schemas) or any(self.other)
489-
)
490-
491-
def to_dict(self) -> Dict[str, Any]:
492-
return {
493-
**self.lists,
494-
**{k: v.to_dict() for k, v in self.ranges.items()},
495-
**self.schemas,
496-
**self.other,
497-
}
498-
499-
@classmethod
500-
def empty(cls) -> "Summaries":
501-
return Summaries({})
502-
503-
504424
class Collection(Catalog):
505425
"""A Collection extends the Catalog spec with additional metadata that helps
506426
enable discovery.

pystac/extensions/eo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919

2020
import pystac
21-
from pystac.collection import RangeSummary
21+
from pystac.summaries import RangeSummary
2222
from pystac.extensions.base import (
2323
ExtensionManagementMixin,
2424
PropertiesExtension,
@@ -453,7 +453,7 @@ def bands(self) -> Optional[List[Band]]:
453453

454454
return map_opt(
455455
lambda bands: [Band(b) for b in bands],
456-
self.summaries.get_list(BANDS_PROP, Dict[str, Any]),
456+
self.summaries.get_list(BANDS_PROP),
457457
)
458458

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

468468
@cloud_cover.setter
469469
def cloud_cover(self, v: Optional[RangeSummary[float]]) -> None:

pystac/extensions/file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def data_type(self) -> Optional[List[FileDataType]]:
191191

192192
return map_opt(
193193
lambda x: [FileDataType(t) for t in x],
194-
self.summaries.get_list(DATA_TYPE_PROP, str),
194+
self.summaries.get_list(DATA_TYPE_PROP),
195195
)
196196

197197
@data_type.setter
@@ -205,7 +205,7 @@ def size(self) -> Optional[pystac.RangeSummary[int]]:
205205
Returns:
206206
int or None
207207
"""
208-
return self.summaries.get_range(SIZE_PROP, int)
208+
return self.summaries.get_range(SIZE_PROP)
209209

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

219219
@nodata.setter
220220
def nodata(self, v: Optional[List[Any]]) -> None:

pystac/extensions/raster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def bands(self) -> Optional[List[RasterBand]]:
729729
"""
730730
return map_opt(
731731
lambda bands: [RasterBand(b) for b in bands],
732-
self.summaries.get_list(BANDS_PROP, Dict[str, Any]),
732+
self.summaries.get_list(BANDS_PROP),
733733
)
734734

735735
@bands.setter

0 commit comments

Comments
 (0)