easierdata / ipfs-stac

Python client utilized for interfacing with the Easier STAC Server
MIT License
3 stars 1 forks source link

IPFS-STAC

ipfs-stac is a Python library that provides functionality for querying and interacting with STAC catalogs enriched with IPFS. The library supports seamless operations between leveraging STAC APIs enriched with IPFS metadata and interfacing with IPFS itself given a node.

Features


Language Support

The client currently supports Python versions 3+

Installation

The client can be installed through pip

$ pip install ipfs-stac

Usage

Create a client object

from ipfs_stac import client

# Create a new client object without a local node (uses remote gateways)
easier = client.Web3(stac_endpoint="<my_stac_server>/api/v1/pgstac/")

# If you want to use your local IPFS node (preferred), specify the endpoint in the local_gateway argument
easier = client.Web3(local_gateway="127.0.0.1", stac_endpoint="<my_stac_server>/api/v1/pgstac/")

Fetch a CID from IPFS

# Simple hello world example
data = easier.getFromCID("QmZ4tDuvesekSs4qM5ZBKpXiZGun7S2CYtEZRB3DYXkjGx")
print(data)

"""
hello worlds
"""

Query STAC API By Bounding Box

easier = client.Web3(local_gateway="127.0.0.1", stac="<my_stac_server>/api/v1/pgstac/")
"""
Retrieve all items from STAC catalog that are in bounding box with searchSTACByBox method (2 arguments)
1. Coordinates of bounding box
2. Name(s) of STAC collections)
"""
# Use any collection name you want. This is just an example
items = easier.searchSTACByBox([-76.964657, 38.978967, -76.928008, 39.002783], "landsat-c2l1") 

"""
The searchSTACByBoxIndex by method takes 3 arguments
1. Coordinates for the bounding box
2. Name of the STAC collection to query
3. Index of the item you want to retrieve
"""
item = easier.searchSTACByBoxIndex([-76.964657, 38.978967, -76.928008, 39.002783], "landsat-c2l1", 0)
# In this example, 'nir08' is the name of the band (asset) we want to retrieve from a landsat item
band = easier.getAssetFromItem(item, 'nir08')

# Optionally, you can fetch multiple assets by the getAssetsFromItem Method
bands = easier.getAssetsFromItem(item, ["blue", "red"]) # Returns array of assets

The Asset Object

# This snippet extends the previous under "Query STAC API By Bounding Box"

# The asset object, when printed, will return the CID
print(band) # QmNddx9BvBsQMXgwp6a83D2wiLrmovgCpRKVYKSJoWNNbx

# The asset content (bytes) can be found in asset.data. If this is None, you can call the fetch method to retrieve the data.
band.fetch()
print(band.data) # b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x...

# Alternatively, you can also transform the asset data in different formats such as a numpy array
band_np = band.to_np_ndarray()
print(band_np) # [[0. 0. 0. ... 0. 0. 0.]
               #  [0. 0. 0. ... 0. 0. 0.]
               #  [0. 0. 0. ... 0. 0. 0.]
               #  ...
               #  [0. 0. 0. ... 0. 0. 0.]
               #  [0. 0. 0. ... 0. 0. 0.]
               #  [0. 0. 0. ... 0. 0. 0.]]

Documentation

class Web3

__init__(local_gateway: str = "localhost", api_port: int = 5001, gateway_port: int = 8080, stac_endpoint: str = "")

Description:
Initializes a Web3 client.

Parameters:

Attributes


_get_collections_ids() -> List[str]

Description:
Fetches the collection IDs from the STAC endpoint.

Returns:


getCollections() -> Sequence[Collection]

Description:
Returns a list of collections from the STAC endpoint.

Returns:


startDaemon() -> None

Description:
Starts the IPFS daemon process.

Raises:


shutdown_process() -> None

Description:
Shuts down the IPFS daemon process.


getFromCID(cid: str) -> Union[bytes, None]

Description:
Retrieves raw data from a specified CID.

Parameters:

Returns:


searchSTACByBox(bbox: List[float], collections: List[str]) -> ItemCollection

Description:
Searches the STAC catalog by bounding box and returns an array of items.

Parameters:

Returns:


searchSTAC(**kwargs) -> ItemCollection

Description:
Searches the STAC catalog for items using the pystac-client search method.

Parameters:

Returns:


getAssetNames(stac_obj: Union[CollectionClient, ItemCollection, Item]) -> Union[List[str], None]

Description:
Fetches a list of unique asset names from a given STAC object.

Parameters:

Returns:


getAssetFromItem(item: Item, asset_name: str, fetch_data: bool = False) -> Union["Asset", None]

Description:
Returns an asset object from a specified STAC item.

Parameters:

Returns:


uploadToIPFS(content: Union[str, Path, bytes], file_name: Optional[str] = None, pin_content: bool = False, mfs_path: Optional[str] = None, chunker: Optional[str] = None) -> None

Description:
Uploads content to IPFS.

Parameters:

Raises:


pinned_list(pin_type: str = recursive, names: bool) -> Union[List[str], None]

Description:
Fetch pinned CIDs from local node

Parameters:

Returns:


class Asset

__init__(cid: str, local_gateway: str, api_port: int, fetch_data: bool = False, name: Optional[str] = None)

Description:
Initializes an Asset object associated with a CID.

Parameters:

Attributes


_is_pinned_to_local_node() -> bool

Description:
Checks if the CID is pinned to the local node.

Returns:


fetch() -> None

Description:
Fetches the data associated with the CID using the fetchCID method.

Raises:


pin() -> None

Description:
Pins the CID to the configured IPFS node


addToMFS(filename: str, mfs_path: str) -> None

Description:
Adds the CID to the Mutable File System (MFS) in IPFS.

Parameters:


to_np_ndarray(dtype: Union[np.dtype, type] = np.float32) -> np.ndarray

Description:
Converts the asset's data into a NumPy ndarray if the data represents an image.

Parameters:

Returns:

Raises:


Attributions

This project was made possible by the following