terrascope module¶
Terrascope STAC API integration for leafmap.
This module provides authentication and helper functions for accessing Terrascope data services, including: - HTTP Basic Authentication for GDAL - STAC catalog search helpers - GDAL authentication for streaming COGs
Example
import leafmap.terrascope as terrascope terrascope.login() # Uses TERRASCOPE_USERNAME/PASSWORD env vars items = terrascope.search_ndvi(bbox=[5.0, 51.2, 5.1, 51.3], start="2025-05-01", end="2025-06-01") m = leafmap.Map() m.add_raster(items[0].assets["NDVI"].href, colormap="RdYlGn")
Note
Authentication uses HTTP Basic Auth via GDAL environment variables. Credentials are set in GDAL_HTTP_USERPWD for authenticated COG streaming.
cleanup_tile_servers()
¶
Kill stale localtileserver processes.
This is useful when switching between visualizations to avoid authentication errors from old tile server processes that have cached expired tokens.
Note
This uses pkill which is Unix-specific and will terminate all localtileserver processes for the current user, not just those started by this session.
Source code in leafmap/terrascope.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
create_time_layers(items, asset_key='NDVI', colormap='RdYlGn', vmin=0, vmax=250)
¶
Create tile layers for time slider visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List of pystac Item objects. |
required |
asset_key
|
str
|
Asset key to use (default "NDVI"). |
'NDVI'
|
colormap
|
str
|
Colormap name (default "RdYlGn"). |
'RdYlGn'
|
vmin
|
float
|
Minimum value for colormap. |
0
|
vmax
|
float
|
Maximum value for colormap. |
250
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary of {date_string: tile_layer} for use with Map.add_time_slider(). |
Example
items = terrascope.search_ndvi(bbox, start, end) layers = terrascope.create_time_layers(items) m = leafmap.Map() m.add_time_slider(layers)
Source code in leafmap/terrascope.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
get_asset_urls(items, asset_key='NDVI')
¶
Extract asset URLs from a list of STAC items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List of pystac Item objects. |
required |
asset_key
|
str
|
Asset key to extract (default "NDVI"). |
'NDVI'
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of asset URLs. |
Source code in leafmap/terrascope.py
307 308 309 310 311 312 313 314 315 316 317 318 | |
get_item_dates(items)
¶
Extract dates from a list of STAC items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
list
|
List of pystac Item objects. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of date strings in "YYYY-MM-DD" format. |
Source code in leafmap/terrascope.py
321 322 323 324 325 326 327 328 329 330 331 | |
get_stac_client()
¶
Get a pystac_client Client for the Terrascope STAC catalog.
Returns:
| Type | Description |
|---|---|
Any
|
pystac_client.Client instance. |
Source code in leafmap/terrascope.py
151 152 153 154 155 156 157 158 159 160 161 162 | |
list_collections()
¶
List available collections in the Terrascope STAC catalog.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of collection IDs. |
Source code in leafmap/terrascope.py
165 166 167 168 169 170 171 172 173 | |
login(username=None, password=None, auto_refresh=True, quiet=False)
¶
Authenticate with Terrascope using HTTP Basic Auth.
This sets up the necessary GDAL environment variables for GDAL/rasterio to authenticate with Terrascope when streaming COGs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
username
|
str | None
|
Terrascope username. Defaults to TERRASCOPE_USERNAME env var. |
None
|
password
|
str | None
|
Terrascope password. Defaults to TERRASCOPE_PASSWORD env var. |
None
|
auto_refresh
|
bool
|
Kept for API compatibility, ignored. |
True
|
quiet
|
bool
|
Suppress status messages. |
False
|
Example
import leafmap.terrascope as terrascope terrascope.login() Authenticated as: your_username
Source code in leafmap/terrascope.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
logout()
¶
Clear GDAL authentication configuration.
Unsets GDAL environment variables and cleans up any legacy OAuth2 files.
Source code in leafmap/terrascope.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
search(collection, bbox=None, start=None, end=None, max_cloud_cover=None, limit=None, unique_dates=True, **kwargs)
¶
Search for items in a Terrascope STAC collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection
|
str
|
Collection ID (e.g., "terrascope-s2-ndvi-v2"). |
required |
bbox
|
list[float] | None
|
Bounding box [west, south, east, north] in WGS84. |
None
|
start
|
str | datetime | None
|
Start date (string "YYYY-MM-DD" or datetime). |
None
|
end
|
str | datetime | None
|
End date (string "YYYY-MM-DD" or datetime). |
None
|
max_cloud_cover
|
float | None
|
Maximum cloud cover percentage (0-100). |
None
|
limit
|
int | None
|
Maximum number of items to return. |
None
|
unique_dates
|
bool
|
If True, return only one item per unique date. |
True
|
**kwargs
|
Additional arguments passed to pystac_client search. |
{}
|
Returns:
| Type | Description |
|---|---|
list
|
List of pystac Item objects, sorted by date. |
Example
items = terrascope.search( ... collection="terrascope-s2-ndvi-v2", ... bbox=[5.0, 51.2, 5.1, 51.3], ... start="2025-05-01", ... end="2025-06-01", ... max_cloud_cover=10, ... )
Source code in leafmap/terrascope.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
search_ndvi(bbox, start, end, max_cloud_cover=10.0, **kwargs)
¶
Search for Sentinel-2 NDVI products.
Convenience wrapper around search() for the terrascope-s2-ndvi-v2 collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
list[float]
|
Bounding box [west, south, east, north] in WGS84. |
required |
start
|
str | datetime
|
Start date (string "YYYY-MM-DD" or datetime). |
required |
end
|
str | datetime
|
End date (string "YYYY-MM-DD" or datetime). |
required |
max_cloud_cover
|
float
|
Maximum cloud cover percentage. Default 10%. |
10.0
|
**kwargs
|
Additional arguments passed to search(). |
{}
|
Returns:
| Type | Description |
|---|---|
list
|
List of pystac Item objects with NDVI assets. |
Example
items = terrascope.search_ndvi( ... bbox=[5.0, 51.2, 5.1, 51.3], ... start="2025-05-01", ... end="2025-06-01", ... ) print(f"Found {len(items)} NDVI scenes")
Source code in leafmap/terrascope.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |