pystac.catalog#

class pystac.catalog.Catalog(id: str, description: str, title: Optional[str] = None, stac_extensions: Optional[List[str]] = None, extra_fields: Optional[Dict[str, Any]] = None, href: Optional[str] = None, catalog_type: pystac.catalog.CatalogType = CatalogType.ABSOLUTE_PUBLISHED)[source]

A PySTAC Catalog represents a STAC catalog in memory.

A Catalog is a STACObject that may contain children, which are instances of Catalog or Collection, as well as Item s.

Parameters
  • id – Identifier for the catalog. Must be unique within the STAC.

  • description – Detailed multi-line description to fully explain the catalog. CommonMark 0.29 syntax MAY be used for rich text representation.

  • title – Optional short descriptive one-line title for the catalog.

  • stac_extensions – Optional list of extensions the Catalog implements.

  • href – Optional HREF for this catalog, which be set as the catalog’s self link’s HREF.

  • catalog_type – Optional catalog type for this catalog. Must be one of the values in CatalogType.

DEFAULT_FILE_NAME = 'catalog.json'

Default file name that will be given to this STAC object in a canonical format.

STAC_OBJECT_TYPE: pystac.stac_object.STACObjectType = 'Catalog'
add_child(child: Union[Catalog, Collection_Type], title: Optional[str] = None, strategy: Optional[pystac.layout.HrefLayoutStrategy] = None) None[source]

Adds a link to a child Catalog or Collection. This method will set the child’s parent to this object, and its root to this Catalog’s root.

Parameters
  • child – The child to add.

  • title – Optional title to give to the Link

  • strategy – The layout strategy to use for setting the self href of the child.

add_children(children: Iterable[Union[Catalog, Collection_Type]]) None[source]

Adds links to multiple Catalog or ~pystac.Collection objects. This method will set each child’s parent to this object, and their root to this Catalog’s root.

Parameters

children – The children to add.

add_item(item: Item_Type, title: Optional[str] = None, strategy: Optional[pystac.layout.HrefLayoutStrategy] = None) None[source]

Adds a link to an Item. This method will set the item’s parent to this object, and its root to this Catalog’s root.

Parameters
  • item – The item to add.

  • title – Optional title to give to the Link

add_items(items: Iterable[Item_Type]) None[source]

Adds links to multiple Item s. This method will set each item’s parent to this object, and their root to this Catalog’s root.

Parameters

items – The items to add.

catalog_type: pystac.catalog.CatalogType

The catalog type. Defaults to CatalogType.ABSOLUTE_PUBLISHED.

clear_children() None[source]

Removes all children from this catalog.

Returns

Returns self

Return type

Catalog

clear_items() None[source]

Removes all items from this catalog.

Returns

Returns self

Return type

Catalog

clone() pystac.catalog.Catalog[source]

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 STACObject.full_copy.

Returns

The clone of this object.

Return type

STACObject

describe(include_hrefs: bool = False, _indent: int = 0) None[source]

Prints out information about this Catalog and all contained STACObjects.

Parameters

include_hrefs (bool) – HREF along with the object ID.

description: str

Detailed multi-line description to fully explain the catalog.

extra_fields: Dict[str, Any]

Extra fields that are part of the top-level JSON properties of the Catalog.

classmethod from_dict(d: Dict[str, Any], href: Optional[str] = None, root: Optional[pystac.catalog.Catalog] = None, migrate: bool = False, preserve_dict: bool = True) pystac.catalog.Catalog[source]

Parses this STACObject from the passed in dictionary.

Parameters
  • 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

The STACObject parsed from this dict.

Return type

STACObject

classmethod from_file(href: str, stac_io: Optional[pystac.stac_io.StacIO] = None) pystac.catalog.Catalog[source]

Reads a STACObject implementation from a file.

Parameters
  • 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.

full_copy(root: Optional[pystac.catalog.Catalog] = None, parent: Optional[pystac.catalog.Catalog] = None) pystac.catalog.Catalog[source]

Create a full copy of this STAC object and any stac objects linked to by this object.

Parameters
  • 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

A full copy of this object, as well as any objects this object

links to.

Return type

STACObject

generate_subcatalogs(template: str, defaults: Optional[Dict[str, Any]] = None, parent_ids: Optional[List[str]] = None) List[pystac.catalog.Catalog][source]

Walks through the catalog and generates subcatalogs for items based on the template string.

See LayoutTemplate for details on the construction of template strings. This template string will be applied to the items, and subcatalogs will be created that separate and organize the items based on template values.

