Skip to content

Examples Catalog

Below is a list of examples for prefect-planetary-computer.

Credentials Module

Using a temporary PC Dask Gateway cluster:

from prefect import flow

pc_credentials = PlanetaryComputerCredentials.load("BLOCK_NAME")

pc_runner = pc_credentials.get_dask_task_runner()

@flow(task_runner=pc_runner)
def my_flow():
    ...

Providing additional kwargs to the PC Dask Gateway cluster:

pc_runner = pc_credentials.get_dask_task_runner(
    cluster_kwargs={
        "image": "pangeo/pangeo-notebook:latest",
    },
    adapt_kwargs={'minimum': 1, 'maximum': 10, 'active': True}
)

Connecting to an existing PC GatewayCluster (use DaskTaskRunner directly for this):

DaskTaskRunner(
    address=cluster.address,
    client_kwargs={'security': cluster.security}
)
Get a configured Dask Gateway client:
from prefect import flow
from prefect_planetary_computer import PlanetaryComputerCredentials

@flow
def example_get_gateway_flow():
    pc_credentials_block = PlanetaryComputerCredentials(
        subscription_key = "sub-key",
        hub_api_token = "hub-token"
    )
    gateway = pc_credentials_block.get_gateway()

    # List available clusters
    clusters = gateway.list_clusters()
    return len(clusters)

example_get_gateway_flow()
Instantiate a new cluster using PC Dask Gateway Server:
import dask.array as da
from prefect import flow
from prefect_planetary_computer import PlanetaryComputerCredentials

@flow
def example_new_cluster_flow():
    pc_credentials_block = PlanetaryComputerCredentials.load("BLOCK_NAME")

    # Create a Dask Gateway cluster with default configuration
    # it will be automatically used for any subsequent dask compute
    cluster = pc_credentials_block.new_cluster()

    # Scale the cluster to at most 10 workers
    cluster.adapt(minimum=2, maximum=10)

    # Create a Dask array with 1 billion elements and sum them
    x = da.random.random(1000000000)
    result = x.sum().compute()

    return result

example_new_cluster_flow()
Get a configured PySTAC client with automatic asset signing:
from prefect import flow
from prefect_planetary_computer import PlanetaryComputerCredentials

@flow
def example_get_stac_catalog_flow():
    pc_credentials_block = PlanetaryComputerCredentials.load("BLOCK_NAME")

    catalog = pc_credentials_block.get_stac_catalog()

    # Search STAC catalog for Landsat Collection 2 Level 2 items
    time_range = "2020-12-01/2020-12-31"
    bbox = [-122.2751, 47.5469, -121.9613, 47.7458]

    search = catalog.search(collections=["landsat-c2-l2"], bbox=bbox, datetime=time_range)
    items = search.get_all_items()
    return len(items)

example_get_stac_catalog_flow()