pystac icon indicating copy to clipboard operation
pystac copied to clipboard

Add_item changes explicitly set links to fit the assumed structure

Open kylemann16 opened this issue 3 years ago • 1 comments

When using the add_item method on a catalog, pystac adjusts links for the dependent object to fit the expected STAC structure even if they're explicitly set and the catalog type is ABSOLUTE_PUBLISHED.

Expected: If explicit links are set and ABSOLUTE_PUBLISHED is the catalog type, they should be kept the same throughout all processes. Actual: Adding items to ABSOLUTE_PUBLISHED catalogs changes explicit links to fit assumed file structure.

Example:

(Pdb) catalog.links
[<Link rel=root target=<Catalog id=Dublin>>, <Link rel=self target=https://{api_endpoint}/product/473/stac_catalog?access_token=>]
(Pdb) item.links
[<Link rel=self target=https://{api_endpoint}/raster/224/stac_item?access_token=>]
(Pdb) catalog.add_item(item)
(Pdb) catalog.links
[<Link rel=root target=<Catalog id=Dublin>>, <Link rel=self target=https://{api_endpoint}/product/473/stac_catalog?access_token=>, <Link rel=item target=<Item id=316000_234500>>]
(Pdb) item.links
[<Link rel=root target=<Catalog id=Dublin>>, <Link rel=parent target=<Catalog id=Dublin>>, <Link rel=self target=https://{api_endpoint}/product/473/316000_234500/316000_234500.json>]
(Pdb) catalog.links[2].href
'https://{api_endpoint}/product/473/316000_234500/316000_234500.json'

The link to the item in the catalog, as well as the self link in the item itself have been changed to mirror the assumed structure despite explicitly setting them to be something else.

The catalog for the above scenario is set as:

        catalog: Catalog = Catalog(
            id=name,
            description=f'{name} Catalog',
            href=href,
            catalog_type='ABSOLUTE_PUBLISHED'
        )

In order to add an explicit link, I have to do

item_link = Link(rel='item', target=item)
parent_link = Link(rel='parent', target=catalog)
root_link = Link(rel='root', target=catalog)

catalog.add_link(item_link)
item.add_link(parent_link)
item.add_link(root_link)
❯ pip show pystac
Name: pystac
Version: 1.6.1
Summary: Python library for working with Spatiotemporal Asset Catalog (STAC).
Home-page: https://github.com/stac-utils/pystac
Author: stac-utils
Author-email: [email protected]
License: Apache Software License 2.0
Location: {location}
Requires: python-dateutil
Required-by: 

Context: I've been implementing some STAC features for a team that wants to avoid writing it to storage for various reasons. What I've had to do is make endpoints on their server's api that generate catalogs and items when hit, and so the self links of items and catalogs needs to be explicitly set to these endpoints and not json files.

kylemann16 avatar Aug 16 '22 15:08 kylemann16

@kylemann16 I've added support for this in #919. From that branch, try:

from pystac.layout import AsIsLayoutStrategy
catalog.add_item(item, strategy=AsIsLayoutStrategy())

gadomski avatar Nov 10 '22 22:11 gadomski