COG Layer Example¶
This notebook demonstrates how to visualize Cloud Optimized GeoTIFFs (COGs) using the @developmentseed/deck.gl-geotiff package.
The COG layer provides GPU-accelerated rendering with automatic reprojection support, allowing you to visualize large raster datasets directly in the browser without server-side processing.
MapLibre Example¶
The add_cog_layer method is available on the MapLibre Map class. It uses deck.gl overlay to render COG files.
from anymap_ts import Map
# NLCD 2024 Land Cover COG (Continental US)
COG_URL = "https://s3.us-east-1.amazonaws.com/ds-deck.gl-raster-public/cog/Annual_NLCD_LndCov_2024_CU_C1V1.tif"
# Create a MapLibre map
m = Map(center=[-98.5, 39.8], zoom=4, style="dark-matter")
# Add the COG layer - the map will automatically fit to the COG bounds
m.add_cog_layer(COG_URL, name="nlcd-landcover", opacity=1.0)
m
DeckGL Example¶
The DeckGL map class also supports COG layers with additional debug options.
from anymap_ts import DeckGLMap
# Create a DeckGL map
m2 = DeckGLMap(center=[-98.5, 39.8], zoom=4, style="dark-matter")
# Add the COG layer with debug mode to visualize the reprojection mesh
m2.add_cog_layer(
COG_URL,
name="nlcd-landcover-debug",
debug=True, # Show the reprojection mesh
debug_opacity=0.25,
)
m2
Mapbox Example¶
The Mapbox map class also supports COG layers (requires a Mapbox access token).
import os
from anymap_ts import MapboxMap
# Create a Mapbox map (requires MAPBOX_TOKEN environment variable)
# Skip if no token is set
if os.environ.get("MAPBOX_TOKEN"):
m3 = MapboxMap(
center=[-98.5, 39.8], zoom=4, style="mapbox://styles/mapbox/dark-v11"
)
# Add the COG layer
m3.add_cog_layer(COG_URL, name="nlcd-landcover-mapbox", opacity=0.9)
display(m3)
else:
print("Set MAPBOX_TOKEN environment variable to use Mapbox maps")
Configuration Options¶
The add_cog_layer method supports several configuration options:
| Parameter | Type | Default | Description |
|---|---|---|---|
url |
str | required | URL to the Cloud Optimized GeoTIFF file |
name |
str | auto | Layer identifier |
opacity |
float | 1.0 | Layer opacity (0-1) |
visible |
bool | True | Whether layer is visible |
debug |
bool | False | Show reprojection mesh for debugging |
debug_opacity |
float | 0.25 | Opacity of debug mesh |
max_error |
float | 0.125 | Maximum reprojection error in pixels |
fit_bounds |
bool | True | Fit map to COG bounds after loading |
before_id |
str | None | ID of layer to insert before |
About the Dataset¶
The example uses the NLCD 2024 Land Cover dataset:
- Coverage: Continental United States
- Resolution: 30 meters
- Source: USGS National Land Cover Database
- Format: Cloud Optimized GeoTIFF (COG)
The dataset classifies land cover into categories including:
- Developed areas (open space, low/medium/high intensity)
- Forest (deciduous, evergreen, mixed)
- Agricultural (pasture, crops)
- Wetlands
- Water bodies
- And more...
# Export to standalone HTML
m.to_html("cog_layer_example.html")
print("Exported to cog_layer_example.html")