Add tags to STAC items

Add tags to STAC items in your storage to improve their discoverability.

Open notebook

Add tags to STAC items in your storage to improve their discoverability.

Set up the notebook

1. Install dependencies

Python
!pip install up42-py --upgrade -q
import up42, pathlib

2. Configure credentials

Run the cell below to create a credentials.json file in a directory named .up42 in your home folder.

Python
# Define the credentials file path if it doesn't exist
credentials_file_path = pathlib.Path.home().joinpath(".up42/credentials.json")
credentials_file_path.parent.mkdir(parents=True, exist_ok=True)
credentials_file_path.touch(exist_ok=True)
  1. Click the link above to the created file and paste the following code:
    JSON
    {
    "username": "<your-email-address>",
    "password": "<your-password>"
    }
  2. Retrieve the email address and password used for logging into the console. Use them as values for username and password.
  3. Save the credentials.json file.

3. Authenticate and create a PySTAC client connection

Python
up42.authenticate(cfg_file=credentials_file_path)
# Use region if you're logging with the NSG service
# up42.authenticate(cfg_file=credentials_file_path, region="sa")
UP42_client = up42.stac_client()

Filter STAC items by existing tags

You can make a detailed search request to find specific STAC items in your storage.

Python
# Set up a filter to search for STAC items that contain the "ortho" tag
filter = {
"op": "a_contains",
"args": [
{"property": "tags"},
["ortho"],
],
}
# Perform the search
stac_items_search = UP42_client.search(filter=filter)
for item in stac_items_search.items():
properties = item.properties
# Print all results matching the search filter
print(f"STAC item ID: {item.id}")
print(f"Order ID: {item.up42.order_id}")
print(f"User title: {item.up42.title}")
print(f"User tags: {item.up42.tags}\n")

Add new tags to the STAC items matching the filter

Python
# Define new tags to add
new_tags = ["ortho-done"]
# Set up a filter to search for STAC items that contain the "ortho" tag
filter = {
"op": "a_contains",
"args": [
{"property": "tags"},
["ortho"],
],
}
# Perform the search
stac_items_search = UP42_client.search(filter=filter)
# Iterate through each STAC item and update its tags
for item in stac_items_search.items():
item.up42.tags = item.up42.tags + new_tags
item.update() # Essential step: update change in our database
for item in stac_items_search.items():
properties = item.properties
# Print all results matching the search filter
print(f"STAC item ID: {item.id}")
print(f"Order ID: {item.up42.order_id}")
print(f"User title: {item.up42.title}")
print(f"User tags: {item.up42.tags}\n")

Add new tags to a selected STAC item

Python
# Define new tags to add
new_tags = ["sentinel-2"]
# Select a STAC item
item_id = "9b919b88-8cec-42d9-b901-0b66240ba282"
item = next(UP42_client.get_items(item_id))
item.up42.tags = item.up42.tags + new_tags
item.update() # Essential step: update change in our database
# Print results
print(f"STAC item ID: {item.id}")
print(f"Order ID: {item.up42.order_id}")
print(f"User title: {item.up42.title}")
print(f"User tags: {item.up42.tags}\n")