Esempio n. 1
0
def _stac_object_from_dict(d, href=None, root=None):
    """Determines how to deserialize a dictionary into a STAC object.

    Args:
        d (dict): The dict to parse.
        href (str): Optional href that is the file location of the object being
            parsed.
        root (Catalog or Collection): Optional root of the catalog for this object.
            If provided, the root's resolved object cache can be used to search for
            previously resolved instances of the STAC object.

    Note: This is used internally in STAC_IO to deserialize STAC Objects.
    It is in the top level __init__ in order to avoid circular dependencies.
    """
    if 'type' in d:
        if d['type'] == 'FeatureCollection':
            # Dealing with an Item Collection
            if 'collections' in d:
                return SingleFileSTAC.from_dict(d, href=href, root=root)
            else:
                return ItemCollection.from_dict(d, href=href, root=root)
        else:
            # Dealing with an Item
            if any([k for k in d['properties'].keys() if k.startswith('eo:')]):
                return EOItem.from_dict(d, href=href, root=root)
            elif any(
                [k for k in d['properties'].keys() if k.startswith('label:')]):
                return LabelItem.from_dict(d, href=href, root=root)
            else:
                return Item.from_dict(d, href=href, root=root)
    elif 'extent' in d:
        return Collection.from_dict(d, href=href, root=root)
    else:
        return Catalog.from_dict(d, href=href, root=root)
Esempio n. 2
0
    def from_dict(d):
        """Constructs an SingleFileSTAC from a dict.

        Returns:
            SingleFileSTAC: The SingleFileSTAC deserialized from the JSON dict.
        """
        features = [Item.from_dict(feature) for feature in d['features']]
        collections = [Collection.from_dict(c) for c in d['collections']]

        # Tie together items to their collections
        collection_dict = dict([(c.id, c) for c in collections])
        for item in features:
            if item.collection_id is not None:
                if item.collection_id not in collection_dict:
                    raise STACError(
                        'Collection with id {} is referenced '
                        'by item {}, but is not in the collections '
                        'of this SingleFileSTAC'.format(
                            item.collection_id, item.id))
                item.set_collection(collection_dict[item.collection_id])

        search_obj = None
        if 'search' in d.keys():
            sd = d['search']
            search_obj = Search(sd.get('endpoint'), sd.get('parameters'))
        return SingleFileSTAC(features, collections, search_obj)
Esempio n. 3
0
def item_to_meta_uri(
    item: Item, rewrite: Optional[Tuple[str, str]] = None
) -> Generator[Tuple[dict, str, bool], None, None]:
    uri, relative = _guess_location(item, rewrite)
    metadata = item.to_dict()
    if relative:
        metadata = stac_transform(metadata)
    else:
        metadata = stac_transform_absolute(metadata)

    return (metadata, uri)
Esempio n. 4
0
def stac_object_from_dict(d):
    """Determines how to deserialize a dictionary into a STAC object."""
    if 'type' in d:
        if 'label:description' in d['properties']:
            return LabelItem.from_dict(d)
        else:
            return Item.from_dict(d)
    elif 'extent' in d:
        return Collection.from_dict(d)
    else:
        return Catalog.from_dict(d)
Esempio n. 5
0
    def from_dict(d):
        """Constructs an ItemCollection from a dict.

        Returns:
            ItemCollection: The ItemCollection deserialized from the JSON dict.
        """
        features = [Item.from_dict(feature) for feature in d['features']]
        ic = ItemCollection(features)
        if 'links' in d.keys():
            for link in d['links']:
                ic.add_link(Link.from_dict(link))
        return ic
Esempio n. 6
0
    def from_dict(d):
        item = Item.from_dict(d)
        props = item.properties

        label_property = props.get('label:property')
        label_classes = props.get('label:classes')
        if label_classes is not None:
            label_classes = [
                LabelClasses.from_dict(classes) for classes in label_classes
            ]
        label_description = props['label:description']
        label_type = props['label:type']
        label_task = props.get('label:task')
        label_method = props.get('label:method')
        label_overview = props.get('label:overview')
        if label_overview is not None:
            label_overview = LabelOverview.from_dict(label_overview)

        li = LabelItem(id=item.id,
                       geometry=item.geometry,
                       bbox=item.bbox,
                       datetime=item.datetime,
                       properties=item.properties,
                       label_description=label_description,
                       label_type=label_type,
                       label_property=label_property,
                       label_classes=label_classes,
                       stac_extensions=item.stac_extensions,
                       label_task=label_task,
                       label_method=label_method,
                       label_overview=label_overview)

        li.links = item.links
        li.assets = item.assets

        return li
Esempio n. 7
0
 def from_dict(cls, d, href=None, root=None):
     item = Item.from_dict(d, href=href, root=root)
     return cls.from_item(item)