You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following up from this discussion in the STAC Gitter. I created this issue for a discussion on the topic of ~automatically modifying STAC objects read with pystac a la pystac_client's support for automatically modifying results.
It is possible to define a custom StacIO class that overrides the StacIO.stac_object_from_dict method to pass the deserialized STAC object to a modifier function (e.g., planetary_computer.sign_inplace) before returning it:
fromfunctoolsimportwrapsimportplanetary_computerimportpystacfrompystac.stac_ioimportDefaultStacIO, StacIO# Define custom StacIO that overrides stac_object_from_dict to pass StacObject to signing modifierclassPCStacIO(DefaultStacIO):
@wraps(StacIO.stac_object_from_dict)defstac_object_from_dict(
self,
*args,
**kwargs,
) ->pystac.STACObject:
returnplanetary_computer.sign_inplace(
super().stac_object_from_dict(*args, **kwargs)
)
# Usage:item_href="https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/S2B_MSIL2A_20240723T205029_R057_T16XDN_20240723T231445"item=pystac.read_file(
item_href,
stac_io=PCStacIO(), # pass instance of PCStacIO
)
# Asset HREFs have been signedprint(item.assets)
# Alternatively, set PCStacIO as default StacIOStacIO.set_default(PCStacIO)
item=pystac.read_file(item_href)
print(item.assets)
When I wrote the custom StacIO class I naively expected that the pystac.Item.from_file would use the StacIO.stac_object_from_dict method, but that is not the case. Invoking the modifier requires reading the STAC objects with the pystac.read_file method. This is because the pystac.STACObject.from_file methods do not rely on StacIO.stac_object_from_dict (in fact the latter relies on the former).
Thinking "out loud" about how to enable the pystac.STACObject.from_file... add a _modifier class variable that is a Callable[[Modifiable], STACObject] and have the STACObject.from_file methods optionally apply the _modifier if it is set? I don't love it...
That said, it's not clear whether there is any demand for enabling this functionality when using the pystac.read_file or separately and explicitly passing STAC objects to a modifier function approaches work just as well. Using the from_file approach would appease mypy w/o the need for casting, but that's a pretty minor DevX nit tbh. Maybe this example is informative enough for the docs?
All feedback is welcome.
The text was updated successfully, but these errors were encountered:
separately and explicitly passing STAC objects to a modifier function approaches work just as well.
As things currently exist, this is my instinct. For a (currently hypothetical) PySTAC v2, it's likely that the whole StacIO mechanism will get a significant refactor (see #675, #506), and auto-magically transforming objects (e.g. by signing assets) could be a good feature to include. I'd like to keep this issue open to track that need 👍🏼.
Following up from this discussion in the STAC Gitter. I created this issue for a discussion on the topic of ~automatically modifying STAC objects read with
pystac
a lapystac_client
's support for automatically modifying results.It is possible to define a custom
StacIO
class that overrides theStacIO.stac_object_from_dict
method to pass the deserialized STAC object to a modifier function (e.g.,planetary_computer.sign_inplace
) before returning it:When I wrote the custom
StacIO
class I naively expected that thepystac.Item.from_file
would use theStacIO.stac_object_from_dict
method, but that is not the case. Invoking the modifier requires reading the STAC objects with thepystac.read_file
method. This is because thepystac.STACObject.from_file
methods do not rely onStacIO.stac_object_from_dict
(in fact the latter relies on the former).Thinking "out loud" about how to enable the
pystac.STACObject.from_file
... add a_modifier
class variable that is aCallable[[Modifiable], STACObject]
and have theSTACObject.from_file
methods optionally apply the_modifier
if it is set? I don't love it...That said, it's not clear whether there is any demand for enabling this functionality when using the
pystac.read_file
or separately and explicitly passing STAC objects to a modifier function approaches work just as well. Using thefrom_file
approach would appeasemypy
w/o the need forcast
ing, but that's a pretty minor DevX nit tbh. Maybe this example is informative enough for the docs?All feedback is welcome.
The text was updated successfully, but these errors were encountered: