Skip to content

Commit bf2a29b

Browse files
jsignellgadomski
andauthored
Simpler extension interface (#1243)
* Add ext syntax * Rework exceptions and names * All the collection, item, and asset exts * Allow item_assets * Fix item assets * Add to non-notebook docs * Fix up some of the more uptodate notebooks * Execute all cells * Add docstrings * docs(fix): small punctuation tweak --------- Co-authored-by: Pete Gadomski <[email protected]>
1 parent 04c8ee1 commit bf2a29b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1896
-666
lines changed

docs/concepts.rst

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -475,56 +475,56 @@ Extension (e.g. Electro-Optical, Projection, etc.) and STAC Object
475475
(:class:`~pystac.Collection`, :class:`pystac.Item`, or :class:`pystac.Asset`). All
476476
classes that extend these objects inherit from
477477
:class:`pystac.extensions.base.PropertiesExtension`, and you can use the
478-
``ext`` method on these classes to extend an object.
478+
``ext`` accessor on the object to access the extension fields.
479479

480480
For instance, if you have an item that implements the :stac-ext:`Electro-Optical
481-
Extension <eo>`, you can access the properties associated with that extension using
482-
:meth:`EOExtension.ext <pystac.extensions.eo.EOExtension.ext>`:
481+
Extension <eo>`, you can access the fields associated with that extension using
482+
:meth:`Item.ext <pystac.Item.ext>`:
483483

484484
.. code-block:: python
485485
486486
import pystac
487-
from pystac.extensions.eo import EOExtension
488487
489488
item = pystac.Item.from_file("tests/data-files/eo/eo-landsat-example.json")
490489
491-
# Check that the Item implements the EO Extension
492-
if EOExtension.has_extension(item):
493-
eo_ext = EOExtension.ext(item)
490+
# As long as the Item implements the EO Extension you can access all the
491+
# EO properties directly
492+
bands = item.ext.eo.bands
493+
cloud_cover = item.ext.eo.cloud_cover
494+
...
494495
495-
bands = eo_ext.bands
496-
cloud_cover = eo_ext.cloud_cover
497-
snow_cover = eo_ext.snow_cover
498-
...
499-
500-
.. note:: The ``ext`` method will raise an :exc:`~pystac.ExtensionNotImplemented`
496+
.. note:: ``ext`` will raise an :exc:`~pystac.ExtensionNotImplemented`
501497
exception if the object does not implement that extension (e.g. if the extension
502-
URI is not in that object's :attr:`~pystac.STACObject.stac_extensions` list). In
503-
the example above, we check that the Item implements the EO Extension before calling
504-
:meth:`EOExtension.ext <pystac.extensions.eo.EOExtension.ext>` to handle this. See
498+
URI is not in that object's :attr:`~pystac.STACObject.stac_extensions` list). See
505499
the `Adding an Extension`_ section below for details on adding an extension to an
506500
object.
507501

502+
If you don't want to raise an error you can use :meth:`~pystac.Item.ext.has`
503+
to first check if the extension is implemented on your pystac object:
504+
505+
.. code-block:: python
506+
507+
if item.ext.has("eo"):
508+
bands = item.ext.eo.bands
509+
508510
See the documentation for each extension implementation for details on the supported
509511
properties and other functionality.
510512

511-
Instances of :class:`~pystac.extensions.base.PropertiesExtension` have a
512-
:attr:`~pystac.extensions.base.PropertiesExtension.properties` attribute that gives
513-
access to the properties of the extended object. *This attribute is a reference to the
514-
properties of the* :class:`~pystac.Item` *or* :class:`~pystac.Asset` *being extended and
515-
can therefore mutate those properties.* For instance:
513+
Extensions have access to the properties of the object. *This attribute is a reference
514+
to the properties of the* :class:`~pystac.Collection`, :class:`~pystac.Item` *or*
515+
:class:`~pystac.Asset` *being extended and can therefore mutate those properties.*
516+
For instance:
516517

517518
.. code-block:: python
518519
519520
item = pystac.Item.from_file("tests/data-files/eo/eo-landsat-example.json")
520521
print(item.properties["eo:cloud_cover"])
521522
# 78
522523
523-
eo_ext = EOExtension.ext(item)
524-
print(eo_ext.cloud_cover)
524+
print(item.ext.eo.cloud_cover)
525525
# 78
526526
527-
eo_ext.cloud_cover = 45
527+
item.ext.eo.cloud_cover = 45
528528
print(item.properties["eo:cloud_cover"])
529529
# 45
530530
@@ -545,24 +545,17 @@ have a default value of ``None``:
545545
.. code-block:: python
546546
547547
# Can also omit cloud_cover entirely...
548-
eo_ext.apply(0.5, bands, cloud_cover=None)
549-
550-
551-
If you attempt to extend an object that is not supported by an extension, PySTAC will
552-
throw a :class:`pystac.ExtensionTypeError`.
548+
item.ext.eo.apply(0.5, bands, cloud_cover=None)
553549
554550
555551
Adding an Extension
556552
-------------------
557553

558554
You can add an extension to a STAC object that does not already implement that extension
559-
using the :meth:`ExtensionManagementMixin.add_to
560-
<pystac.extensions.base.ExtensionManagementMixin.add_to>` method. Any concrete
561-
extension implementations that extend existing STAC objects should inherit from the
562-
:class:`~pystac.extensions.base.ExtensionManagementMixin` class, and will therefore have
563-
this method available. The
564-
:meth:`~pystac.extensions.base.ExtensionManagementMixin.add_to` adds the correct schema
565-
URI to the :attr:`~pystac.STACObject.stac_extensions` list for the object being
555+
using the :meth:`~pystac.Item.ext.add` method. Any concrete
556+
extension implementations that extend existing STAC objects should have
557+
this method available. The :meth:`~pystac.Item.ext.add` method adds the correct schema
558+
URI to the :attr:`~pystac.Item.stac_extensions` list for the object being
566559
extended.
567560

568561
.. code-block:: python
@@ -573,7 +566,7 @@ extended.
573566
# []
574567
575568
# Add the Electro-Optical extension
576-
EOExtension.add_to(item)
569+
item.ext.add("eo")
577570
print(item.stac_extensions)
578571
# ['https://stac-extensions.github.io/eo/v1.1.0/schema.json']
579572
@@ -617,7 +610,7 @@ Item Asset properties
617610
=====================
618611

619612
Properties that apply to Items can be found in two places: the Item's properties or in
620-
any of an Item's Assets. If the property is on an Asset, it applies only that specific
613+
any of an Item's Assets. If the property is on an Asset, it applies only to that specific
621614
asset. For example, gsd defined for an Item represents the best Ground Sample Distance
622615
(resolution) for the data within the Item. However, some assets may be lower resolution
623616
and thus have a higher gsd. In that case, the `gsd` can be found on the Asset.

0 commit comments

Comments
 (0)