Terrascope STAC API with leafmap¶
This notebook demonstrates how to access Terrascope data using the leafmap.terrascope module.
Setup: Set environment variables before running:
export TERRASCOPE_USERNAME='your_username'
export TERRASCOPE_PASSWORD='your_password'
In [ ]:
Copied!
import leafmap
import leafmap.terrascope as terrascope
import leafmap
import leafmap.terrascope as terrascope
Authentication¶
In [ ]:
Copied!
terrascope.login()
terrascope.login()
Search for NDVI Data¶
In [ ]:
Copied!
bbox = [5.032597, 51.220809, 5.055170, 51.234246]
items = terrascope.search_ndvi(
bbox=bbox,
start="2025-05-01",
end="2025-06-01",
max_cloud_cover=10,
)
print(f"Found {len(items)} scenes:")
for date in terrascope.get_item_dates(items):
print(f" {date}")
bbox = [5.032597, 51.220809, 5.055170, 51.234246]
items = terrascope.search_ndvi(
bbox=bbox,
start="2025-05-01",
end="2025-06-01",
max_cloud_cover=10,
)
print(f"Found {len(items)} scenes:")
for date in terrascope.get_item_dates(items):
print(f" {date}")
Single Layer Visualization¶
In [ ]:
Copied!
center = [(bbox[1] + bbox[3]) / 2, (bbox[0] + bbox[2]) / 2]
m = leafmap.Map(center=center, zoom=14)
m.add_raster(
items[0].assets["NDVI"].href,
layer_name=f"NDVI {items[0].datetime.date()}",
colormap="RdYlGn",
vmin=0,
vmax=250,
)
m
center = [(bbox[1] + bbox[3]) / 2, (bbox[0] + bbox[2]) / 2]
m = leafmap.Map(center=center, zoom=14)
m.add_raster(
items[0].assets["NDVI"].href,
layer_name=f"NDVI {items[0].datetime.date()}",
colormap="RdYlGn",
vmin=0,
vmax=250,
)
m
Time Slider Animation¶
In [ ]:
Copied!
layers = terrascope.create_time_layers(items[:3]) # Limit to 3 for demo
m = leafmap.Map(center=center, zoom=14)
m.add_time_slider(layers, time_interval=1)
m
layers = terrascope.create_time_layers(items[:3]) # Limit to 3 for demo
m = leafmap.Map(center=center, zoom=14)
m.add_time_slider(layers, time_interval=1)
m
Data Analysis with rioxarray¶
In [ ]:
Copied!
import rioxarray
import numpy as np
# Clean up stale tile servers before analysis
terrascope.cleanup_tile_servers()
first_item = items[0]
print(f"Analyzing: {first_item.datetime.date()}")
with rioxarray.open_rasterio(first_item.assets["NDVI"].href, mask_and_scale=True) as ds:
clipped = ds.rio.clip_box(*bbox, crs="EPSG:4326")
data = clipped.sel(band=1).values
print(f"\nNDVI Statistics:")
print(f" Min: {np.nanmin(data):.2f}")
print(f" Max: {np.nanmax(data):.2f}")
print(f" Mean: {np.nanmean(data):.2f}")
import rioxarray
import numpy as np
# Clean up stale tile servers before analysis
terrascope.cleanup_tile_servers()
first_item = items[0]
print(f"Analyzing: {first_item.datetime.date()}")
with rioxarray.open_rasterio(first_item.assets["NDVI"].href, mask_and_scale=True) as ds:
clipped = ds.rio.clip_box(*bbox, crs="EPSG:4326")
data = clipped.sel(band=1).values
print(f"\nNDVI Statistics:")
print(f" Min: {np.nanmin(data):.2f}")
print(f" Max: {np.nanmax(data):.2f}")
print(f" Mean: {np.nanmean(data):.2f}")
Explore Available Collections¶
In [ ]:
Copied!
collections = terrascope.list_collections()
print(f"Available collections ({len(collections)}):")
for c in sorted(collections):
print(f" {c}")
collections = terrascope.list_collections()
print(f"Available collections ({len(collections)}):")
for c in sorted(collections):
print(f" {c}")
In [ ]:
Copied!
# Optional: logout when done
# terrascope.logout()
# Optional: logout when done
# terrascope.logout()