Skip to content

Commit 1cfea0c

Browse files
authored
Merge branch 'main' into feature/non-hierarchical-links
2 parents b9e77cf + dd999cd commit 1cfea0c

File tree

21 files changed

+1725
-81
lines changed

21 files changed

+1725
-81
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- `max_items` and `recursive` to `Catalog.validate_all` ([#1141](https://github.com/stac-utils/pystac/pull/1141))
2020
- `KML` as a built in media type ([#1127](https://github.com/stac-utils/pystac/issues/1127))
2121
- `move/copy/delete` operations for local Assets ([#1158](https://github.com/stac-utils/pystac/issues/1158))
22+
- Latest core STAC spec jsonshemas are included in pytstac and used for validation ([#1165](https://github.com/stac-utils/pystac/pull/1165))
2223

2324
### Changed
2425

@@ -39,6 +40,7 @@
3940
- Improved error message when `.ext` is called on a Collection ([#1157](https://github.com/stac-utils/pystac/pull/1157))
4041
- `add_child` and `add_item` return a Link object instead of None ([#1160](https://github.com/stac-utils/pystac/pull/1160))
4142
- `add_children` and `add_items` return a list of Link objects instead of None ([#1160](https://github.com/stac-utils/pystac/pull/1160))
43+
- Include collection assets in `make_all_asset_hrefs_relative/absolute` ([#1168](https://github.com/stac-utils/pystac/pull/1168))
4244

4345
### Deprecated
4446

docs/concepts.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,13 @@ You can validate any :class:`~pystac.Catalog`, :class:`~pystac.Collection` or
394394
395395
item.validate()
396396
397-
This will validate against the latest set of JSON schemas hosted at
398-
https://schemas.stacspec.org, including any extensions that the object extends. If there
399-
are validation errors, a :class:`~pystac.validation.STACValidationError` will be raised.
397+
This validates against the latest set of JSON schemas (which are included with the
398+
PySTAC package) or older versions (which are hosted at https://schemas.stacspec.org).
399+
This validation includes any extensions that the object extends (these are always
400+
accessed remotely based on their URIs).
401+
402+
If there are validation errors, a :class:`~pystac.validation.STACValidationError`
403+
is raised.
400404

401405
You can also call :meth:`~pystac.Catalog.validate_all` on a Catalog or Collection to
402406
recursively walk through a catalog and validate all objects within it.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ test = [
6464
"pytest-mock~=3.10",
6565
"pytest-vcr~=1.0",
6666
"pytest~=7.3",
67-
"ruff==0.0.272",
67+
"ruff==0.0.274",
6868
"types-html5lib~=1.1",
6969
"types-orjson~=3.6",
7070
"types-python-dateutil~=2.8",

pystac/catalog.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -657,20 +657,18 @@ def clone(self) -> Catalog:
657657
return clone
658658

659659
def make_all_asset_hrefs_relative(self) -> None:
660-
"""Makes all the HREFs of assets belonging to items in this catalog
661-
and all children to be relative, recursively.
662-
"""
663-
for _, _, items in self.walk():
664-
for item in items:
665-
item.make_asset_hrefs_relative()
660+
"""Recursively makes all the HREFs of assets in this catalog relative"""
661+
for item in self.get_all_items():
662+
item.make_asset_hrefs_relative()
663+
for collection in self.get_all_collections():
664+
collection.make_asset_hrefs_relative()
666665

667666
def make_all_asset_hrefs_absolute(self) -> None:
668-
"""Makes all the HREFs of assets belonging to items in this catalog
669-
and all children to be absolute, recursively.
670-
"""
671-
for _, _, items in self.walk():
672-
for item in items:
673-
item.make_asset_hrefs_absolute()
667+
"""Recursively makes all the HREFs of assets in this catalog absolute"""
668+
for item in self.get_all_items():
669+
item.make_asset_hrefs_absolute()
670+
for collection in self.get_all_collections():
671+
collection.make_asset_hrefs_absolute()
674672

675673
def normalize_and_save(
676674
self,

pystac/collection.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from dateutil import tz
2121

2222
import pystac
23-
from pystac import CatalogType, STACObjectType
23+
from pystac import CatalogType, STACError, STACObjectType
2424
from pystac.asset import Asset
2525
from pystac.catalog import Catalog
2626
from pystac.errors import DeprecatedWarning, ExtensionNotImplemented, STACTypeError
@@ -33,7 +33,13 @@
3333
migrate_to_latest,
3434
)
3535
from pystac.summaries import Summaries
36-
from pystac.utils import datetime_to_str, str_to_datetime
36+
from pystac.utils import (
37+
datetime_to_str,
38+
is_absolute_href,
39+
make_absolute_href,
40+
make_relative_href,
41+
str_to_datetime,
42+
)
3743

3844
if TYPE_CHECKING:
3945
from pystac.item import Item
@@ -760,6 +766,42 @@ def delete_asset(self, key: str) -> None:
760766

761767
del self.assets[key]
762768

769+
def make_asset_hrefs_relative(self) -> Collection:
770+
"""Modify each asset's HREF to be relative to this collection's self HREF.
771+
772+
Returns:
773+
Collection: self
774+
"""
775+
self_href = self.get_self_href()
776+
for asset in self.assets.values():
777+
if is_absolute_href(asset.href):
778+
if self_href is None:
779+
raise STACError(
780+
"Cannot make asset HREFs relative " "if no self_href is set."
781+
)
782+
asset.href = make_relative_href(asset.href, self_href)
783+
return self
784+
785+
def make_asset_hrefs_absolute(self) -> Collection:
786+
"""Modify each asset's HREF to be absolute.
787+
788+
Any asset HREFs that are relative will be modified to absolute based on this
789+
collection's self HREF.
790+
791+
Returns:
792+
Collection: self
793+
"""
794+
self_href = self.get_self_href()
795+
for asset in self.assets.values():
796+
if not is_absolute_href(asset.href):
797+
if self_href is None:
798+
raise STACError(
799+
"Cannot make relative asset HREFs absolute "
800+
"if no self_href is set."
801+
)
802+
asset.href = make_absolute_href(asset.href, self_href)
803+
return self
804+
763805
def update_extent_from_items(self) -> None:
764806
"""
765807
Update datetime and bbox based on all items to a single bbox and time window.

pystac/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def __init__(
9595
super().__init__(msg)
9696

9797

98+
class STACLocalValidationError(Exception):
99+
"""Schema not available locally"""
100+
101+
98102
class STACValidationError(Exception):
99103
"""Represents a validation error. Thrown by validation calls if the STAC JSON
100104
is invalid.

pystac/item.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,13 @@ def make_asset_hrefs_relative(self) -> Item:
287287
Returns:
288288
Item: self
289289
"""
290-
291-
self_href = None
290+
self_href = self.get_self_href()
292291
for asset in self.assets.values():
293-
href = asset.href
294-
if is_absolute_href(href):
292+
if is_absolute_href(asset.href):
295293
if self_href is None:
296-
self_href = self.get_self_href()
297-
if self_href is None:
298-
raise STACError(
299-
"Cannot make asset HREFs relative "
300-
"if no self_href is set."
301-
)
294+
raise STACError(
295+
"Cannot make asset HREFs relative " "if no self_href is set."
296+
)
302297
asset.href = make_relative_href(asset.href, self_href)
303298
return self
304299

@@ -311,19 +306,15 @@ def make_asset_hrefs_absolute(self) -> Item:
311306
Returns:
312307
Item: self
313308
"""
314-
self_href = None
309+
self_href = self.get_self_href()
315310
for asset in self.assets.values():
316-
href = asset.href
317-
if not is_absolute_href(href):
311+
if not is_absolute_href(asset.href):
318312
if self_href is None:
319-
self_href = self.get_self_href()
320-
if self_href is None:
321-
raise STACError(
322-
"Cannot make relative asset HREFs absolute "
323-
"if no self_href is set."
324-
)
313+
raise STACError(
314+
"Cannot make relative asset HREFs absolute "
315+
"if no self_href is set."
316+
)
325317
asset.href = make_absolute_href(asset.href, self_href)
326-
327318
return self
328319

329320
def set_collection(self, collection: Optional[Collection]) -> Item:

pystac/validation/jsonschemas/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)