Parameters
  • template – A template string that can be consumed by a LayoutTemplate

  • defaults – Default values for the template variables that will be used if the property cannot be found on the item.

  • parent_ids – Optional list of the parent catalogs’ identifiers. If the bottom-most subcatalogs already match the template, no subcatalog is added.

Returns

List of new catalogs created

Return type

[catalog]

get_all_collections() Iterable[Collection_Type][source]

Get all collections from this catalog and all subcatalogs. Will traverse any subcatalogs recursively.

get_all_items() Iterable[Item_Type][source]

Get all items from this catalog and all subcatalogs. Will traverse any subcatalogs recursively.

Returns

All items that belong to this catalog, and all

catalogs or collections connected to this catalog through child links.

Return type

Generator[Item]

get_child(id: str, recursive: bool = False) Optional[Union[Catalog, Collection_Type]][source]

Gets the child of this catalog with the given ID, if it exists.

Parameters
  • id – The ID of the child to find.

  • recursive – If True, search this catalog and all children for the item; otherwise, only search the children of this catalog. Defaults to False.

Returns

The child with the given ID, or None if not found.

Return type

Optional Catalog or Collection

get_child_links() List[pystac.link.Link][source]

Return all child links of this catalog.

Returns

List of links of this catalog with rel == 'child'

Return type

List[Link]

get_children() Iterable[Union[Catalog, Collection_Type]][source]

Return all children of this catalog.

Returns

Iterable of children who’s parent is this catalog.

Return type

Iterable[Catalog or Collection]

get_collections() Iterable[Collection_Type][source]

Return all children of this catalog that are Collection instances.

get_item(id: str, recursive: bool = False) Optional[Item_Type][source]

Returns an item with a given ID.

Parameters
  • id – The ID of the item to find.

  • recursive – If True, search this catalog and all children for the item; otherwise, only search the items of this catalog. Defaults to False.

Returns

The item with the given ID, or None if not found.

Return type

Item or None

get_item_links() List[pystac.link.Link][source]

Return all item links of this catalog.

Returns

List of links of this catalog with rel == 'item'

Return type

List[Link]

get_items() Iterable[Item_Type][source]

Return all items of this catalog.

Returns

Generator of items whose parent is this catalog.

Return type

Iterable[Item]

id: str

Identifier for the catalog.

is_relative() bool[source]
links: List[pystac.link.Link]

A list of Link objects representing all links associated with this Catalog.

make_all_asset_hrefs_absolute() None[source]

Makes all the HREFs of assets belonging to items in this catalog and all children to be absolute, recursively.

make_all_asset_hrefs_relative() None[source]

Makes all the HREFs of assets belonging to items in this catalog and all children to be relative, recursively.

map_assets(asset_mapper: Callable[[str, Asset_Type], Union[Asset_Type, Tuple[str, Asset_Type], Dict[str, Asset_Type]]]) Catalog[source]

Creates a copy of a catalog, with each Asset for each Item passed through the asset_mapper function.

Parameters

asset_mapper – A function that takes in an key and an Asset, and returns either an Asset, a (key, Asset), or a dictionary of Assets with unique keys. The Asset that is passed into the item_mapper is a copy, so the method can mutate it safely.

Returns

A full copy of this catalog, with assets manipulated according to the asset_mapper function.

Return type

Catalog

map_items(item_mapper: Callable[[Item_Type], Union[Item_Type, List[Item_Type]]]) Catalog[source]

Creates a copy of a catalog, with each item passed through the item_mapper function.

Parameters

item_mapper – A function that takes in an item, and returns either an item or list of items. The item that is passed into the item_mapper is a copy, so the method can mutate it safely.

Returns

A full copy of this catalog, with items manipulated according to the item_mapper function.

Return type

Catalog

classmethod matches_object_type(d: Dict[str, Any]) bool[source]

Returns a boolean indicating whether the given dictionary represents a valid instance of this STACObject sub-class.

Parameters

d – A dictionary to identify

normalize_and_save(root_href: str, catalog_type: Optional[pystac.catalog.CatalogType] = None, strategy: Optional[pystac.layout.HrefLayoutStrategy] = None, stac_io: Optional[pystac.stac_io.StacIO] = None) None[source]

Normalizes link HREFs to the given root_href, and saves the catalog.

This is a convenience method that simply calls Catalog.normalize_hrefs and Catalog.save in sequence.

Parameters
  • root_href – The absolute HREF that all links will be normalized against.

  • catalog_type – The catalog type that dictates the structure of the catalog to save. Use a member of CatalogType. Defaults to the root catalog.catalog_type or the current catalog catalog_type if there is no root catalog.

  • strategy – The layout strategy to use in setting the HREFS for this catalog. Defaults to BestPracticesLayoutStrategy

  • stac_io – Optional instance of StacIO to use. If not provided, will use the instance set while reading in the catalog, or the default instance if this is not available.

