@@ -475,56 +475,56 @@ Extension (e.g. Electro-Optical, Projection, etc.) and STAC Object
475
475
(:class: `~pystac.Collection `, :class: `pystac.Item `, or :class: `pystac.Asset `). All
476
476
classes that extend these objects inherit from
477
477
: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 .
479
479
480
480
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> `:
483
483
484
484
.. code-block :: python
485
485
486
486
import pystac
487
- from pystac.extensions.eo import EOExtension
488
487
489
488
item = pystac.Item.from_file(" tests/data-files/eo/eo-landsat-example.json" )
490
489
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
+ ...
494
495
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`
501
497
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
505
499
the `Adding an Extension `_ section below for details on adding an extension to an
506
500
object.
507
501
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
+
508
510
See the documentation for each extension implementation for details on the supported
509
511
properties and other functionality.
510
512
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:
516
517
517
518
.. code-block :: python
518
519
519
520
item = pystac.Item.from_file(" tests/data-files/eo/eo-landsat-example.json" )
520
521
print (item.properties[" eo:cloud_cover" ])
521
522
# 78
522
523
523
- eo_ext = EOExtension.ext(item)
524
- print (eo_ext.cloud_cover)
524
+ print (item.ext.eo.cloud_cover)
525
525
# 78
526
526
527
- eo_ext .cloud_cover = 45
527
+ item.ext.eo .cloud_cover = 45
528
528
print (item.properties[" eo:cloud_cover" ])
529
529
# 45
530
530
@@ -545,24 +545,17 @@ have a default value of ``None``:
545
545
.. code-block :: python
546
546
547
547
# 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 )
553
549
554
550
555
551
Adding an Extension
556
552
-------------------
557
553
558
554
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
566
559
extended.
567
560
568
561
.. code-block :: python
@@ -573,7 +566,7 @@ extended.
573
566
# []
574
567
575
568
# Add the Electro-Optical extension
576
- EOExtension.add_to(item )
569
+ item.ext.add( " eo " )
577
570
print (item.stac_extensions)
578
571
# ['https://stac-extensions.github.io/eo/v1.1.0/schema.json']
579
572
@@ -617,7 +610,7 @@ Item Asset properties
617
610
=====================
618
611
619
612
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
621
614
asset. For example, gsd defined for an Item represents the best Ground Sample Distance
622
615
(resolution) for the data within the Item. However, some assets may be lower resolution
623
616
and thus have a higher gsd. In that case, the `gsd ` can be found on the Asset.
0 commit comments