|
20 | 20 | from dateutil import tz |
21 | 21 |
|
22 | 22 | import pystac |
23 | | -from pystac import CatalogType, STACObjectType |
| 23 | +from pystac import CatalogType, STACError, STACObjectType |
24 | 24 | from pystac.asset import Asset |
25 | 25 | from pystac.catalog import Catalog |
26 | 26 | from pystac.errors import DeprecatedWarning, ExtensionNotImplemented, STACTypeError |
|
33 | 33 | migrate_to_latest, |
34 | 34 | ) |
35 | 35 | 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 | +) |
37 | 43 |
|
38 | 44 | if TYPE_CHECKING: |
39 | 45 | from pystac.item import Item |
@@ -760,6 +766,42 @@ def delete_asset(self, key: str) -> None: |
760 | 766 |
|
761 | 767 | del self.assets[key] |
762 | 768 |
|
| 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 | + |
763 | 805 | def update_extent_from_items(self) -> None: |
764 | 806 | """ |
765 | 807 | Update datetime and bbox based on all items to a single bbox and time window. |
|
0 commit comments