Usage
To use the STAC data source, you can load a STAC catalog into a Sedona DataFrame using the stac format. The path can be either a local STAC collection JSON file or an HTTP/HTTPS endpoint to retrieve the collection JSON file. You can load a STAC collection from a local collection file:Filter Pushdown
The STAC data source supports predicate pushdown for spatial and temporal filters. The data source can push down spatial and temporal filters to the underlying data source to reduce the amount of data that needs to be read.Spatial Filter Pushdown
Spatial filter pushdown allows the data source to apply spatial predicates (e.g., st_contains, st_intersects) directly at the data source level, reducing the amount of data transferred and processed.Temporal Filter Pushdown
Temporal filter pushdown allows the data source to apply temporal predicates (e.g., BETWEEN,>=, <=) directly at the data source level, similarly reducing the amount of data transferred and processed.
Examples
Here are some examples demonstrating how to query a STAC data source that is loaded into a table namedSTAC_TABLE.
SQL Select Without Filters
SQL Select With Temporal Filter
SQL Select With Spatial Filter
Sedona Configuration for STAC Reader
When using the STAC reader in Sedona, several configuration options can be set to control the behavior of the reader. These configurations are typically set in aMap[String, String] and passed to the reader. Below are the key sedona configuration options:
This option specifies the maximum number of item files that can be included in a single partition. It helps in controlling the size of partitions. The default value is set to -1, meaning the system will automatically determine the number of item files per partition.
This option sets the number of partitions to be created for the STAC data. It allows for better control over data distribution and parallel processing. The default value is set to -1, meaning the system will automatically determine the number of item files per partition.
This option specifies the maximum number of items to be loaded from the STAC collection. It helps in limiting the amount of data processed. The default value is set to -1, meaning all items will be loaded.
This option specifies the threshold for reporting the progress of item loading. It helps in monitoring the progress of the loading process. The default value is set to 1000000, meaning the progress will be reported every 1,000,000 items loaded.
This option specifies the maximum number of items to be requested in a single API call. It helps in controlling the size of each request. The default value is set to 10.
headers
This option specifies HTTP headers to include in STAC API requests. It should be a JSON-encoded string containing a dictionary of header key-value pairs. This is useful for authentication and custom headers. Example:
{"Authorization": "Basic <base64_credentials>"}This option controls whether output database raster fields are generated for each row. When enabled, the “assets” field in each row is processed so that each asset is updated with a raster linked to its “href” value. The default value is
true.Map[String, String] and passed to the STAC reader as shown below:
Python API
The Python API allows you to interact with a SpatioTemporal Asset Catalog (STAC) API using the Client class. This class provides methods to open a connection to a STAC API, retrieve collections, and search for items with various filters.Client Class
Methods
open(url: str, headers: Optional[dict] = None) -> Client
Opens a connection to the specified STAC API URL.
Parameters:
The URL of the STAC API to connect to.
Example:
"https://planetarycomputer.microsoft.com/api/stac/v1"Optional dictionary of HTTP headers for authentication or custom headers.
Example:
{"Authorization": "Bearer token123"}An instance of the
Client class connected to the specified URL.with_basic_auth(username: str, password: str) -> Client
Adds HTTP Basic Authentication to the client.
This method encodes the username and password using Base64 and adds the appropriate Authorization header for HTTP Basic Authentication.
Parameters:
The username for authentication. For API keys, this is typically the API key itself.
Example:
"your_api_key"The password for authentication. For API keys, this is often left empty.
Example:
""Returns self for method chaining.
with_bearer_token(token: str) -> Client
Adds Bearer Token Authentication to the client.
This method adds the appropriate Authorization header for Bearer Token authentication, commonly used with OAuth2 and API tokens.
Parameters:
The bearer token for authentication.
Example:
"your_access_token_here"Returns self for method chaining.
get_collection(collection_id: str) -> CollectionClient
Retrieves a collection client for the specified collection ID.
Parameters:
The ID of the collection to retrieve.
Example:
"aster-l1t"An instance of the
CollectionClient class for the specified collection.search(*ids: Union[str, list], collection_id: str, bbox: Optional[list] = None, datetime: Optional[Union[str, datetime.datetime, list]] = None, max_items: Optional[int] = None, return_dataframe: bool = True) -> Union[Iterator[PyStacItem], DataFrame]
Searches for items in the specified collection with optional filters.
Parameters:
A positional variable argument parameter of item IDs to filter the items.
Example:
"item_id1" or ["item_id1", "item_id2"]The ID of the collection to search in.
Example:
"aster-l1t"A list of bounding boxes for filtering the items. Each bounding box is represented as a list of four float values:
[min_lon, min_lat, max_lon, max_lat].
Example: [[ -180.0, -90.0, 180.0, 90.0 ]]A single datetime, RFC 3339-compliant timestamp, or a list of date-time ranges for filtering the items.
Example:
"2020-01-01T00:00:00Z"datetime.datetime(2020, 1, 1)[["2020-01-01T00:00:00Z", "2021-01-01T00:00:00Z"]]
The maximum number of items to return from the search, even if there are more matching results.
Example:
100If
True (default), return the result as a Spark DataFrame instead of an iterator of PyStacItem objects.
Example: TrueAn iterator of
PyStacItem objects or a Spark DataFrame that matches the specified filters.Sample Code
Initialize the Client
Search Items on a Collection Within a Year
Search Items on a Collection Within a Day and Max Items
Search Items with Bounding Box and Interval
Search Multiple Items with Multiple Bounding Boxes
Search Items and Get DataFrame as Return with Multiple Intervals
Save Items in DataFrame to GeoParquet with Both Bounding Boxes and Intervals
Authentication
Many STAC services require authentication to access their data. The STAC client supports multiple authentication methods including HTTP Basic Authentication, Bearer Token Authentication, and custom headers.Basic Authentication
Basic authentication is commonly used with API keys or username/password combinations. Many services (like Planet Labs) use API keys as the username with an empty password.Bearer Token Authentication
Bearer token authentication is used with OAuth2 tokens and JWT tokens. Note that some services may only support specific authentication methods.Custom Headers
You can also pass custom headers directly when creating the client, which is useful for services with non-standard authentication requirements.Authentication with Scala DataSource
When using the STAC data source directly in Scala or through Spark SQL, you can pass authentication headers as a JSON-encoded option:Important Notes
- Authentication methods are mutually exclusive: Setting a new authentication method will overwrite any previously set Authorization header, but other custom headers remain unchanged.
- Headers are propagated: Headers set on the Client are automatically passed to all collection and item requests.
- Service-specific requirements: Different STAC services may require different authentication methods. For example, Planet Labs requires Basic Authentication rather than Bearer tokens for collection access.
- Backward compatibility: All authentication parameters are optional. Existing code that accesses public STAC services without authentication will continue to work unchanged.
References
- STAC Specification: https://stacspec.org/
- STAC Browser: https://github.com/radiantearth/stac-browser
- STAC YouTube Video: https://www.youtube.com/watch?v=stac-video

