|
6 | 6 |
|
7 | 7 | from typing_extensions import deprecated |
8 | 8 |
|
| 9 | +import pystac |
9 | 10 | from pystac.errors import STACError |
10 | 11 | from pystac.media_type import MediaType |
11 | 12 | from pystac.rel_type import RelType |
12 | | -from pystac.utils import make_absolute_href, make_posix_style |
| 13 | +from pystac.utils import is_absolute_href, make_absolute_href, make_posix_style |
13 | 14 |
|
14 | 15 | from .reader import Reader |
15 | 16 |
|
@@ -203,6 +204,34 @@ def to_dict(self, transform_href: bool | None = None) -> dict[str, Any]: |
203 | 204 | data["body"] = self.body |
204 | 205 | return data |
205 | 206 |
|
| 207 | + def resolve_stac_object(self, start_href: str = "") -> Link: |
| 208 | + """Resolves a STAC object from the HREF of this link, if the link is not |
| 209 | + already resolved. |
| 210 | +
|
| 211 | + Args: |
| 212 | + start_href : Optional string to put ahead of the href in this Link. |
| 213 | +
|
| 214 | + NOTE: This uses reader.DEFAULT_READER to read the HREF, if necessary. |
| 215 | + """ |
| 216 | + if self._target: |
| 217 | + return self |
| 218 | + elif self._href: |
| 219 | + # If it's a relative link, base it off the parent. |
| 220 | + target_href = self._href |
| 221 | + if not is_absolute_href(target_href): |
| 222 | + target_href = make_absolute_href(self._href, start_href=start_href) |
| 223 | + try: |
| 224 | + obj = pystac.read_file(target_href) |
| 225 | + except Exception as e: |
| 226 | + raise STACError( |
| 227 | + f"HREF: '{target_href}' does not resolve to a STAC object" |
| 228 | + ) from e |
| 229 | + self._target = obj |
| 230 | + else: |
| 231 | + raise ValueError("Cannot resolve STAC object without a target") |
| 232 | + |
| 233 | + return self |
| 234 | + |
206 | 235 | @override |
207 | 236 | def __repr__(self) -> str: |
208 | 237 | return f"Link(rel={self.rel}, href={self._href})" |
|
0 commit comments