def from_dict(cls, d, href=None, root=None): d = deepcopy(d) id = d.pop('id') description = d.pop('description') title = d.pop('title', None) stac_extensions = d.pop('stac_extensions', None) links = d.pop('links') d.pop('stac_version') cat = Catalog(id=id, description=description, title=title, stac_extensions=stac_extensions, extra_fields=d) has_self_link = False for link in links: has_self_link |= link['rel'] == 'self' if link['rel'] == 'root': # Remove the link that's generated in Catalog's constructor. cat.remove_links('root') cat.add_link(Link.from_dict(link)) if not has_self_link and href is not None: cat.add_link(Link.self_href(href)) return cat
def from_dict( cls, d: Dict[str, Any], href: Optional[str] = None, root: Optional[Catalog] = None, migrate: bool = False, preserve_dict: bool = True, ) -> "Item": if migrate: info = identify_stac_object(d) d = migrate_to_latest(d, info) if not cls.matches_object_type(d): raise pystac.STACTypeError( f"{d} does not represent a {cls.__name__} instance") if preserve_dict: d = deepcopy(d) id = d.pop("id") geometry = d.pop("geometry") properties = d.pop("properties") bbox = d.pop("bbox", None) stac_extensions = d.get("stac_extensions") collection_id = d.pop("collection", None) datetime = properties.get("datetime") if datetime is not None: datetime = str_to_datetime(datetime) links = d.pop("links") assets = d.pop("assets") d.pop("type") d.pop("stac_version") item = cls( id=id, geometry=geometry, bbox=bbox, datetime=datetime, properties=properties, stac_extensions=stac_extensions, collection=collection_id, extra_fields=d, assets={k: Asset.from_dict(v) for k, v in assets.items()}, ) has_self_link = False for link in links: has_self_link |= link["rel"] == pystac.RelType.SELF item.add_link(Link.from_dict(link)) if not has_self_link and href is not None: item.add_link(Link.self_href(href)) if root: item.set_root(root) return item
def add_link(self, link: Link) -> None: """Add a link to this object's set of links. Args: link : The link to add. """ link.set_owner(self) self.links.append(link)
def __init__( self, 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: CatalogType = CatalogType.ABSOLUTE_PUBLISHED, ): super().__init__(stac_extensions or []) self.id = id self.description = description self.title = title if extra_fields is None: self.extra_fields = {} else: self.extra_fields = extra_fields self._resolved_objects = ResolvedObjectCache() self.add_link(Link.root(self)) if href is not None: self.set_self_href(href) self.catalog_type: CatalogType = catalog_type self._resolved_objects.cache(self)
def from_dict(d): id = d['id'] description = d['description'] license = d['license'] extent = Extent.from_dict(d['extent']) title = d.get('title') stac_extensions = d.get('stac_extensions') keywords = d.get('keywords') version = d.get('version') providers = d.get('providers') if providers is not None: providers = list(map(lambda x: Provider.from_dict(x), providers)) properties = d.get('properties') summaries = d.get('summaries') collection = Collection(id=id, description=description, extent=extent, title=title, license=license, stac_extensions=stac_extensions, keywords=keywords, version=version, providers=providers, properties=properties, summaries=summaries) for l in d['links']: collection.add_link(Link.from_dict(l)) return collection
def from_dict(d): id = d['id'] geometry = d['geometry'] bbox = d['bbox'] properties = d['properties'] stac_extensions = d.get('stac_extensions') datetime = properties.get('datetime') if datetime is None: raise STACError( 'Item dict is missing a "datetime" property in the "properties" field' ) datetime = dateutil.parser.parse(datetime) item = Item(id=id, geometry=geometry, bbox=bbox, datetime=datetime, properties=properties, stac_extensions=stac_extensions) for l in d['links']: item.add_link(Link.from_dict(l)) for k, v in d['assets'].items(): item.assets[k] = Asset.from_dict(v) return item
def from_dict(cls, d, href=None, root=None): id = d['id'] description = d['description'] license = d['license'] extent = Extent.from_dict(d['extent']) title = d.get('title') stac_extensions = d.get('stac_extensions') keywords = d.get('keywords') version = d.get('version') providers = d.get('providers') if providers is not None: providers = list(map(lambda x: Provider.from_dict(x), providers)) properties = d.get('properties') summaries = d.get('summaries') collection = Collection(id=id, description=description, extent=extent, title=title, license=license, stac_extensions=stac_extensions, keywords=keywords, version=version, providers=providers, properties=properties, summaries=summaries) for l in d['links']: if l['rel'] == 'root': # Remove the link that's generated in Catalog's constructor. collection.remove_links('root') collection.add_link(Link.from_dict(l)) return collection
def set_root(self, root: Optional["Catalog_Type"]) -> 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)
def set_collection(self, collection, link_type=None): """Set the collection of this item. This method will replace any existing Collection link and attribute for this item. Args: collection (Collection or None): The collection to set as this item's collection. If None, will clear the collection. link_type (str): the link type to use for the collection link. One of :class:`~pystac.LinkType`. Returns: Item: self """ if not link_type: prev = self.get_single_link('collection') if prev is not None: link_type = prev.link_type else: link_type = LinkType.ABSOLUTE self.remove_links('collection') self.collection_id = None if collection is not None: self.add_link(Link.collection(collection, link_type=link_type)) self.collection_id = collection.id return self
def add_child( self, child: Union["Catalog", "Collection_Type"], title: Optional[str] = None, strategy: Optional[HrefLayoutStrategy] = None, ) -> None: """Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`. This method will set the child's parent to this object, and its root to this Catalog's root. Args: child : The child to add. title : Optional title to give to the :class:`~pystac.Link` strategy : The layout strategy to use for setting the self href of the child. """ # Prevent typo confusion if isinstance(child, pystac.Item): raise pystac.STACError("Cannot add item as child. Use add_item instead.") if strategy is None: strategy = BestPracticesLayoutStrategy() child.set_root(self.get_root()) child.set_parent(self) # set self link self_href = self.get_self_href() if self_href: child_href = strategy.get_href(child, os.path.dirname(self_href)) child.set_self_href(child_href) self.add_link(Link.child(child, title=title))
def add_item( self, item: "Item_Type", title: Optional[str] = None, strategy: Optional[HrefLayoutStrategy] = None, ) -> None: """Adds a link to an :class:`~pystac.Item`. This method will set the item's parent to this object, and its root to this Catalog's root. Args: item : The item to add. title : Optional title to give to the :class:`~pystac.Link` """ # Prevent typo confusion if isinstance(item, pystac.Catalog): raise pystac.STACError("Cannot add catalog as item. Use add_child instead.") if strategy is None: strategy = BestPracticesLayoutStrategy() item.set_root(self.get_root()) item.set_parent(self) # set self link self_href = self.get_self_href() if self_href: item_href = strategy.get_href(item, os.path.dirname(self_href)) item.set_self_href(item_href) self.add_link(Link.item(item, title=title))
def set_root(self, root, link_type=None): """Sets the root :class:`~pystac.Catalog` or :class:`~pystac.Collection` for this object. Args: root (Catalog, Collection or None): The root object to set. Passing in None will clear the root. link_type (str): The link type (see :class:`~pystac.LinkType`) """ # Remove from old root resolution cache root_link = self.get_root_link() if root_link is not None: if root_link.is_resolved(): root_link.target._resolved_objects.remove(self) if not link_type: prev = self.get_single_link('root') if prev is not None: link_type = prev.link_type else: link_type = LinkType.ABSOLUTE self.remove_links('root') if root is not None: self.add_link(Link.root(root, link_type=link_type)) root._resolved_objects.cache(self) return self
def from_dict(cls, d, href=None, root=None): catalog_type = CatalogType.determine_type(d) d = deepcopy(d) id = d.pop('id') description = d.pop('description') title = d.pop('title', None) stac_extensions = d.pop('stac_extensions', None) links = d.pop('links') d.pop('stac_version') cat = Catalog(id=id, description=description, title=title, stac_extensions=stac_extensions, extra_fields=d, href=href, catalog_type=catalog_type) for link in links: if link['rel'] == 'root': # Remove the link that's generated in Catalog's constructor. cat.remove_links('root') if link['rel'] != 'self' or href is None: cat.add_link(Link.from_dict(link)) return cat
def __init__(self, id, description, title=None, stac_extensions=None, extra_fields=None, href=None, catalog_type=None): super().__init__(stac_extensions) self.id = id self.description = description self.title = title if extra_fields is None: self.extra_fields = {} else: self.extra_fields = extra_fields self._resolved_objects = ResolvedObjectCache() self.add_link(Link.root(self)) if href is not None: self.set_self_href(href) self.catalog_type = catalog_type self._resolved_objects.cache(self)
def from_dict(cls, d, href=None, root=None): id = d['id'] geometry = d['geometry'] bbox = d['bbox'] properties = d['properties'] stac_extensions = d.get('stac_extensions') collection_id = None if 'collection' in d.keys(): collection_id = d['collection'] datetime = properties.get('datetime') if datetime is None: raise STACError('Item dict is missing a "datetime" property in the "properties" field') datetime = dateutil.parser.parse(datetime) item = Item(id=id, geometry=geometry, bbox=bbox, datetime=datetime, properties=properties, stac_extensions=stac_extensions, collection=collection_id) for l in d['links']: item.add_link(Link.from_dict(l)) for k, v in d['assets'].items(): asset = Asset.from_dict(v) asset.set_owner(item) item.assets[k] = asset return item
def from_dict(cls, d, href=None, root=None): d = deepcopy(d) id = d.pop('id') description = d.pop('description') license = d.pop('license') extent = Extent.from_dict(d.pop('extent')) title = d.get('title') stac_extensions = d.get('stac_extensions') keywords = d.get('keywords') providers = d.get('providers') if providers is not None: providers = list(map(lambda x: Provider.from_dict(x), providers)) properties = d.get('properties') summaries = d.get('summaries') links = d.pop('links') d.pop('stac_version') collection = Collection(id=id, description=description, extent=extent, title=title, stac_extensions=stac_extensions, extra_fields=d, license=license, keywords=keywords, providers=providers, properties=properties, summaries=summaries) has_self_link = False for link in links: has_self_link |= link['rel'] == 'self' if link['rel'] == 'root': # Remove the link that's generated in Catalog's constructor. collection.remove_links('root') collection.add_link(Link.from_dict(link)) if not has_self_link and href is not None: collection.add_link(Link.self_href(href)) return collection
def add_source(self, source_item, title=None, assets=None): properties = None if assets is not None: properties = {'label:assets': assets} link = Link('source', source_item, title=title, media_type='application/json', properties=properties) self.add_link(link)
def __init__(self, id, description, title=None, href=None): self.id = id self.description = description self.title = title self.links = [Link.root(self)] if href is not None: self.set_self_href(href) self._resolved_objects = ResolvedObjectCache()
def from_dict(cls, d, href=None, root=None): id = d['id'] description = d['description'] title = d.get('title') cat = Catalog(id=id, description=description, title=title) has_self_link = False for link in d['links']: has_self_link |= link['rel'] == 'self' if link['rel'] == 'root': # Remove the link that's generated in Catalog's constructor. cat.remove_links('root') cat.add_link(Link.from_dict(link)) if not has_self_link and href is not None: cat.add_link(Link.self_href(href)) return cat
def from_dict(cls, d, href=None, root=None): d = deepcopy(d) id = d.pop('id') geometry = d.pop('geometry') properties = d.pop('properties') bbox = d.pop('bbox', None) stac_extensions = d.get('stac_extensions') collection_id = d.pop('collection', None) datetime = properties.get('datetime') if datetime is not None: datetime = dateutil.parser.parse(datetime) links = d.pop('links') assets = d.pop('assets') d.pop('type') d.pop('stac_version') item = Item(id=id, geometry=geometry, bbox=bbox, datetime=datetime, properties=properties, stac_extensions=stac_extensions, collection=collection_id, extra_fields=d) has_self_link = False for link in links: has_self_link |= link['rel'] == 'self' item.add_link(Link.from_dict(link)) if not has_self_link and href is not None: item.add_link(Link.self_href(href)) for k, v in assets.items(): asset = Asset.from_dict(v) asset.set_owner(item) item.assets[k] = asset return item
def set_self_href(self, href): """Sets the absolute HREF that is represented by the ``rel == 'self'`` :class:`~pystac.Link`. Args: str: 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. """ self.remove_links('self') self.add_link(Link.self_href(href)) return self
def set_parent(self, parent: Optional["Catalog_Type"]) -> 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))
def from_dict(d): """Constructs an ItemCollection from a dict. Returns: ItemCollection: The ItemCollection deserialized from the JSON dict. """ features = [Item.from_dict(feature) for feature in d['features']] ic = ItemCollection(features) if 'links' in d.keys(): for link in d['links']: ic.add_link(Link.from_dict(link)) return ic
def test_remove_none_doi(self) -> None: """Calling remove_link with doi = None should have no effect.""" link = Link( rel=ScientificRelType.CITE_AS, target="https://some-domain.com/some/paper.pdf", ) links = [link] remove_link(links, doi=None) self.assertEqual(len(links), 1) self.assertIn(link, links)
def __init__(self, id, description, title=None, stac_extensions=None, href=None): self.id = id self.description = description self.title = title self.stac_extensions = stac_extensions self.links = [] self.add_link(Link.root(self)) if href is not None: self.set_self_href(href) self._resolved_objects = ResolvedObjectCache() self._resolved_objects.cache(self)
def from_dict( cls, d: Dict[str, Any], href: Optional[str] = None, root: Optional["Catalog"] = None, migrate: bool = False, preserve_dict: bool = True, ) -> "Catalog": if migrate: info = identify_stac_object(d) d = migrate_to_latest(d, info) if not cls.matches_object_type(d): raise STACTypeError(f"{d} does not represent a {cls.__name__} instance") catalog_type = CatalogType.determine_type(d) if preserve_dict: d = deepcopy(d) id = d.pop("id") description = d.pop("description") title = d.pop("title", None) stac_extensions = d.pop("stac_extensions", None) links = d.pop("links") d.pop("stac_version") cat = cls( id=id, description=description, title=title, stac_extensions=stac_extensions, extra_fields=d, href=href, catalog_type=catalog_type or CatalogType.ABSOLUTE_PUBLISHED, ) for link in links: if link["rel"] == pystac.RelType.ROOT: # Remove the link that's generated in Catalog's constructor. cat.remove_links(pystac.RelType.ROOT) if link["rel"] != pystac.RelType.SELF or href is None: cat.add_link(Link.from_dict(link)) if root: cat.set_root(root) return cat
def from_dict(d): id = d['id'] description = d['description'] title = d.get('title') cat = Catalog(id=id, description=description, title=title) for l in d['links']: if not l['rel'] == 'root': cat.add_link(Link.from_dict(l)) return cat
def from_dict(cls, d, href=None, root=None): id = d['id'] description = d['description'] title = d.get('title') cat = Catalog(id=id, description=description, title=title) for l in d['links']: if l['rel'] == 'root': # Remove the link that's generated in Catalog's constructor. cat.remove_links('root') cat.add_link(Link.from_dict(l)) return cat
def add_child(self, child, title=None): """Adds a link to a child :class:`~pystac.Catalog` or :class:`~pystac.Collection`. This method will set the child's parent to this object, and its root to this Catalog's root. Args: child (Catalog or Collection): The child to add. title (str): Optional title to give to the :class:`~pystac.Link` """ # Prevent typo confusion if isinstance(child, pystac.Item): raise STACError('Cannot add item as child. Use add_item instead.') child.set_root(self.get_root()) child.set_parent(self) self.add_link(Link.child(child, title=title))
def add_item(self, item, title=None): """Adds a link to an :class:`~pystac.Item`. This method will set the item's parent to this object, and its root to this Catalog's root. Args: item (Item): The item to add. title (str): Optional title to give to the :class:`~pystac.Link` """ # Prevent typo confusion if isinstance(item, pystac.Catalog): raise STACError('Cannot add catalog as item. Use add_child instead.') item.set_root(self.get_root()) item.set_parent(self) self.add_link(Link.item(item, title=title))