How to use the pystac.STACError function in pystac

To help you get started, we’ve selected a few pystac examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github azavea / pystac / pystac / catalog.py View on Github external
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))
github azavea / pystac / pystac / extensions / eo.py View on Github external
Args:
            asset [Asset]: The asset from the associated item to get the bands of.

        Returns:
            [List[Band] or None]: A list of Band objects associated with the asset, or None
            if the asset doesn't exist or does not contain band information.
        """
        if 'eo:bands' not in asset.properties:
            return None

        result = []
        band_length = len(self.bands)
        for band_idx in asset.properties['eo:bands']:
            if band_idx >= band_length:
                raise STACError("Item {} contains assets with band indexes out of range! "
                                "Band index {} is greater than the number of bands "
                                "for the item ({})".format(self.item.id, band_idx, band_length))
            result.append(self.bands[band_idx])

        return result
github azavea / pystac / pystac / stac_object.py View on Github external
Otherwise, leave out the self link.
          dest_href (str): Optional HREF to save the file to. If None, the object will be saved
              to the object's self href.

        Raises:
            :class:`~pystac.STACError`: If no self href is set, this error will be raised.

        Note:
            When to include a self link is described in the `Use of Links section of the
            STAC best practices document
            `_
        """
        if dest_href is None:
            self_href = self.get_self_href()
            if self_href is None:
                raise STACError(
                    'Self HREF must be set before saving without an explicit dest_href.')
            dest_href = self_href

        STAC_IO.save_json(dest_href, self.to_dict(include_self_link=include_self_link))
github azavea / pystac / pystac / eo.py View on Github external
"""
        if isinstance(item, EOItem):
            return item.clone()

        eo_params = {}
        for eof in EOItem._EO_FIELDS:
            eo_key = EOItem._eo_key(eof)
            if eo_key in item.properties.keys():
                if eof == 'bands':
                    eo_params[eof] = [
                        Band.from_dict(b) for b in item.properties.pop(eo_key)
                    ]
                else:
                    eo_params[eof] = item.properties.pop(eo_key)
            elif eof in ('gsd', 'platform', 'instrument', 'bands'):
                raise STACError(
                    "Missing required field '{}' in properties".format(eo_key))

        if not any(item.properties):
            item.properties = None

        e = cls(id=item.id,
                geometry=item.geometry,
                bbox=item.bbox,
                datetime=item.datetime,
                properties=item.properties,
                stac_extensions=item.stac_extensions,
                collection=item.collection_id,
                **eo_params)

        e.links = item.links
        e.assets = item.assets
github azavea / pystac / pystac / extensions / label.py View on Github external
def label_type(self, v):
        if v not in LabelType.ALL:
            raise STACError("label_type must be one of "
                            "{}. Invalid input: {}".format(LabelType.ALL, v))

        self.item.properties['label:type'] = v
github azavea / pystac / pystac / extensions / label.py View on Github external
def label_overviews(self, v):
        if v is None:
            self.item.properties.pop('label:overviews', None)
        else:
            if not type(v) is list:
                raise STACError("label_overviews must be a list! Invalid input: {}".format(v))

            overviews = [x.to_dict() for x in v]
            self.item.properties['label:overviews'] = overviews
github azavea / pystac / pystac / item.py View on Github external
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)

        has_self_link = False
        for l in d['links']:
            has_self_link |= l['rel'] == 'self'
            item.add_link(Link.from_dict(l))

        if not has_self_link and href is not None:
github azavea / pystac / pystac / extensions / label.py View on Github external
def counts(self, v):
        if v is None:
            self.properties.pop('counts', None)
        else:
            if not type(v) is list:
                raise STACError("counts must be a list! Invalid input: {}".format(v))

            self.properties['counts'] = [c.to_dict() for c in v]
github azavea / pystac / pystac / extensions / label.py View on Github external
def label_properties(self, v):
        if v is not None:
            if not type(v) is list:
                raise STACError("label_properties must be a list! Invalid input: {}".format(v))

        self.item.properties['label:properties'] = v