Source code for pystac.stac_object

from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Iterable
from html import escape
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    TypeVar,
    cast,
)

import pystac
from pystac import STACError
from pystac.html.jinja_env import get_jinja_env
from pystac.link import Link
from pystac.utils import (
    HREF,
    StringEnum,
    is_absolute_href,
    make_absolute_href,
    make_posix_style,
)

if TYPE_CHECKING:
    from pystac.catalog import Catalog

S = TypeVar("S", bound="STACObject")


[docs]class STACObjectType(StringEnum): CATALOG = "Catalog" COLLECTION = "Collection" ITEM = "Feature"
[docs]class STACObject(ABC): """A base class for other PySTAC classes that contains a variety of useful methods for dealing with links, copying objects, accessing extensions, and reading and writing files. You shouldn't use STACObject directly, but instead access this functionality through the implementing classes. """ id: str """The ID of the STAC Object.""" links: list[Link] """A list of :class:`~pystac.Link` objects representing all links associated with this STAC Object.""" stac_extensions: list[str] """A list of schema URIs for STAC Extensions implemented by this STAC Object.""" STAC_OBJECT_TYPE: STACObjectType _allow_parent_to_override_href: bool = True """Private attribute for whether parent objects should override on normalization""" def __init__(self, stac_extensions: list[str]) -> None: self.links = [] self.stac_extensions = stac_extensions
[docs] def validate( self, validator: pystac.validation.stac_validator.STACValidator | None = None, ) -> list[Any]: """Validate this STACObject. Returns a list of validation results, which depends on the validation implementation. For JSON Schema validation (default validator), this will be a list of schema URIs that were used during validation. Args: validator : A custom validator to use for validation of the object. If omitted, the default validator from :class:`~pystac.validation.RegisteredValidator` will be used instead. Raises: STACValidationError """ import pystac.validation return pystac.validation.validate(self, validator=validator)
[docs] def target_in_hierarchy(self, target: str | STACObject) -> bool: """Determine if target is somewhere in the hierarchical link tree of a STACObject. Args: target: A string or STACObject to search for Returns: bool: Returns True if the target was found in the hierarchical link tree for the current STACObject """ def traverse(obj: str | STACObject, visited: set[str | STACObject]) -> bool: if obj == target: return True if isinstance(obj, str): return False new_targets = [ link.target for link in obj.links if link.is_hierarchical() and link.target not in visited ] if target in new_targets: return True for subtree in new_targets: visited.add(subtree) if traverse(subtree, visited): return True return False return traverse(self, {self})
@property def self_href(self) -> str: """Gets the absolute HREF that is represented by the ``rel == 'self'`` :class:`~pystac.Link`. Raises: ValueError: If the self_href is not set, this method will throw a ValueError. Use get_self_href if there may not be an href set. """ result = self.get_self_href() if result is None: raise ValueError(f"{self} does not have a self_href set.") return result
[docs] def get_self_href(self) -> str | None: """Gets the absolute HREF that is represented by the ``rel == 'self'`` :class:`~pystac.Link`. Returns: str or None: The absolute HREF of this object, or ``None`` if there is no self link defined. Note: A self link can exist for objects, even if the link is not read or written to the JSON-serialized version of the object. Any object read from :func:`STACObject.from_file <pystac.STACObject.from_file>` will have the HREF the file was read from set as it's self HREF. All self links have absolute (as opposed to relative) HREFs. """ self_link = self.get_single_link(pystac.RelType.SELF) if self_link and self_link.has_target_href(): return self_link.get_target_str() else: return None
[docs] def set_self_href(self, href: str | None) -> None: """Sets the absolute HREF that is represented by the ``rel == 'self'`` :class:`~pystac.Link`. Args: href : The absolute HREF of this object. If the given HREF is not absolute, it will be transformed to an absolute HREF based on the current working directory. If this is None the call will clear the self HREF link. """ root_link = self.get_root_link() if root_link is not None and root_link.is_resolved(): cast(pystac.Catalog, root_link.target)._resolved_objects.remove(self) self.remove_links(pystac.RelType.SELF) if href is not None: self.add_link(Link.self_href(href)) if root_link is not None and root_link.is_resolved(): cast(pystac.Catalog, root_link.target)._resolved_objects.cache(self)
[docs] def get_root(self) -> Catalog | None: """Get the :class:`~pystac.Catalog` or :class:`~pystac.Collection` to the root for this object. The root is represented by a :class:`~pystac.Link` with ``rel == 'root'``. Returns: Catalog, Collection, or None: The root object for this object, or ``None`` if no root link is set. """ root_link = self.get_root_link() if root_link: if not root_link.is_resolved(): root_link.resolve_stac_object() # Use set_root, so Catalogs can merge ResolvedObjectCache instances. self.set_root(cast(pystac.Catalog, root_link.target)) return cast(pystac.Catalog, root_link.target) else: return None
[docs] def set_root(self, root: Catalog | None) -> None: """Sets the root :class:`~pystac.Catalog` or :class:`~pystac.Collection` for this object. Args: root : The root object to set. Passing in None will clear the root. """ root_link_index = next( iter( [ i for i, link in enumerate(self.links) if link.rel == pystac.RelType.ROOT ] ), None, ) # Remove from old root resolution cache if root_link_index is not None: root_link = self.links[root_link_index] if root_link.is_resolved(): cast(pystac.Catalog, root_link.target)._resolved_objects.remove(self) if root is None: self.remove_links(pystac.RelType.ROOT) else: new_root_link = Link.root(root) if root_link_index is not None: self.links[root_link_index] = new_root_link new_root_link.set_owner(self) else: self.add_link(new_root_link) root._resolved_objects.cache(self)
[docs] def get_parent(self) -> Catalog | None: """Get the :class:`~pystac.Catalog` or :class:`~pystac.Collection` to the parent for this object. The root is represented by a :class:`~pystac.Link` with ``rel == 'parent'``. Returns: Catalog, Collection, or None: The parent object for this object, or ``None`` if no root link is set. """ parent_link = self.get_single_link(pystac.RelType.PARENT) if parent_link: return cast(pystac.Catalog, parent_link.resolve_stac_object().target) else: return None
[docs] def set_parent(self, parent: Catalog | None) -> None: """Sets the parent :class:`~pystac.Catalog` or :class:`~pystac.Collection` for this object. Args: parent : The parent object to set. Passing in None will clear the parent. """ self.remove_links(pystac.RelType.PARENT) if parent is not None: self.add_link(Link.parent(parent))
[docs] def get_stac_objects( self, rel: str | pystac.RelType, typ: type[STACObject] | None = None, modify_links: Callable[[list[Link]], list[Link]] | None = None, ) -> Iterable[STACObject]: """Gets the :class:`~pystac.STACObject` instances that are linked to by links with their ``rel`` property matching the passed in argument. Args: rel : The relation to match each :class:`~pystac.Link`'s ``rel`` property against. typ : If not ``None``, objects will only be yielded if they are instances of ``typ``. modify_links : A function that modifies the list of links before they are iterated over. For instance, this option can be used to sort the list so that links matching a particular pattern are earlier in the iterator. Returns: Iterable[STACObjects]: A possibly empty iterable of STACObjects that are connected to this object through links with the given ``rel`` and are of type ``typ`` (if given). """ links = self.links[:] if modify_links: links = modify_links(links) for i in range(0, len(links)): link = links[i] if link.rel == rel: link.resolve_stac_object(root=self.get_root()) if typ is None or isinstance(link.target, typ): yield cast(STACObject, link.target)
[docs] def save_object( self, include_self_link: bool = True, dest_href: str | None = None, stac_io: pystac.StacIO | None = None, ) -> None: """Saves this STAC Object to it's 'self' HREF. Args: include_self_link : If this is true, include the 'self' link with this object. Otherwise, leave out the self link. dest_href : Optional HREF to save the file to. If None, the object will be saved to the object's self href. stac_io: Optional instance of StacIO to use. If not provided, will use the instance set on the object's root if available, otherwise will use the default instance. Raises: :class:`~pystac.STACError`: If no self href is set, this error will be raised. Note: When to include a self link is described in the :stac-spec:`Use of Links section of the STAC best practices document <best-practices.md#use-of-links>` """ if stac_io is None: root = self.get_root() if root is not None: root_stac_io = root._stac_io if root_stac_io is not None: stac_io = root_stac_io if stac_io is None: stac_io = pystac.StacIO.default() if dest_href is None: self_href = self.get_self_href() if self_href is None: raise STACError( "Self HREF must be set before saving without an explicit dest_href." ) dest_href = self_href stac_io.save_json(dest_href, self.to_dict(include_self_link=include_self_link))
[docs] def full_copy( self, root: Catalog | None = None, parent: Catalog | None = None, ) -> STACObject: """Create a full copy of this STAC object and any stac objects linked to by this object. Args: root : Optional root to set as the root of the copied object, and any other copies that are contained by this object. parent : Optional parent to set as the parent of the copy of this object. Returns: STACObject: A full copy of this object, as well as any objects this object links to. """ clone = self.clone() if root is None and isinstance(clone, pystac.Catalog): root = clone # Set the root of the STAC Object using the base class, # avoiding child class overrides # extra logic which can be incompatible with the full copy. STACObject.set_root(clone, cast(pystac.Catalog, root)) if parent: clone.set_parent(parent) link_rels = set(self._object_links()) for link in clone.links: if link.rel in link_rels: link.resolve_stac_object() target = cast(STACObject, link.target) if root is not None and target in root._resolved_objects: cached_target = root._resolved_objects.get(target) assert cached_target is not None target = cached_target else: target_parent = None if link.rel in [ pystac.RelType.CHILD, pystac.RelType.ITEM, ] and isinstance(clone, pystac.Catalog): target_parent = clone copied_target = target.full_copy(root=root, parent=target_parent) if root is not None: root._resolved_objects.cache(copied_target) target = copied_target if link.rel in [pystac.RelType.CHILD, pystac.RelType.ITEM]: target.set_root(root) if isinstance(clone, pystac.Catalog): target.set_parent(clone) link.target = target return clone
@abstractmethod def _object_links(self) -> list[str]: """Inherited classes return a list of link 'rel' types that represent STACObjects linked to by this object (not including root, parent or self). This can include optional relations (which may not be present). """ raise NotImplementedError
[docs] @abstractmethod def to_dict( self, include_self_link: bool = True, transform_hrefs: bool = True ) -> dict[str, Any]: """Returns this object as a dictionary. Args: include_self_link : If True, the dict will contain a self link to this object. If False, the self link will be omitted. transform_hrefs: If True, transform the HREF of hierarchical links based on the type of catalog this object belongs to (if any). I.e. if this object belongs to a root catalog that is RELATIVE_PUBLISHED or SELF_CONTAINED, hierarchical link HREFs will be transformed to be relative to the catalog root. dict: A serialization of the object. """ raise NotImplementedError
def _repr_html_(self) -> str: jinja_env = get_jinja_env() if jinja_env: template = jinja_env.get_template("JSON.jinja2") return str(template.render(dict=self.to_dict(transform_hrefs=False))) else: return escape(repr(self))
[docs] @abstractmethod def clone(self) -> STACObject: """Clones this object. Cloning an object will make a copy of all properties and links of the object; however, it will not make copies of the targets of links (i.e. it is not a deep copy). To copy a STACObject fully, with all linked elements also copied, use :func:`STACObject.full_copy <pystac.STACObject.full_copy>`. Returns: STACObject: The clone of this object. """ raise NotImplementedError
[docs] @classmethod def from_file(cls: type[S], href: HREF, stac_io: pystac.StacIO | None = None) -> S: """Reads a STACObject implementation from a file. Args: href : The HREF to read the object from. stac_io: Optional instance of StacIO to use. If not provided, will use the default instance. Returns: The specific STACObject implementation class that is represented by the JSON read from the file located at HREF. """ if cls == STACObject: return cast(S, pystac.read_file(href)) href = make_posix_style(href) if stac_io is None: stac_io = pystac.StacIO.default() if not is_absolute_href(href): href = make_absolute_href(href) d = stac_io.read_json(href) o = cls.from_dict(d, href=href, migrate=True, preserve_dict=False) # If this is a root catalog, set the root to the catalog instance. root_link = o.get_root_link() if root_link is not None: if not root_link.is_resolved(): if root_link.get_absolute_href() == href: o.set_root(cast(pystac.Catalog, o)) return o
[docs] @classmethod @abstractmethod def from_dict( cls: type[S], d: dict[str, Any], href: str | None = None, root: Catalog | None = None, migrate: bool = False, preserve_dict: bool = True, ) -> S: """Parses this STACObject from the passed in dictionary. Args: d : The dict to parse. href : Optional href that is the file location of the object being parsed. root : Optional root catalog for this object. If provided, the root of the returned STACObject will be set to this parameter. migrate: Use True if this dict represents JSON from an older STAC object, so that migrations are run against it. preserve_dict: If False, the dict parameter ``d`` may be modified during this method call. Otherwise the dict is not mutated. Defaults to True, which results results in a deepcopy of the parameter. Set to False when possible to avoid the performance hit of a deepcopy. Returns: STACObject: The STACObject parsed from this dict. """ raise NotImplementedError
[docs] @classmethod @abstractmethod def matches_object_type(cls, d: dict[str, Any]) -> bool: """Returns a boolean indicating whether the given dictionary represents a valid instance of this :class:`~STACObject` sub-class. Args: d : A dictionary to identify """ raise NotImplementedError