How to use the pystac.stac_io.STAC_IO 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 / tests / __init__.py View on Github external
raise HTTPError(url=uri,
                                msg='cool-sat.com does not exist',
                                hdrs=None,
                                code=404,
                                fp=None)

            with urlopen(uri) as f:
                return f.read().decode('utf-8')
        else:
            with open(uri) as f:
                return f.read()
    except HTTPError as e:
        raise Exception("Could not read uri {}".format(uri)) from e


STAC_IO.read_text_method = unsafe_read_https_method
github azavea / pystac / pystac / item_collection.py View on Github external
Args:
            href (str): The HREF to save the ItemCollection to. If None,
                will save to the currently set ``self`` link.
                If supplied, will set this href to the ItemCollection's self link.
            include_self_link (bool): If True, will include the self link
                in the set of links of the saved JSON.
        """
        if href is None:
            href = self.get_self_href()
            if href is None:
                raise STACError('Must either supply an href or set a self href on '
                                'this ItemCollection.')
        else:
            self.set_self_href(href)

        STAC_IO.save_json(href, self.to_dict(include_self_link=include_self_link))
github azavea / pystac / pystac / __init__.py View on Github external
return Catalog.from_dict(d, href=href, root=root)
    if info.object_type == STACObjectType.COLLECTION:
        return Collection.from_dict(d, href=href, root=root)
    if info.object_type == STACObjectType.ITEMCOLLECTION:
        if 'single-file-stac' in info.common_extensions:
            return SingleFileSTAC.from_dict(d, href=href, root=root)
        return ItemCollection.from_dict(d, href=href, root=root)
    if info.object_type == STACObjectType.ITEM:
        if 'eo' in info.common_extensions:
            return EOItem.from_dict(d, href=href, root=root)
        if 'label' in info.common_extensions:
            return LabelItem.from_dict(d, href=href, root=root)
        return Item.from_dict(d, href=href, root=root)


STAC_IO.stac_object_from_dict = _stac_object_from_dict
github azavea / pystac / pystac / stac_io.py View on Github external
"""Read a dict from the given URI.

        Args:
            uri (str): The URI from which to read.

        Returns:
            dict: A dict representation of the JSON contained in the file at the
            given uri.

        Note:
            This method uses the :func:`STAC_IO.read_text_method
            `. If you want to modify the behavior of
            STAC_IO in order to enable additional URI types, replace that member
            with your own implementation.
        """
        return json.loads(STAC_IO.read_text(uri))
github azavea / pystac / pystac / stac_object.py View on Github external
def from_file(cls, href):
        """Reads a STACObject implementation from a file.

        Args:
            href (str): The HREF to read the object from.

        Returns:
            The specific STACObject implementation class that is represented
            by the JSON read from the file located at HREF.
        """
        if not is_absolute_href(href):
            href = make_absolute_href(href)
        d = STAC_IO.read_json(href)

        if cls == STACObject:
            o = STAC_IO.stac_object_from_dict(d, href=href)
        else:
            o = cls.from_dict(d, href=href)

        # Set the self HREF, if it's not already set to something else.
        if o.get_self_href() is None:
            o.set_self_href(href)

        # If this is a root catalog, set the root to the catalog instance.
        root_link = o.get_root_link()
        if root_link is not None:
            if not root_link.is_resolved():
                if root_link.get_absolute_href() == href:
                    o.set_root(o, link_type=root_link.link_type)
github azavea / pystac / pystac / stac_io.py View on Github external
def save_json(cls, uri, json_dict):
        """Write a dict to the given URI as JSON.

        Args:
            uri (str): The URI of the file to write the text to.
            json_dict (dict): The JSON dict to write.

        Note:
            This method uses the :func:`STAC_IO.write_text_method
            `. If you want to modify the behavior of
            STAC_IO in order to enable additional URI types, replace that member
            with your own implementation.
        """
        STAC_IO.write_text(uri, json.dumps(json_dict, indent=4))
github azavea / pystac / pystac / stac_object.py View on Github external
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 / link.py View on Github external
target_path = self.target
            parsed = urlparse(self.target)
            if parsed.scheme == '':
                if not os.path.isabs(parsed.path):
                    if self.owner is None:
                        raise STACError('Relative path {} encountered '
                                        'without owner.'.format(target_path))
                    owner_href = self.owner.get_self_href()
                    if owner_href is None:
                        raise STACError(
                            'Relative path {} encountered '
                            'without owner "self" link set.'.format(
                                target_path))
                    target_path = make_absolute_href(self.target, owner_href)

            obj = STAC_IO.read_stac_object(target_path, root=root)
            obj.set_self_href(target_path)
        else:
            obj = self.target

        if root is not None:
            self.target = root._resolved_objects.get_or_cache(obj)
            self.target.set_root(root, link_type=self.link_type)
        else:
            self.target = obj

        if self.owner and self.rel in ['child', 'item']:
            self.target.set_parent(self.owner, link_type=self.link_type)

        return self
github azavea / pystac / pystac / single_file_stac.py View on Github external
def from_file(uri):
        """Reads an SingleFileSTAC from a file.

        Args:
            href (str): The HREF to read the item from.

        Returns:
            SingleFileSTAC: SingleFileSTAC that was read from the given file.
        """

        d = json.loads(STAC_IO.read_text(uri))
        c = SingleFileSTAC.from_dict(d)
        return c
github azavea / pystac / pystac / serialization / common_properties.py View on Github external
links = item_dict['links']

        # Account for 0.5 links, which were dicts
        if isinstance(links, dict):
            links = list(links.values())

        collection_link = next((link for link in links if link['rel'] == 'collection'), None)
        if collection_link is not None:
            collection_href = collection_link['href']
            if json_href is not None:
                collection_href = make_absolute_href(collection_href, json_href)
            if collection_cache is not None:
                collection = collection_cache.get_by_href(collection_href)

            if collection is None:
                collection = STAC_IO.read_json(collection_href)

    if collection is not None:
        collection_id = None
        collection_props = None
        if isinstance(collection, Collection):
            collection_id = collection.id
            collection_props = collection.properties
        else:
            collection_id = collection['id']
            if 'properties' in collection:
                collection_props = collection['properties']

        if collection_props is not None:
            for k in collection_props:
                if k not in item_dict['properties']:
                    properties_merged = True