normalize_hrefs(root_href: str, strategy: Optional[pystac.layout.HrefLayoutStrategy] = None) None[source]

Normalize HREFs will regenerate all link HREFs based on an absolute root_href and the canonical catalog layout as specified in the STAC specification’s best practices.

This method mutates the entire catalog tree.

Parameters
  • root_href – The absolute HREF that all links will be normalized against.

  • strategy – The layout strategy to use in setting the HREFS for this catalog. Defaults to BestPracticesLayoutStrategy

See:

STAC best practices document for the canonical layout of a STAC.

remove_child(child_id: str) None[source]

Removes an child from this catalog.

Parameters

child_id – The ID of the child to remove.

remove_item(item_id: str) None[source]

Removes an item from this catalog.

Parameters

item_id – The ID of the item to remove.

save(catalog_type: Optional[pystac.catalog.CatalogType] = None, dest_href: Optional[str] = None, stac_io: Optional[pystac.stac_io.StacIO] = None) None[source]

Save this catalog and all it’s children/item to files determined by the object’s self link HREF or a specified path.

Parameters
  • catalog_type – The catalog type that dictates the structure of the catalog to save. Use a member of CatalogType. If not supplied, the catalog_type of this catalog will be used. If that attribute is not set, an exception will be raised.

  • dest_href – The location where the catalog is to be saved. If not supplied, the catalog’s self link HREF is used to determine the location of the catalog file and children’s files.

  • stac_io – Optional instance of StacIO to use. If not provided, will use the instance set while reading in the catalog, or the default instance if this is not available.

Note

If the catalog type is CatalogType.ABSOLUTE_PUBLISHED, all self links will be included, and hierarchical links be absolute URLs. If the catalog type is CatalogType.RELATIVE_PUBLISHED, this catalog’s self link will be included, but no child catalog will have self links, and hierarchical links will be relative URLs If the catalog type is CatalogType.SELF_CONTAINED, no self links will be included and hierarchical links will be relative URLs.

set_root(root: Optional[pystac.catalog.Catalog]) None[source]

Sets the root Catalog or Collection for this object.

Parameters

root – The root object to set. Passing in None will clear the root.

stac_extensions: List[str]

List of extensions the Catalog implements.

title: Optional[str]

Optional short descriptive one-line title for the catalog.

to_dict(include_self_link: bool = True, transform_hrefs: bool = True) Dict[str, Any][source]

Generate a dictionary representing the JSON of this serialized object.

Parameters
  • 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 that can be written out as JSON.

validate_all() None[source]

Validates each catalog, collection contained within this catalog.

Walks through the children and items of the catalog and validates each stac object.

Raises

STACValidationError – Raises this error on any item that is invalid. Will raise on the first invalid stac object encountered.

walk() Iterable[Tuple[Catalog, Iterable[Catalog], Iterable[Item_Type]]][source]

Walks through children and items of catalogs.

For each catalog in the STAC’s tree rooted at this catalog (including this catalog itself), it yields a 3-tuple (root, subcatalogs, items). The root in that 3-tuple refers to the current catalog being walked, the subcatalogs are any catalogs or collections for which the root is a parent, and items represents any items that have the root as a parent.

This has similar functionality to Python’s os.walk().

Returns

A generator that yields a 3-tuple (parent_catalog, children, items).

Return type

Generator[(Catalog, Generator[Catalog], Generator[Item])]

class pystac.catalog.CatalogType(value)[source]

An enumeration.

ABSOLUTE_PUBLISHED = 'ABSOLUTE_PUBLISHED'

Absolute Published Catalog is a catalog that uses absolute links for everything, both in the links objects and in the asset hrefs.

See:

The best practices documentation on published catalogs

RELATIVE_PUBLISHED = 'RELATIVE_PUBLISHED'

Relative Published Catalog is a catalog that uses relative links for everything, but includes an absolute self link at the root catalog, to identify its online location.

See:

The best practices documentation on published catalogs

SELF_CONTAINED = 'SELF_CONTAINED'

A ‘self-contained catalog’ is one that is designed for portability. Users may want to download an online catalog from and be able to use it on their local computer, so all links need to be relative.

See:

The best practices documentation on self-contained catalogs

classmethod determine_type(stac_json: Dict[str, Any]) Optional[pystac.catalog.CatalogType][source]

Determines the catalog type based on a STAC JSON dict.

Only applies to Catalogs or Collections

Parameters

stac_json – The STAC JSON dict to determine the catalog type

Returns

The catalog type of the catalog or collection.

Will return None if it cannot be determined.

Return type

Optional[CatalogType]